Handy tip: If you follow SET with a letter you will see all of the variables that start with that letter. In the example above SET L would show just %LOCALAPPDATA% and %LOGONSERVER%.
So where do these already available environment variables come from? Well it looks a little something like this:
There are 3 kinds of environment variables automatically available. There are variables whose values are calculated at logon such as %USERNAME% and %COMPUTERNAME%. These variables are static throughout the current session.
There are also variables that are static across sessions such as %OS% and %PATH%. You can find these variables by going to computer Properties > Advanced > Environment Variables.
Note there are two sections. User variables are just available to the current user while System variables are available to all users on the computer.
Sometimes it is fun and informative to look under the hood. How does the operating system know what these variables and their values between sessions? If you said, "the registry", you win the prize.
HKLM/System/CurrentControlSet/Session Manager/Environment and HKCU/Environment are where to check. If you need to you can hack the registry to create new system variables.
And note that even though these variables are static you can still change the values in your batch file. It is not generally recommended but you can do it. Most commonly a batch file will append a folder to the PATH with the command:
PATH %PATH%;C:\Some\New\Folder
So that's two types of variables. But I said there were three. Was I lying? After all, I did mislead you earlier about where variables come from as a cheap excuse to show a Monty Python clip. But I'm not lying, there really are three.
The third type of automatically available variables aren't displayed with the SET command. These variables are not static during your session but are calculated as needed. They are described at the end of HELP SET (or SET /? if you prefer).
%CD% - expands to the current directory string
%DATE% - expands to current date using same format as DATE command.
%TIME% - expands to current time using same format as TIME command.
%RANDOM% - expands to a random decimal number between 0 and 32767.
%ERRORLEVEL% - expands to the current ERRORLEVEL value
%CMDEXTVERSION% - expands to the current Command Processor Extensions
version number.
%CMDCMDLINE% - expands to the original command line that invoked the
Command Processor.
%DATE% - expands to current date using same format as DATE command.
%TIME% - expands to current time using same format as TIME command.
%RANDOM% - expands to a random decimal number between 0 and 32767.
%ERRORLEVEL% - expands to the current ERRORLEVEL value
%CMDEXTVERSION% - expands to the current Command Processor Extensions
version number.
%CMDCMDLINE% - expands to the original command line that invoked the
Command Processor.
These variables can be mangled just like any other variable.
%CMDEXTVERSION% is used to determine what commands are available. The value is 1 if the operating system is Windows NT and is 2 if the operating system is Windows 2000 or later. If you check this variable and the value is 1 you are working with limited command set and you might be wearing clothes that are out of style.
On Windows 7 and 2008 there is another variable named %HIGHESTNUMANODENUMBER% which is the highest NUMA node number on the machine. This is handy information in a multi-processor environment with NUMA support and multi-threaded applications. But for batch file processing leveraging this variable is probably overkill.
Commenters: Can you think of a reasonable use for the %HighestNumaNodeNumber% variable? Or is it like the pizza shop that offers pineapple as a topping knowing full well that nobody ever orders pineapple as a topping.
Your example is specious because Hawaiian pizza is quite popular.
ReplyDelete