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.
If you use %* to refer to all parameters, the value returned will include the delimiters.
^ Escape character.
Adding the escape character before a command symbol allows it to be treated as ordinary text. These characters which normally have a special meaning can be escaped and then treated like regular characters : & \ < > ^ |e.g. Echo THIS ^& THAT Echo Heading1 ^| heading2 ^| heading3 Echo The Escape character looks like this ^^
When piping or redirecting text, applying an escape character gets a little more complex, a pipe will start two new cmd.exe instances, each of these cmd processes are passed one side of the pipe and will parse their part once again (with the cmd-line parser, not with the batch-line-parser).
Knowing this you might try:
Echo THIS ^^& THAT | subroutine
So the new cmd instance would be passed THIS ^& THAT and everything would work? Not so fast, with two carets together, the first will escape the second, meaning that the & is no longer escaped at all.
Echo This ^^^& That | subroutine
In this version, the first caret escapes the second caret '^', the third caret escapes the '&' so now the new cmd instance inherits ^&
(Many thanks to Jeb who explained this over in the forum better than I could.)
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%\)
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 ^
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 %* by passing the value to a subroutine:
@ECHO OFF SET _params=%* :: pass params to a subroutine CALL :sub "%_params%" GOTO :eof :sub :: Now display just the filename (not path) ECHO %~n1
“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.
PowerShell Escape Character
In bash use \ to escape a line ending.
How does the Windows Command Interpreter (CMD.EXE) parse scripts? - StackOverflow.
Why is the DOS path character "\"? - Larry Osterman