Command Prompt for Windows




Environment Variables

Environment variables can be used to store a sequence of characters which can be instantiated or manipulated using the SET, XSET or LET commands from either the command prompt or at various points within a batch file.

Standard Environment Variables

Standard environment variables have the format %name%. For example, %COMSPEC% inside a batch file will be replaced with D:\WINDOWS\SYSTEM32\CMD.EXE. This value is system dependent and may vary from system to system.

Multi-dimensional Environment variables

Multi-dimensional environment variables have the following syntax :-

NAME.INDEX1.INDEX2.INDEX3[....]

where NAME is the name of the environment variable and INDEX1, INDEX2 and so on specifies the index values, which may either be an integer value or the name of another environment variable which contains an integer value. Consider the following example, where the index value is specified by the contents of another environment variable :-

SET idx=0
SET array.idx=10
ECHO %array.idx%

When the WinOne® Command Prompt encounters the environment variable array.idx then the idx part is replaced with the contents of the idx environment variable, mapping the final name to array.0. The environment variable array.0 is then created and assigned the value 10. The ECHO command will display the value 10. Continuing from the above example :-

LET idx=%idx% + 1
SET array.idx=20
ECHO %array.idx%

Now the environment variable idx will contain the value 1 and the environment variable array.1 is created and assigned the value 20. The ECHO command will display the value 20. Continuing from the above example :-

FOR %%i is 0 to 1 DO ECHO %array.%%i%

the FOR loop is identical to the following :-

ECHO %array.0%
ECHO %array.1%

and will display the value 10 on the first line and the value 20 on the second line.

Dynamic Environment Variables

Dynamic environment variables are environment variables that contain values that are determined dynamically, that is, at the time they are instantiated. The following dynamic environment variables are allowed :-

CMDEXTVERSIONCurrent extension version number
CMDCMDLINEOriginal command line that invoked the WinOne® Command Prompt
DATECurrent date (formatted the same as command DATE)
DATE_FORMATSystem date format string
DATE_TODAYCurrent date (formatted according to DATE_FORMAT)
DATE_YEARCurrent year (yyyy)
DATE_MONTHCurrent month (mm)
DATE_DAYCurrent day (dd)
DATE_DAYOFWEEKCurrent day of the week (0=Sun)
ERRORLEVELLast errorlevel set
TIMECurrent time (formatted the same as command TIME)
TIME_TODAYCurrent time (hh:mm:ss in 24 hour format)
TIME_HOURCurrent hour (hh as 24 hour)
TIME_MINUTECurrent minute (mm)
TIME_SECONDCurrent second (ss)
DISK_DRIVECurrent disk drive letter (A, B, C ...)
DISK_PATHCurrent working directory
CDSame as DISK_PATH
SCREEN_WIDTHCurrent screen width
SCREEN_HEIGHTCurrent screen height
WHERE_XHorizontal cursor location (starting from 0)
WHERE_YVertical cursor location (starting from 0)
WINDOWS_VERSIONWindows version number
WINONE_VERSIONThe WinOne® Command Prompt version number
WINONE_FILENAMEFull path of the the WinOne® Command Prompt executable
WINONE_PATHThe WinOne® Command Prompt home directory
DESKTOP_PATHPath of the Windows Desktop
SENDTO_PATHPath of the Sendto folder
STARTMENU_PATHPath of the Start Menu
STARTUP_PATHWindows Startup directory
MY_DOCUMENTSPath of the local My Documents folder
MY_RECENT_DOCUMENTSPath of the local Recent Documents folder
MY_MUSICPath of the local My Music folder
MY_PICTURESPath of the local My Pictures folder
MY_VIDEOSPath of the local My Videos folder
MY_FAVORITESPath of the local My Favourites folder
OPERATING_SYSTEMOperating system (WinNT or OTHER)
RANDOMRandom number (0 to 32767)
RANDOM_NUMBERRandom number (0 to 65534)

To use these dynamic environment variables, enclose the name in between percentage characters. For example to display the system date format, enter at the command prompt :-

ECHO %DATE_FORMAT%

The system date format string displayed is system dependent and may vary from system to system.

Environment Variable Modifiers

Environment variable modifiers allow for the substitution of characters or the extraction of a range of characters from the environment variable specified. The modifiers have the following syntax :-

%var:~idx,num%Return num characters starting from index idx for the environment variable specified. The first character in the environment variable starts from index 0. If idx is less then zero, then start from the end of the environment variable, where -1 is the last character in the environment variable and so on. If num is less then zero, count the number of character to return from the end of the string.
%var:~idx%Return all the characters starting from index idx for the environment variable specified. The first character in the environment variable starts from index 0. If idx is less then zero, then start from the end of the environment variable, where -1 is the last character in the environment variable and so on.
%var:str1=str2%Replace all occurrences of str1 with str2 for the environment variable found. If str2 is an empty string then all occurrences of str1 are removed from the environment variable found.

For example, consider the environment variable abc as below :-

SET abc=abcdefghijklmnopqrstuvwxyz
Variable Result Description
%abc% abcdefghijklmnopqrstuvwxyz Return the whole environment variable.
%abc:~5,2% fg Return 2 characters starting from index 5 (ie. the 6th character) in the environment variable abc.
%abc:~5,-2% fghijklmnopqrstuvwx Return all the characters starting from index 5 until the 2nd last character in the environment variable abc.
%abc:~-5,2% vw Return 2 characters starting from the last 5 characters in the environment variable abc.
%abc:~-5,-2% vwx Return the characters starting from the last 5 characters until the 2nd last character in the environment variable abc.
%abc:~5% fghijklmnopqrstuvwxyz Return all the characters starting from index 5 (ie. the 6th character) in the environment variable abc.
%abc:~-5% vwxyz Return all the characters starting from the last 5 characters in the environment variable abc.
%abc:ab=01 01cdefghijklmnopqrstuvwxyz Replace all the occurrences of ab with 01 in the environment variable abc.
%abc:Ab=01% 01cdefghijklmnopqrstuvwxyz The string replacements are case insensitive. This is the same as %abc:ab=01%.