\
Skip Navigation LinksHome > Coding > Scripting > MS-DOS > Special Variables\
 
 
[]UDF / UDC Library
[]Script Library



 

There are three categories of variables available to you within a batch file. Those variables are:

Command Line Variables

Command line arguments are the strings following the program name in the command that invokes the program. In batch files they are read- only and are referred to by an escaped digit ('%' is the escape character in batch files). %0 through %9 are readily available, and any beyond %9 can be read by SHIFTing them down into the single digit range. For each SHIFT, %0 disappears, and each argument takes on the value of the one to its right on the command line. Once shifted out of the %0 - %9 range, an argument cannot be recovered. %0 always exists (initially) and is the name of the batch file as given in the command. If given in short form, it is just the name; if given in long form, the path that was used precedes the name.

Command line arguments can be used to pass any string that doesn't contain a delimiter into the batch file. When a delimiter is encountered, the argument is split into two arguments, and all information about the nature of the delimiter is lost (you have no way of knowing if it was a space, a a tab, a comma, a semicolon, or an equals sign) - quotes don't help: the opening quote mark is part of the earliest argument and the closing quote mark is part of the last one in the string.

Example:
Save script as: C:\CLVars.BAT
Call script as: C:\CLVARS.BAT ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN

@ECHO OFF
ECHO Command line variables passed:
ECHO.
ECHO %%0 = %0
ECHO %%1 = %1
ECHO %%2 = %2
ECHO %%3 = %3
ECHO %%4 = %4
ECHO %%5 = %5
ECHO %%6 = %6
ECHO %%7 = %7
ECHO %%8 = %8
ECHO %%9 = %9

ECHO.
ECHO.

ECHO Accessing variables beyonD position nine:
SHIFT
ECHO %%9 now contains = %9
PAUSE

Environmental Variables

Environment variables are strings associated with a variable's name. Nearly all characters (including delimiters, even spaces) except the '=' sign are permitted in both the name and value. These variables reside in the current environment area of memory, and changes to the environment are accessible to child programs, but not to parent programs. One string is set equal to another by means of the SET command, and the value is read by placing the name between a pair of '%' characters.

One common problem with these variables is due to the inclusion of unwanted spaces. This is a byproduct of the ability to include spaces anywhere in the strings on both sides of the '=' sign - the name ends at the '=' sign and the value begins immediately after it. The usual remedy for this sort of thing is the use of quoted strings, but quoted strings exist in only a few places in batch file syntax (notably in remarks, where they are meaningless, and in strings to be ECHOed) - everywhere else, the quote marks are simply additional characters. These are the only kinds of variables to which the user can assign a constant value or have carry over from batch file to other programs. They are frequently used inside batch files as scratch pad memory.

All environmental variables set from within DOS are always uppercase. Environmental variables set by Windows are in mixed case.

Example:
Save script as: C:\EVars.BAT

@ECHO OFF
ECHO Environmental Variables Set by Windows:
ECHO (varies depending on OS)
ECHO.
ECHO %%WinDir%% = %WinDir%
ECHO %%PATH%% = %PATH%
ECHO %%OS%% = %OS%
ECHO.
ECHO Setting the variable 'NAME'
SET name=Dx21
ECHO %%NAME%% = %NAME%
ECHO Clearing the variable 'NAME'
SET name=
ECHO %%NAME%% = %NAME%
PAUSE

Special Variables

Every batch file has a special environmental variable assigned upon startup. This variable is accessed internally through %0, which returns the full path and name of itself.

This variable is useful because DOS batch files are not aware of where they are. With this variable, you can have your batch file dynamically determine it’s own location, and pass the path off to another script or look for other files within the same directory or sub-directories by using a special syntax: %0\..\

Using this notation, your script file can call another script file in the same or deeper location, without the environment needing to have the initial location in the PATH.

MS-DOS 3.3 to 5.0 Example:
Save script as: C:\SVars.BAT

@ECHO OFF
ECHO Using redirection to set a variable
ECHO. | TIME | FIND "Current" > %0\..\CURTM.BAT
ECHO SET NOW=%%3 > %0\..\CURRENT.BAT
CALL %0\..\CURTM.BAT
DEL %0\..\CURTM.BAT
DEL %0\..\CURRENT.BAT
ECHO The Current Time Is: %NOW%
PAUSE

NT/2K/XP Console Example:
Save script as: C:\SVars.BAT

@ECHO OFF
ECHO Using redirection to set a variable
ECHO. | TIME | FIND "current" > %0\..\CURTM.BAT
ECHO SET NOW=%%4 > %0\..\THE.BAT
CALL %0\..\CURTM.BAT
DEL %0\..\CURTM.BAT
DEL %0\..\THE.BAT
ECHO The Current Time Is: %NOW%
REM PAUSE

Explaination of script flow and results:

Number the lines starting with #1

#3 - when you enter this command on the command line, you will get a two line return, the first line (in the XP Console case) says "The current time is: ##:##:##.##" Using the redirector | the Find command is instructed to find the word "current" within the entire output of the Time command, and write the line that the word "current" appears in to a file called CURTM.BAT. The Find command is case sensitive. This line will output the entire line, "The current time is: ##:##:##.##", to the file specified.
#4 - This will output the command to create an environmental variable to a second file, that in our new script, is called THE.BAT. We've named the new batch file the first word in the line outputted in the redirection of line #3.
#5 - Next, we call our first output. This file contains the output of the Find command, or in this case "The current time is ##:##:##.##". When we call this script file, that batch file will dump all content to the command line. "The" isn't a DOS command, but it is the name of the second output file we created. So, in this instance, we are calling the BAT file NOW, with the command line parameters "current", "time", "is", and "##:##:##.##". The NOW batch file then takes the command line parameter in the fourth position, or "##:##:##.##", and puts it into the newly created variable NOW.
#6, #7 - just clean up
#8 - This will display the information contained in the NOW variable.

Help Support Dx21
Buy Thru Us

 FAQs  |  Terms Of Use  |  Privacy Policy  |  Contact Us
Copyright © 1997 - 2010 Dx21, LLC. All rights reserved.
Dx21, LLC a Washington Limited Liability Company
Page Rendered at: 9/2/2010 8:17:03 PM for Unknown