The following is a collection of cases for strange behaviour
when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming"
, as I'm assuming some knowledge and not further explain the basics. For some basics I will create a tutorial to be published here very soon (reference will be posted here).
It's an Interpreter
If using complex mechanisms like subshelling (using commands enclosed in round brackets), keep in mind batch files are not really like using a programming language - batch language isn't that accurate, there are several flaws in the interpreter we have to take care of.
being an interpreter, it reads line after line, and does some pattern replacing in advance to executing the line.
A "line" can be
* a single physical line
* several physical lines connected by a caret (^) at the end of each line
* several physical lines enclosed in round brackets
A single line is hence:
if 1 == 1 (echo yes) else (echo no)
echo Writing a long ^
if 1 == 1 (
) else (
Why should one know? As said already, it's an interpreter we use, and it's applying some string replacements.
If you are using environment variables, which is one of the things you will use every time, this gets most important.
E.g. the following code will not work as expected:
if %example% == 1 (
REM result: 1
REM expected: 2
In the first line, variable example
is set to 1. In the next "line", containing IF
up to the closing bracket, each occurance of %example%