JSI Tip 3240. Windows 2000 delayed environment variable expansion.


In Windows NT and Windows 2000, the default behavior in the CMD command processor is to evaluate an environment variable once per statement execution.

To demonstrate this behavior, assume the root of your D: drive has 3 files:

File1.txt
File2.txt
File3.txt

If you run a batch script containing:

set LIST=
for %%i in (*) do set LIST=%LIST% %%i
echo %LIST%
You would see the following:
D:\>set LIST=
D:\>for %i in (*) do set LIST= %i
D:\>set LIST= File1.txt
D:\>set LIST= File2.txt
D:\>set LIST= File3.txt
D:\>echo  File3.txt
D:\> File3.txt
You can see from this example that the LIST variable is expanded just once when the FOR statement is executed. Since LIST was empty, only the last file found is set into the variable.

Windows 2000 supports delayed environment variable expansion. You must enable delayed environment variable expansion for the CMD session by using the CMD /V:ON switch, or by issuing a setlocal ENABLEDELAYEDEXPANSION command.

With delayed environment variable expansion enabled, you can use the ! instead of the %, as follows:

set LIST=
for %%i in (*) do set LIST=!LIST! %%i
echo %LIST%
This yields the following output:
D:\>set LIST=
D:\>for %i in (*) do set LIST=!LIST! %i
D:\>set LIST=!LIST! File1.txt
D:\>set LIST=!LIST! File2.txt
D:\>set LIST=!LIST! File3.txt
D:\>echo  File1.txt File2.txt File3.txt
D:\> File1.txt File2.txt File3.txt
You can have delayed environment variable expansion enabled by default, if you use Regedt32 to navigate to either of the following keys:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor

HKEY_CURRENT_USER\Software\Microsoft\Command Processor
On the Edit menu, Add Value name DelayedExpansion, as a REG_DWORD data type. A data value of 1 enables delayed environment variable expansion and a data value of 0 disables it. Invoking the /V:ON or /V:OFF switch on CMD.EXE and/or using the setlocal ENABLEDELAYEDEXPANSION command, overrides the registry setting for the CMD session.


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish