Someone around the office joked that my blog is turning into "Random Batch File Tips and Tricks." I'll continue that trend with this post...
I recently encountered an environment variable surrounded by exclamation points instead of the usual percent signs. (ie. !MYENVVAR! instead of %MYENVVAR%). It turns out that this is used to delay the expansion of the environment variable until it is actually needed. The first processing step that happens in a statement is to expand all the environment variables.
I was in the process of writing up a big example when I found one on microsoft.com already. Scroll down and click on "What is delayed environment variable expansion?"
[UPDATE] Looks like that link is dead, but you can find it on archive.org: http://web.archive.org/web/20080430040305/http://www.microsoft.com/technet/prodtechnol/Windows2000serv/support/FAQW2KCP.mspx I'll copy it here too just in case that archived version disappears:
The default behavior of the CMD command processor is to evaluate an environment variable once per statement execution.
To demonstrate this behavior, assume that your c:\test folder has 3 files:
If you run a batch script containing:@echo off cd /d c:\test @echo on set LIST= for %%i in (*) do set LIST=%LIST% %%i echo %LIST%
You would see the following:
C:\TEST\>for %i in (*) do set LIST= %i
C:\TEST\>set LIST= File1.txt
C:\TEST\>set LIST= File2.txt
C:\TEST\>set LIST= 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:@echo off cd /d c:\test @echo on set LIST= for %%i in (*) do set LIST=!LIST! %%i echo %LIST%
This yields the following output:
C:\TEST\>for %i in (*) do set LIST=!LIST! %i
C:\TEST\>set LIST=!LIST! File1.txt
C:\TEST\>set LIST=!LIST! File2.txt
C:\TEST\>set LIST=!LIST! File3.txt
C:\TEST\>echo File1.txt File2.txt File3.txt
C:\TEST\> 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.