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:
%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""
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.
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 v_params=%* ver | find "NT" >nul && set v_params=%v_params:~1% echo %v_params%
^ 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.
SET v_1=gg^<hh :: now %v_1% contains the text "gg
So far so good, but now if I try to use this variable
ECHO %v_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 v_1=gg^^^<hh :: now %v_1% contains the text "gg
^<hh" ECHO %v_1% :: Will display "gg <hh" SET v_2=%v_1% :: now %v_2% contains the text "gg <hh" Taking this further gives the following SET v_1=gg^^^^^^^<hh :: now %v_1% contains the text "gg ^^^<hh" SET v_2=%v_1% :: now %v_2% contains the text "gg^ <hh" SET v_3=%v_2% :: now %v_3% contains the text "gg <hh" Note that at this stage we can do ECHO %v_2% but ECHO %v_3% would return a file not found error.
You can remove quotes from a variable with the following:
:: Remove quotes SET v_string=###%v_string%### SET v_string=%v_string:"###=% SET v_string=%v_string:###"=% SET v_string=%v_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 v_params=%* :: Remove the first character if NT 4 ver | find "NT" >nul && set v_params=%v_params:~1% :: pass params to a subroutine CALL :s_next "%v_params%" GOTO :eof :s_next :: Now display just the filename (not path) ECHO %~n1