Hi,
Consider the following test.bat file:
@echo off
echo 1:%1 2:%2 3:%3
now run each of the following commandlines:
C:\>test.bat hi bye now
1:hi 2:bye 3:now
C:\>test.bat ""hi" "bye"" now again
1:""hi" "bye"" 2:now 3:again
C:\>test.bat ""hi hi" "bye"" now again
1:""hi 2:hi" "bye"" 3:now
How are quoted strings within quoted args working? The 2nd commandline seems fine, but throw a space in the inner quoted string and it gets messed up (the 3rd command)
An explanation of what's happening or a different method for passing along multiple quoted strings in one quoted arg would be much appreciated.
Thanks,
Paul
Quotes, Spaces in Args interpreted inconsistently?
Moderator: DosItHelp
-
- Posts: 1
- Joined: 18 Sep 2009 17:45
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
The short answer for "WHY?" Special characters are special. You can't stick a double quote inside other double quotes. How would you expect to consistently know if a double quote is supposed to be special or normal? In reality -- a pair of double quotes causes delimiters inside the double quotes to NOT be treated as delimiters. Consider:
test " "nodelim " " delimited
test " "" " delim "delim not inside"
So %1 is everything after the first delimiter character (the space after the word "test") until the next delimiter that ISN'T ignored (the space after the "m" -- because the space inside the double quotes is not considered a delimiter).
That's pretty much it. Double quotes are paired, in order, as they are seen. 1,2 3,4 5,6 etc. Delimiters inside pairs of quotes are not considered delimiters. Multiple delimiters together are treated as a single delimiter.
What to do about it:
If you need quotes inside quotes, then you will have to write the code yourself to accomodate your specific needs. In the scenario presented above, you would basically need to do something like assign %* to a variable and then use character substitution to change the double quote characters to some other non-special character, then you can manipulate that variable as you want, and finally (if needed) transform the non-special substitue character back into a double qutoe.
test " "nodelim " " delimited
test " "" " delim "delim not inside"
So %1 is everything after the first delimiter character (the space after the word "test") until the next delimiter that ISN'T ignored (the space after the "m" -- because the space inside the double quotes is not considered a delimiter).
That's pretty much it. Double quotes are paired, in order, as they are seen. 1,2 3,4 5,6 etc. Delimiters inside pairs of quotes are not considered delimiters. Multiple delimiters together are treated as a single delimiter.
What to do about it:
If you need quotes inside quotes, then you will have to write the code yourself to accomodate your specific needs. In the scenario presented above, you would basically need to do something like assign %* to a variable and then use character substitution to change the double quote characters to some other non-special character, then you can manipulate that variable as you want, and finally (if needed) transform the non-special substitue character back into a double qutoe.