tags:

views:

41

answers:

3

Hi

I am wanting to pass a space separated variable into a batch file like:

c:\applications\mi_pocess.bat A1 1AA

when I run echo %1 in mi_process it returns A1

How would I go about it recognising A1 1AA to be a single string?

I have tried wrapping it in Double quotes in my external software like

c:\applications\mi_pocess.bat + chr$(34) + A1 1AA + Chr$(34)

and echo %1 now returns "A1 1AA" (I do not want the quote marks in the variable)

Thanks

A: 

That is the only way to send a value containing spaces to the batch file, so if you don't want the quotation marks, you would have to remove them in the batch file.

Guffa
+2  A: 

I'm sure you are aware that %1, %2, etc. inside a .bat represent the numbered arguments passed to the .bat on the commandline.

If you use them as %~1, %~2, etc. all surrounding quotes will be removed automatically, should they be there.

Consider this space-in-args.bat for testing:

@echo off
echo.  %1
echo.    (original first argument echo'd)
echo.
echo. "%1"
echo.    (original first argument with additional surrounding quotes)
echo.
echo.  %~1
echo.    (use %%~1 instead of %%1 to remove surrounding quotes, should there be)
echo.
echo. "%~1"
echo.    (we better use "%%~1" instead of "%%1" in this case:
echo.      1. ensure, that argument is quoted when used inside the batch;
echo.      2. avoid quote doubling should the user have already passed quotes.
echo.

Run it:

space-in-args.bat "a b c d e"

Output is:

 "a b c d e"
     (original first argument echo'd)

""a b c d e""
     (original first argument with additional surrounding quotes)

  a b c d e
     (using %~1 instead of %1 removes surrounding quotes, should there be some)

 "a b c d e"
     (we better use "%~1" instead of "%1" in this case:
       1. ensure, that argument is quoted when used inside the batch;
       2. avoid quote doubling should the user have already passed quotes.

See also for /? (scroll down towards the end) to find out about some more of those conversions.

pipitas
Thanks Pipitas - that explain it nicely
Mark
A: 

You either want to use %~1 in the batch file:

echo %1
echo %~1

yields:

> test "A1 AA1"
"A1 AA1"
A1 AA1

because %~1 removes the quotes from the argument.

Another option would be %* if you never expect more than a single argument. %* contains all arguments to the batch file. This would enable you to avoid using quotes here but it also means that if you ever want to give another argument that should not be part of the one you're currently expecting, you're out of luck:

echo %*

then yields:

> test2 A1 AA1
A1 AA1
Joey
Thanks Johannes. Implemented similar in my processing now
Mark