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 with spaces in the Program Path requiring "quotes"CMD /k ""c:\batch files\test.cmd" "Parameter 1 with space" "Parameter2 with space""
In the FIND comand, the " quote can be escaped by doubling it to ""
Several methods for removing quotes are listed on the dequote page.
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=%* :: pass params to a subroutine CALL :sub "%_params%" GOTO :eof :sub :: Now display just the filename (not path) ECHO %~n1
Delimiters separate one parameter from the next - they split the command line up into words.
Parameters are most often separated by spaces, but any of the following are also valid delimiters:
Space ( )
Tab ( )
If you are passing a parameter to a batch file that contains any of these delimiter characters, it will split the parameter into two parameters unless you surround the whole thing with double quotes: "this is;one=param,"
Notice that although / and - are commonly used to separate 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 - )
One exception to this standard list of delimiters is the FOR command where the default is just [space] and [tab] and you can use the delims= option to specify something different.
When using the TAB character 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 Windows 2000 and above it won’t.
^ 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 characters you should prefix with the escape character: & \ < > ^ |
e.g. ^\ ^& ^| ^> ^< ^^
The ^ escape character can be used to make long commands more readable by splitting them into multiple lines and escaping the Carriage Return + Line Feed (CR/LF) at the end of a line:ROBOCOPY \\FileServ1\e$\users ^ \\FileServ2\e$\BackupUsers ^ /COPYALL /B /SEC /MIR ^ /R:0 /W:0 /LOG:MyLogfile.txt /NFL /NDL
Mark Yocom [MSFT] has more on this technique here.
A couple of things to be aware of:
- A stray space at the end of a line (after the ^) will break the command, this can be hard to spot unless you have a text editor that displays spaces and tab characters.
- If you want comment something out with REM, then EVERY line needs to be prefixed with REM.
Alternatively if you use a double colon :: as a REM comment, that will still parse the caret at the end of a line, so in the example above changing the first line to :: ROBOCOPY… will comment out the whole multi-line command.
Some commands (e.g. REG and FINDSTR) use the standard escape character of \ (as used by C, Python, SQL, bash and many other languages.)
The \ escape can cause problems with quoted directory paths that contain a trailing backslash because the closing quote " at the end of the line will be escaped \".
To save a directory path with a trailing backslash (\) requires adding a second backslash to 'escape the escape'
so for example instead of "C:\My Docs\" use "C:\My Docs\\"
To be sure that a path includes a trailing backslash, you can test for it:
Set _prog=C:\Program Files\SS64 App
IF %_prog:~-1% NEQ \ (Set _prog=%_prog%\)
When a pipe is used, the expressions are parsed twice. First when the expression before the pipe is executed and a second time when the expression after the pipe is executed. So to escape any characters in the second expression double escaping is needed:
The line below will echo a single `&` character:
break| echo ^^^&
The % character has a special meaning for command line parameters and FOR parameters.
To treat a percent as a regular character, double it:
Many characters such as \ = ( ) do not need to be escaped when they are used within a "quoted string" typically these are characters you might find in a filename/path. The percent character is one exception to this rule, even though under NTFS % is a valid filename character.
Escaping Exclamation marks
When the shell is running in EnableDelayedExpansion mode the ! character is used to denote a variable and so must be escaped (twice) if you wish to treat it as a regular character:
The escape character can be used to escape itself ^^ (meaning don’t treat the first ^ as an escape character), so you are escaping the escape character:The characters in bold get escaped: ^& => & ^^^& => ^& ^^^^^& => ^^&
A small number of commands follow slightly different rules, FINDSTR, REG and RUNAS all use \ as an escape character instead of ^
“All the best stories in the world are but one story in reality - the story of escape. It is the only thing which interests us all and at all times, how to escape” ~ A. C. Benson
SETLOCAL EnableDelayedExpansion - More examples, particularly for HTML.
Long Filenames and NTFS - Valid characters in filenames.
FINDSTR Escapes and Length limits.
How does the Windows Command Interpreter (CMD.EXE) parse scripts? - StackOverflow.
Powershell Escape Character
In bash use \ to escape a line ending.