Parenthesis can be used to split commands across multiple lines. This can make code more readable. Variables will be evaluated for the code block just as if the command was a single line.
(command) ( command command )
IF EXIST C:\pagefile.sys ( ECHO pagefile found on C: drive)
If the command will fit on a single line, then the parenthesis can be omitted e.g.
IF EXIST data.xls Echo The file was found.
IF EXIST data.xls ( Echo The file was found. )
The CMD shell will expand [read into memory] all the variables at the beginning of a code block and use those values even if the variable’s value is changed within the code block. Turning on DelayedExpansion will force the shell to read variables at the start of every line.
Code blocks can be used to expand variables in places where they would not otherwise be accepted such as the "tokens" parameter of the FOR command:@echo off set tknz="tokens=1-2" ( for /f %tknz% %%A in ('echo') do ( echo %%B echo %%A ) )
The CMD shell does not use any great intelligence when evaluating parenthesis, so for example the command below will fail:
IF EXIST MyFile.txt (ECHO Some(more)Potatoes)
This version will work:
IF EXIST MyFile.txt (ECHO Some[more]Potatoes)
You could also escape the extra parenthesis like (ECHO Some^(more^)Potatoes)
Or use quotes; though they will appear in the output (ECHO "Some(more)Potatoes")
Although parenthesis are legal in NTFS pathnames, they are often misinterpreted.
Chaining to a second batch file (without using CALL) will normally exit the first batch file completely, but if you do this from a code block within parentheses then execution will return to the first batch file and continue where it left off.
This action appears to be very similar to just using CALL, but in some cases; particularly using parenthesis within the DO section of a FOR loop; the behaviour becomes buggy. A recommended coding style is to always explicitly use CALL when running a second batch file.
The CMD shell will read the entire content of a code block in parenthesis into memory at once. That code will always be executed unless you break out of the block by executing a goto :label or goto :eof
A GOTO command inside a bracketed code block will break the parenthesis context and may cause errors. For example within a For-Do Loop, a GOTO command will exit the entire loop not just the current iteration.
The interpretation of comments/Labels within a code block is problematic, so put all comments outside the parenthesis.
Do not use parenthesis or quotes if you are comparing numeric values with an IF command.
IF (2) GEQ (15) echo "bigger"
IF "2" GEQ "15" echo "bigger"
Will perform a character comparison and will echo "bigger"
however the commands:
IF 2 GEQ 15 echo "bigger"
IF (2 GEQ 15) echo "bigger"
Will perform a numeric comparison and return the correct result.
This behaviour is opposite to the SET /a command where quotes are required.
The maximum number of nested expressions in parenthesis is 256 (more will cause a buffer overrun).
“Yes, we are the richest major nation. But because so much of our national income is concentrated in relatively few hands, large numbers of Americans are worse off economically than their counterparts in other advanced countries” - Paul Krugman
DelayedExpansion - Force the shell to read variables at the start of every line.
IF - conditional command