Variable types in the Windows CLI

From Michael's Information Zone
Jump to navigation Jump to search

http://stackoverflow.com/questions/14509652/what-is-the-difference-between-and-in-a-cmd-file

PLEASE CHECK THE LINK ABOVE, all credit goes to marapet for the post.

Three things to know:

    The percent sign is used in batch files to represent command line parameters: %1, %2, ...

    Two percent signs with any characters in between them are interpreted as a variable:

    echo %myvar%
    Two percent signs without anything in between (in a batch file) are treated like a single percent sign in a command (not a batch file): %%f

Why's that?

For example, if we execute your (simplified) command line

FOR /f %f in ('dir /b .') DO somecommand %f

in a batch file, rule 2 would try to interpret

%f in ('dir /b .') DO somecommand %

as a variable. In order to prevent that, you have to apply rule 3 and escape the % with an second %:

FOR /f %%f in ('dir /b .') DO somecommand %%f