Home » Computers » Windows » Variable expansion

Variable expansion

Something to remember is that be default .bat and .cmd scripts do not process all the variable updates, or expansion, immediately. To use variable that are updated or created inside a loop for instance you must have Variable Expansion enabled.

A quick check to see if  variable expansion is enable in your command prompt is to run this:

If Variable Expansion is ON you will see:
c:\> echo !errorlevel!
0

If expansion is OFF you get:
c:\> echo !errorlevel!
!errorlevel!

When launching cmd.exe from a prompt; Command-line; Run…; or Search programs and files you can specify the /V:ON parameter to enable expansion.

Within a bat/cmd file variable expansion can be controlled through the SETLOCAL [ENABLEDELAYEDEXPANSION | DISABLEDELAYEDEXPANSION] statement.

Turn on variable expansion (delayed) and you will not be sorry.

–Mike


Leave a comment