Question About Quotation Marks
Posted: 28 Feb 2013 18:53
As jeb's answer here: viewtopic.php?f=3&t=4345 highlighted, quotation marks in env vars often make if statements explode. We wrap each side of the comparison in quotation marks to avoid other problems.
So when I was working on something to check if a batch was run via double-click / ShellExecute / etc. versus run directly via command line, I got confused by situations where quotation marks in env vars WASN'T making things explode.
Please forgive the use of " as the name of the env var I work with; it makes things more confusing to read, but I wanted to show exactly what I was working with:
Most noteworthy is set ""=%0" which, even if %0 contains quotation marks, doesn't break. But I don't understand why. Is set just special and matches the first quotation mark to the final quotation mark, rather than to the first intermediate quotation mark it encounters? Does elsewhere in batch behave this way, because it sure doesn't seem to in if statements where it would be useful.
Also, set ""=" to clear the " env var had to end a line as I was having it choke if there was whitespace after it.
Aaand, how viable of a check is this? It works for me on XP SP3 but I haven't tested it anywhere else yet. The "%~0"=="%~f0" comparison was an afterthought and I intend to work it into the test a little more fluidly.
Queue
So when I was working on something to check if a batch was run via double-click / ShellExecute / etc. versus run directly via command line, I got confused by situations where quotation marks in env vars WASN'T making things explode.
Please forgive the use of " as the name of the env var I work with; it makes things more confusing to read, but I wanted to show exactly what I was working with:
Code: Select all
set CMDCMDLINE=%CMDCMDLINE%
set ""=%0"
set ""=%":~2,1%%CMDCMDLINE:~-2,1%%CMDCMDLINE:~10,1%"
set ""=%":"=%"
if /i not "%~0"=="%~f0" set ""="
if "%"%"==": :" ( set ""="" ) else set ""="
if not defined ^" echo on & prompt;::$S$T$_
Most noteworthy is set ""=%0" which, even if %0 contains quotation marks, doesn't break. But I don't understand why. Is set just special and matches the first quotation mark to the final quotation mark, rather than to the first intermediate quotation mark it encounters? Does elsewhere in batch behave this way, because it sure doesn't seem to in if statements where it would be useful.
Also, set ""=" to clear the " env var had to end a line as I was having it choke if there was whitespace after it.
Aaand, how viable of a check is this? It works for me on XP SP3 but I haven't tested it anywhere else yet. The "%~0"=="%~f0" comparison was an afterthought and I intend to work it into the test a little more fluidly.
Queue