Syntax : Escape Characters, Delimiters and Quotes
Using "Double Quotes"
If a single parameter contains spaces, you can still pass it as one item by
surrounding in "quotes" - this works well for long filenames.
If a parameter is used to supply a filename; like this
MyBatch.cmd "C:\Program Files\My Data File.txt"
This parameters will be:
%0 =MyBatch
%1 ="C:\Program Files\My Data File.txt"
To launch a batch script which itself requires "quotes"
CMD /k ""c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space""
Removing Quotes
You can remove quotes from a variable with the following:
:: Remove quotes SET _string=###%_string%### SET _string=%_string:"###=% SET _string=%_string:###"=% SET _string=%_string:###=% Unfortunately the above will fail if the variable is NULL. Here is a batch routine to reliably remove quotes.
Working without Quotes
Without surrounding quotes, a long filename will be passed as %1 %2 %3...
MyBatch C:\Program Files\My Data File.txt
To refer to the pathname above use %* rather than %1 %2 %3 - the %* will cover all parameters - even if there are more than %9
You can apply Extended Filename syntax to %* with the following workaround:
@ECHO OFF SET _params=%* :: Remove the first character if NT 4 ver | find "NT" >nul && set _params=%_params:~1% :: pass params to a subroutine CALL :s_next "%_params%" GOTO :eof :s_next :: Now display just the filename (not path) ECHO %~n1
Delimiters
Delimiters separate one parameter from the next ( they split the command
line up into words)
Parameters are normally separated by spaces, but any of the following are also
valid delimiters.
Comma ,
Semicolon ;
Equals =
Space
Tab
Notice that although / and - are commonly used to separate NT command options,
they are absent from the list above. This is because batch file parameters are
passed to CMD.exe which can accept it's own parameters (which are invoked using
/ and - )
Most commands will accept any of the delimiters above with the exception of
FOR - the FOR command assumes a SPACE delimiter - for anything else you need
to specify the `delims=' option.
When using the TAB as a delimiter be aware that many text editors will insert
a TAB as a series of SPACEs.
When you use %* to refer to all parameters, the value returned will include
the delimiters, under NT 4.0 this will include the leading space, under Win
2K and Win XP it won't.
:: Removing a leading space set _params=%* ver | find "NT" >nul && set _params=%_params:~1% echo %_params%
Escape Character
^ Escape character. Adding the escape character before a command symbol
allows it to be treated as ordinary text.
When piping or redirecting any of these charcters you should prefix with the escape character \ & | > < ^ ( ^\ ^& ^| ^> ^< ^^ ) for example: ECHO This is > Than That would echo the text "This is" into a file
ECHO This is ^> Than That will echo the whole line "This is > Than That"
Storing an escape character in a variable
This is typically required when building HTML by storing a mix of text and
tags in an environment variable.
for example
SET _1=gg^<hh :: now %_1% contains the text "gg<hh"
So far so good, but now if I try to use this variable
ECHO %_1% - The system cannot find the file specified. One way around this problem is to add a second escape caret to the variable, the second caret also has to be escaped with a third caret: "^^^<" SET _1=gg^^^<hh :: now %_1% contains the text "gg^<hh" ECHO %_1% :: Will display "gg <hh" SET _2=%_1% :: now %_2% contains the text "gg <hh" Taking this further gives the following SET _1=gg^^^^^^^<hh :: now %_1% contains the text "gg ^^^<hh" SET _2=%_1% ECHO %_2% :: Will display "gg <hh" :: %_2% actually contains the text "gg^ <hh")
Related commands:
NT Syntax
Long Filenames and NTFS - Valid characters in filenames