Windows Environment Variables

Environment variables are mainly used within batch files, they can be created, modified and deleted for a session using the SETcommand. To make permanent changes, use SETX 

Variables can be displayed using either SET or ECHO.

Variables have a percent sign on both sides: %ThisIsAVariable%
The variable name can include spaces, punctuation and mixed case: %_Another Ex.ample%
(This is unlike Parameter variables which only have one % sign and are always one character long: %A )

In a batch file the default behaviour is to read and expand variables one line at a time, for example:

 SET /P _price="Enter the price: "
 ECHO %_price%

The above will work, but the version below will fail because %_price% will remain undefined until processing reaches the next line, this behaviour can be changed using SETLOCAL EnableDelayedExpansion

 SET /P _price="Enter the price: " & ECHO %_price%
 ECHO That didnt work!


Standard (built-in) Environment Variables

VariableDefault value: 
Windows XP
Default value: 
Windows 7/2008
%ALLUSERSPROFILE% C:\Documents and Settings\All Users C:\ProgramData
%APPDATA% C:\Documents and Settings\{username}\Application Data C:\Users\{username}\AppData\Roaming
%CommonProgramFiles% C:\Program Files\Common Files C:\Program Files\Common Files
%COMMONPROGRAMFILES(x86)% C:\Program Files (x86)\Common Files C:\Program Files (x86)\Common Files
%COMPUTERNAME% {computername} {computername}
%COMSPEC% C:\Windows\System32\cmd.exe C:\Windows\System32\cmd.exe
%HOMEDRIVE% C: C:
%HOMEPATH% \Documents and Settings\{username} \Users\{username}
%LOCALAPPDATA%

N/A

(but can be manually added LOCALAPPDATA=%USERPROFILE%\Local Settings\Application Data)

C:\Users\{username}\AppData\Local
%LOGONSERVER% \\{domain_logon_server} \\{domain_logon_server}
%PATH% C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem;{plus program paths} C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem;{plus program paths}
%PATHEXT% .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH; .MSC
%ProgramData% N/A C:\ProgramData
%ProgramFiles% C:\Program Files C:\Program Files
%ProgramFiles(x86)% 1 C:\Program Files (x86) C:\Program Files (x86)
%PROMPT% Code for current command prompt format,usually $P$G
C:>
Code for current command prompt format,usually $P$G
C:>
%PSModulePath% N/A %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\
%Public% N/A C:\Users\Public
%SYSTEMDRIVE% C: C:
%SYSTEMROOT% C:\Windows C:\Windows
%TEMP% and %TMP% C:\Documents and Settings\{username}\Local Settings\Temp C:\Users\{Username}\AppData\Local\Temp
%USERDOMAIN% {userdomain} {userdomain}
%USERNAME% {username} {username}
%USERPROFILE% %SystemDrive%\Documents and Settings\{username} %SystemDrive%\Users\{username}
%WINDIR% C:\Windows C:\Windows

1 Only on 64 bit systems, is used to store 32 bit programs.

By default, files stored under Local Settings do not roam with a roaming profile.

Environment variables are stored in the registry:

User Variables: HKEY_CURRENT_USER\Environment
System Variables: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Dynamic variables

There are also 6 dynamic environment variables, these are computed each time the variable is expanded.
n.b. you should not attempt to directly SET a dynamic variable.

%CD%   The current directory (string). 

%DATE%   The current date using same region specific format as DATE.

%ERRORLEVEL%   The current ERRORLEVEL value, automatically set when a program exits. 

%TIME%   The current time using same format as TIME

%RANDOM%   A random decimal number between 0 and 32767. 

%CMDEXTVERSION%   The current Command Processor Extensions version number.
(Windows NT = "1", Windows 2000, XP and later return "2".)

%CMDCMDLINE%
   The original command line that invoked the Command Processor.

Undocumented Dynamic variables (read only)

%__CD__%   The current directory, terminated with a trailing backslash.

%=C:%   The current directory of the C: drive.

%=D:%   The current directory of the D: drive if drive D: has been accessed in the current CMD session.

%=ExitCode%   The hex value of the last return code set by EXIT /B

%=ExitCodeAscii%   The ASCII value of the last return code set by EXIT /B if greater than 32.

Pass variables between batch scripts

There are several ways to pass values between batch files, or between a batch file and the command line, see the CALL andSETLOCAL pages for full details.

“Men may be convinced, but they cannot be pleased against their will. But though taste is obstinate, it is very variable, and time often prevails when arguments have failed” ~ Samuel Johnson

Related:

User Shell Folders - Standard folder locations for XP and Windows 7
Detecting 32 vs 64 bit Windows
CALL - Evaluate environment variables
SET - View environment variables, set local variables
SETX - Set environment variables
Q100843 - The four types of environment variable 
Q286705 - Set compatibility variables
Q242557 - Registry Settings for Folder Redirection
StackOverflow - Storing a Newline in a variable
Managing Roaming User Data Deployment Guide - Microsoft.com (Word Doc)


转自:http://ss64.com/nt/syntax-variables.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值