DOS and Batch Files

Escape Character

It is sometimes necessary to want to use a character that is used as a command or a condition in a batch file as a real character. Examples would be <, >, |, %, & and ^.

To do this you need to "escape" the command character and in DOS batch files this is done with ^ character which is especially useful when piping or redirecting characters. There is one exception and that is where the character % needs to be escaped and the format for that is %%

For example none of the following commands will work properly in batch files

echo 50% >> textfile.txt
echo <title>My HTML Page</title> >> textfile.txt
echo start > programs >> textfile.txt
echo eggs & bacon >> textfile.txt
echo Start | programs >> textfile.txt
echo 4^2 >> textfile.txt

The output will not work at all well and the output if all these commands are run together will be that textfile.txt will simple consist of one line saying 50. Instead the characters have to be escaped and the batch file will look like this...

echo 50%% >> textfile.txt
echo ^<title^>My HTML Page^</title^> >> textfile.txt
echo start ^> programs >> textfile.txt
echo eggs ^& bacon >> textfile.txt
echo Start ^| programs >> textfile.txt
echo 4^^2 >> textfile.txt

Textfile.txt will then look like this...

50%
<title>My HTML Page</title>
start > programs
eggs & bacon
Start | programs
4^2

This page created 26th December 2011, last modified 30th December, 2011


GoStats stats counter