Whether the specific variables are created by the operating system or by you, the Environment Variables have many uses. The concept of environment variables has been with us since the days of DOS and they continue to be with us even in the era of Windows XP and 2003.
You can quickly view what variables currently exist... click on Start, Run and type "cmd" + [Enter] ("command" instead of "cmd" in the case of Win95/98/ME). When the DOS command window opens, type "set" + [Enter]. All the existing environment variables and their current contents will scroll down the DOS command window.
One of the many uses we find convenient is the fact that all versions of windows have an environment variable called "windir" which contains the full path to the windows system root directory. In W98/Me, this defaults to "C:\Windows"; in WinNT and Win2K, this is "C:\Winnt" and in XP this is "C:\Windows". The actual path of the Windows system folder may differ depending on whether the default was chosen or a different folder was created during the Windows installation process. In any case, the "windir" environment variable will always contain the valid path to the Windows directory.
Most scripting and programming languages allow the reading or writing of environment variables so if you should ever create any generic script or program intended to run in all your various Windows version workstations, the best place to put such scripts would be in the Windows system root directory and run that script from that location. The problem that the actual folder name may differ in each workstation does not really matter since we have the "windir" environment variable to refer to the full path.
In DOS command batch statements, for example, you can replace the path with %windir%.
So let us say you have a script called "example.bat" placed in the
Windows directory, the full path to the batch file would be:
%windir%\example.bat
This is true regardless of what the actual path to the Windows directory might be.
Notice that the path contained in %windir% does not have the last backslash so you need to add it in the batch statement.
Try this... open the Windows folder, and select one of the files in that folder. For this trial, right-click a text file (one with the .txt filename extension) and select "Create a Shortcut". Once the shortcut has been made, drag it to the desktop and right-click properties.
The actual syntax for the "windir" environment referencing will depend on the scripting language you are using. For Autoit3, it is best to get the environment into your own variable first as in the example statements below:
$Env = EnvGet("windir")
$Batpath = $Env & "\example.bat"
Do remember that environment variable names are case sensitive. Most pre-existing "new" variables are upper-case, but most "old" variables are in their original lower-case. "windir" is one of those variables that have survived version changes in Windows and remain in lower-case. Another environment variable, "SystemRoot" exists and can be used in place of "windir", but for reason of brevity and backward compatibility, "windir" seems to be a better choice for this purpose.
Other environment variables are used in some of our other tips. Environment variables are so useful that we cannot live without them.