Processing Semicolon on Command line
Solution 1
I found a little trick to get around the way the shell is interpreting the value of "%1" in the FOR /F loop: instead of parsing the string, parse the output of the command ECHO %1
, like this:
FOR /F "tokens=1,2 delims=:" %%a IN ( 'ECHO %1' ) DO ECHO Switch: %%a Value: %%b
This works if you put the password in quotes on the command line (call script2.bat -pw="my;password"
), so we'll have to remove the quotes as follows:
SET VALUE=%VALUE:~1,-1%
So this is the code I came up with:
ECHO OFF
ECHO Arguments: %1
FOR /F "tokens=1,2 delims=:" %%a IN ( 'ECHO %1' ) DO (
SET SWITCH=%%a
SET VALUE=%%b
)
ECHO SWITCH: %SWITCH%
SET VALUE=%VALUE:~1,-1%
ECHO VALUE: %VALUE%
...which returns the following results:
Arugments: -pw:"my;Password"
SWITCH: -pw
VALUE: my;Password
Solution 2
Try escaping the ; with a ^.
call script2.bat "-pw:my^;Password"
Joe Simon
Updated on August 07, 2022Comments
-
Joe Simon over 1 year
I have two batch files which is used to run a large C++ build, the first one starts the processes, creating directories and figuring out what to send to the second script. If certain information is presented to the first script, I have a routine that pops up a window and asks for a password. This is passed to the second script by calling the second script like this
call script2.bat -pw:myPassword
where myPassword is something the user entered. now, i have been testing this script and one of my users password contains a semicolon, so we get this
call script2.bat -pw:my;Password
I found by putting in quotes I can get this into the second script OK
call script2.bat -pw:"my;Password"
However, the command line parsing breaks when I try to do this
for /F "tokens=1,2 delims=:" %%a in ( "%1" ) DO SET switch=%%a&value=%%b
if I
echo %1
it shows like this-pw:"my;Password"
But with echo on when the script runs I see
for /F "tokens=1,2 delims=:" %%a in ( "-pw:"my Password"" ) DO SET switch=%%a&value=%%b
and it parses as
switch=-pw
andvalue="my
What I eventually need is for value to contain my;Password so I can pass it to another program
Any ideas on how to get this to parse correctly
Here re 2 batch file that issulstrate the problem:
a.bat:
echo on call b.bat -pw:eatme call b.bat -pw:eat;me call b.bat -pw:"eat;me" call "b.bat -pw:\"eat;me\""
b.bat:
echo on echo %1 for /F "tokens=1,2 delims=: " %%a in ( "%1" ) DO SET switch=%%a&SET value=%%b echo switch=%switch% echo value=%value%
-
Joe Simon about 14 yearsI must not be understanding something, I thought I did put the command in double quotes, -pw:"my;Password" when it get to the FOR statement, hgowever it shows up as "-pm:"my Password"", the semi-colon is gone. It seems to have something to do with the "for" statement, since when in do echo %1 immediately before the "for" it shows up as -pw:"my;Password".
-
Joe Simon about 14 yearsPutting the quotes around the whole command line (other than the call) results in an error "The filename, directory name, or volume lable syntax is incorrect"
-
Joe Simon about 14 yearsHere are 2 batch files that issulstrate the problem a.bat: echo on call b.bat -pw:eatme call b.bat -pw:eat;me call b.bat -pw:"eat;me" call "b.bat -pw:\"eat;me\"" b.bat echo on echo %1 for /F "tokens=1,2 delims=: " %%a in ( "%1" ) DO SET switch=%%a&SET value=%%b echo switch=%switch% echo value=%value%
-
Joe Simon about 14 yearsClose, I end up with an extra " at the end H:\My Documents\test>a H:\My Documents\test>echo on H:\My Documents\test>call b.bat -pw:"eat;me" H:\My Documents\test>ECHO OFF Arguments: -pw:"eat;me" SWITCH: -pw VALUE: eat;me"
-
ewall about 14 yearsAre you sure you have the syntax correct for
SET VALUE=%VALUE:~1,-1%
? That should remove the first and last characters, whatever they are... -
Joe Simon about 14 yearsI don't know what is going on. It always removes only the first character... maybe there are some spaces at the end that are not visible ? I will look into that.
-
Joe Simon about 14 yearsYes, it was trailing spaces... Thanks for the Help !
-
KatieK over 11 yearsThis would be a better answer if it included the essential parts of the answer here, and provided the link for reference. Link-only answers can become invalid if the linked page changes.
-
Tim Friesen over 8 yearsJust what I needed. Escaping the semicolon with the caret caused cmd to properly handle my input at least in my case.