cmd batch use variable
1.define and del variable
set variablename=variablevalue_in_string
variable name is not case-sensitive, could have embedded spaces, like set test test=t
if value variablevalue_in_string is emty, the variable is deleted, like set variablename=, then set variablename will show tip that no such variable exists.
2.assign value to variable and del
set variablename=variablevalue_in_string
set /p variablename=please enter variablename value
set /p is accept use input to a variable
set /p variablename=<filename.txt
set /p =< will assign first row to a variable
to avoid your value endwith spaces, you can set "variablename=variablevalue_in_string"
3.dynamic evaluate variable value
set start=1
set len=2
set str=abcdefg
call set substr=%%str:~%start%,%len%%% (%%str:~1,2%% get substring)
you can pass args to variablevalue_in_string and evaluate the result
4.use/call variable
%variablename% if the variable is not defined, the %variablename% is emtpy:
set first=1
set sum=%first% +2
set sum will be:sum=1+2
set /a variablename=variablevalue_in_int operator other_variablename like:
set /a first=1 or set first=1 (when variable first with /a used in arithmetic expression, the value will be translated to number )
set /a sum=2+first
set sum will be sum=3
5.show variable value
set virablename show variable name and value.
echo %virablename% show variable value
4.query variable
set will show all variables and their values in current session of cmd
set p will show all matched variables start with "p" and their values in current session of cmd
if there is no variable matched, the errorlevel will be 1(an error)
6.variable in number
set /a variablename=variablevalue_in_int (a is arithmetic)
set a=1
set /a sum=2+a or set /a sum=2+%a%
set sum will be 3
set variable for hex/octal number set /a a=0x12 or set a=0x12 for hex number, and set /a a=02 set a=02 for octal number
7.system variable
The following table lists the system and local environment variables for Windows XP.
Variable | Type | Description |
%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. Uses the same format as the date /t command. Generated by Cmd.exe. For more information about the datecommand, see Date |
%ERRORLEVEL% | System | Returns the error code of the most recently used command. A non zero value usually indicates an error. |
%HOMEDRIVE% | System | Returns which local workstation drive letter is connected to the user's home directory. 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. 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. 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 operating system name. Windows 2000 displays the operating system as Windows_NT. |
%PATH% | System | Specifies the search path for executable files. |
%PATHEXT% | System | Returns a list of the file extensions that the operating system considers to be executable. |
%PROCESSOR_ARCHITECTURE% | System | Returns the chip architecture of the processor. Values: x86, IA64. |
%PROCESSOR_IDENTIFIER% | System | Returns a description of the processor. |
%PROCESSOR_LEVEL% | System | Returns the model number of the processor installed on the computer. |
%PROCESSOR_REVISION% | System | Returns the revision number of the processor. |
%PROMPT% | Local | Returns the command prompt settings for the current interpreter. Generated by Cmd.exe. |
%RANDOM% | System | Returns a random decimal number between 0 and 32767. Generated by Cmd.exe. |
%SYSTEMDRIVE% | System | Returns the drive containing the Windows XP root directory (that is, the system root). |
%SYSTEMROOT% | System | Returns the location of the Windows XP root directory. |
%TEMP% and %TMP% | System and User | Returns the default temporary directories that are used by applications available to users who are currently logged on. Some applications require TEMP and others require TMP. |
%TIME% | System | Returns the current time. Uses the same format as the time /t command. Generated by Cmd.exe. For more information about the timecommand, see Time |
%USERDOMAIN% | Local | Returns the name of the domain that contains the user's account. |
%USERNAME% | Local | Returns the name of the user who is currently logged on. |
%USERPROFILE% | Local | Returns the location of the profile for the current user. |
%WINDIR% | System | Returns the location of the operating system directory. |
8.permanent save variable
use setx, like setx a 3, without =
used after windows 2k3, of course you can download and install it
http://vlaurie.com/computers2/Articles/environment.htm
http://www.wilsonmar.com/1envvars.htm