A. You can use environment variables to gain information about a system. For a complete list of environment variables available in Windows, see the following table:
Table 1: Environment Variables
ALLUSERSPROFILE
Local returns the location of the All Users Profile.
APPDATA
Local returns the location where applications store data by
default.
CD
Local returns the current directory string.
CMDCMDLINE
Local returns the exact command line used to start the
current cmd.exe.
CMDEXTVERSION
System returns the version number of the current Command
Processor Extensions.
COMPUTERNAME
System returns the name of the computer.
COMSPEC
System returns the exact path to the command shell
executable.
DATE
System returns the current date. This variable uses the same format as the date /t command. Cmd.exe generates this variable. For more information about the date command, see the Date command.
ERRORLEVEL
System returns the error code of the most recently used
command. A non-0 value usually indicates an error.
HOMEDRIVE
System returns which local workstation drive letter is
connected to the user's home directory. This variable is set based on the value of the home directory. The user's home directory is specified in Local Users and Groups.
HOMEPATH
System returns the full path of the user's home directory.
This variable is set based on the value of the home directory. The user's home directory is specified in Local Users and Groups.
HOMESHARE
System returns the network path to the user's shared home
directory. This variable is set based on the value of the home directory. The user's home directory is specified in Local Users and Groups.
LOGONSEVER
Local returns the name of the domain controller that
validated the current logon session.
NUMBER_OF_PROCESSORS
System specifies the number of processors installed on the
computer.
OS
System returns the OS name. Windows XP and Windows 2000 display the OS as Windows_NT.
PATH
System specifies the search path for executable files.
PATHEXT
System returns a list of the file extensions that the
OS considers to be executable.
PROCESSOR_ARCHITECTURE
System returns the processor's chip architecture.
Values: x86, IA64.
PROCESSOR_IDENTFIER
System returns a description of the processor.
PROCESSOR_LEVEL
System returns the model number of the computer's processor.
PROCESSOR_REVISION
System returns the revision number of the processor.
PROMPT
Local returns the command-prompt settings for the current
interpreter. Cmd.exe generates this variable.
RANDOM
System returns a random decimal number between 0 and 32767. Cmd.exe generates this variable.
SYSTEMDRIVE
System returns the drive containing the Windows root
directory (i.e., the system root).
SYSTEMROOT
System returns the location of the Windows root directory.
TEMP or TMP
System and User return the default temporary directories
for applications that are available to users who are currently logged
on. Some applications require TEMP and others require TMP.
TIME
System returns the current time. This variable uses the same format as the time /t command. Cmd.exe generates this variable. For more information about the time command, see the Time command.
USERDOMAIN
Local returns the name of the domain that contains the
user's account.
USERNAME
Local returns the name of the user currently logged
on.
USERPROFILE
Local returns the location of the profile for the current
user.
WINDIR
System returns the location of the OS directory
To access these environment variables, you must place a percentage symbol (%) before and after the variable. For example,
echo %PROCESSOR_ARCHITECTURE%
x86
will display the processor type (but still won't display Intel's ia64 platform).
Some environment variables are available only in later OSs (e.g., %RANDOM% is not available in Windows NT 4.0).
Reader Comments
Well, in xp, set USERPROFILE=D:\Somefile works only during the current session. Logging out and in resets the environment variable to default. I don't know how to fix that.
Rendall -March 15, 2004
Most excellent article! Looked all over for this info.
Darrel Doehr -May 26, 2004
Perfect
HarryC0der -July 20, 2004
finally - the list of variables
I've been looking for this for quite a while...
Anonymous User -November 01, 2004
Wish there were more.
Anonymous User -November 16, 2004
USERPROFILE=%HOMEDRIVE%%HOMEPATH%
Anonymous User -November 18, 2004
happiness.
Anonymous User -January 10, 2005
i have set reg values for env so that bat files can use them but the pc needs to be rebooted before set can see them from cmd prompt,who can i force a reread from a vbs script, is there a reg reread similar to reg read/regwrite
Anonymous User -February 07, 2005
How would I store the value of an environment variable into....say a text file?
Anonymous User -February 23, 2005
How would I store the value of an environment variable into....say a text file?
Anonymous User -February 23, 2005
go to a command prompt and type set > c:\textfile.txt
Anonymous User -February 25, 2005
oops or
echo %somevariable% > c:\somefile.txt
Anonymous User -February 25, 2005
does anyone know if there is one to find the path that a bat file was started from. i want to copy a file from the same dir as the .bat file TIA
Dom
Anonymous User -February 25, 2005
Great Article
Anonymous User -March 08, 2005
It's a good article but i think there are more environment variables p.e. %programfiles%
Anonymous User -March 15, 2005
I want to uses there variables in java or vbscript for some calcuation can any one tell me some api for this task
to the person looking to copy a file from the same directory that the .BAT or .CMD file was run from, simply do this
copy %0\..\filename.ext destination
It works!
Anonymous User -March 29, 2005
Terrific article!! I was looking for something like this for a long time.
Anonymous User -April 19, 2005
when java(ver.1.5) is installed in windows it put the java.exe in windows system folder and also in its on directory, and it the path variable if we add the path of java, ie: path=%path%;C:\JDK1.5\bin; which java.exe will be invocked by windows ?? java.exe in he system folder or java.exe in the jdk folder ?? and also is there any way to understand the full path name of the program that is beeing executed ???
ie: there are two java.exe in the path, one in the windows system folder and one in the jdk folder, and path of both directoy is mapped into the path variable, how to know which java.exe is working ????
Anonymous User -August 23, 2005
Rendall Said:
> Well, in xp, set USERPROFILE=D:\Somefile works
> only during the current session. Logging out and
> in resets the environment variable to default. I
> don't know how to fix that.
If you have XP Pro, you can set that under "Manage Users". If you have XP Home will have to manually edit your registry. Open the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
You need to find your profile discription, the easyest way to do this is:
Click Edit -> Find
then type: \YOUR_USER_NAME_HERE
(replace YOUR_USER_NAME_HERE with your username, put NO space between the \ and your username)
check "Match whole string only"
Click "Find Next"
This should find your profile information, stopping on the entry "ProfileImagePath".
Right Click "ProfileImagePath" -> Modify
You will now be able to modify your profile path, something currently like "%SystemDrive%\Documents and Settings\xxx"
So, in short, you want to edit this entry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-???-???-???\ProfileImagePath
Disclaimer: Messing with your registry can turn your PC into a nice paperweight, so backup anything you are about to change. And then STILL act with care. The registry is not for the faint of heart, nor the stupid.
=====
Dom Asked:
> [how can I] find the path that a bat file was started from.
> i want to copy a file from the same dir as the .bat file TIA
> Dom
%~dp0
To use it as requested:
copy "%~dp0\somefile.ext" "E:\where\ever\U\like"
FYI some neat ~ options are
d - drive
p - path
n - name (w/o extention)
x - extention only
s - short filenames (8.3 format)
a - the file attributes
t - time and date
z - file size
(These are not the only modifiers, but they are the ones I use most.)
All or none can be used, and in any order, so %~dpnxsatz0 will return something like:
--a------ 08/19/2007 01:46 AM 127 C:\PROGRA~1\SUPPOR~1\x.bat
Free CDs Offer Fundamental Content for IT Pros Are you up to speed on the latest technologies and solutions? Don't miss out on your chance to get up to speed quickly on fundamental, in-depth information on some of the hottest topics in our library of content.
Let Your Users Reset Their Own Passwords: Free Download Try a 30 day free trial of Desktop Authority Password Self-Service – it provides an easy-to-use, robust system for allowing users to reset their own forgotten passwords or locked accounts.
Get Windows IT Pro & Mark Minasi’s Favorite Power Tools Guide Order Windows IT Pro now and get "More of Mark Minasi's Favorite Power Tools"--a in-depth guide to the most useful Windows commands --FREE with your paid order! Subscribe today, and save 58% off the cover price!
Deep Dive into VMware vSphere, eLearning Series Join John Savill to explore the major functionality capabilities of the vSphere virtualization platform, including identification of the changes from ESX 3.5.