Command | @ |
Description | Do not echo this line. |
Syntax | @ command line |
Typical Use | To hide a single line if echo is switched on, or to hide the switching off of the echo command. |
Examlpe | echo This line will be echoed twice to the screen, @echo Whereas this line will occur only once. |
Command | ECHO |
Description | The ECHO command has several different uses. MS DOS batch files use two echo 'modes'. The default echo mode is ECHO ON . When ECHO is set to ON ,
every command in the batch file is displayed to the screen before it is
run. Sometimes this information is not required, and can even be
downright annoying for larger batch files. The command ECHO OFF sets the batch echo mode to OFF . In this mode, the commands are not printed to the screen prior to their execution.As well as the echo modes, the ECHO command is used to print a message to the user. Messages are displayed to the user by preceding a line of text with ECHO . |
Syntax | ECHO MODE ON : ECHO ON ECHO MODE OFF : ECHO OFF DISPLAY A MESSAGE : ECHO message |
Typical Use | The command @ECHO OFF is almost always placed at the top of a batch file to switch off subsequent command echo.ECHO is also the only way a batch file can communicate information to a user. |
Example | @ECHO OFF |
Command | REM (short for remark) |
Description | REM is the MS DOS batch file method of providing comments. Comments are lines of code which are not
executed by the batch file, but rather are used to convey information
about the workings of the batch file itself. Good batch file
programming practice demands a comment at the head of every batch file
explaining its use and syntax. Comments can also be put in other parts
of the file to clarify ambiguous commands and to 'comment-out' a line
of commands so that they are temporarily ignored by the batch file. |
Syntax | REM line containing comment. |
Typical Use | REM should be used at the top of every batch file to provide a description and example use. However, too many REM lines are not an example of good programming style! Don't provide a comment for obvious commands - only the tricky ones! |
Example | REM SFORMAT.BAT : Safe Format |
Command | PAUSE |
Description | The PAUSE command prints the message "Press any key to continue..." to the screen and waits for the user to respond. |
Syntax | PAUSE (it's as simple as that!) |
Typical Use | The PAUSE command was
the only method of getting a user's response in batch files until the
choice command arrived in MS DOS 6.x. By issuing instructions with the ECHO command, the PAUSE command waited for the user to read them and respond appropriately. |
Example | ECHO Please insert the disk in drive A: and |
Command | GOTO |
Description | The GOTO command allows a batch file to branch to a different location to continue executing commands from. To tell the batch file where to go to, a label is placed after the GOTO command. This label must conform to several guidelines for it to be a valid batch file label. |
Syntax | GOTO label |
Typical Use | Until MS DOS 6.x introduced the FOR command, the GOTO command was a batch files only mechanism of performing a command repeatedly. GOTO s are still the only method in a batch file to perform a sub-set of commands. (MS DOS Batch files do not have sub-procedures) |
Example | IF %1 == "" GOTO ERROR |
Command | IF |
Description | The IF command is used
in batch files to test whether a condition is met or not. This allows
the batch file to perform a particular action only if a particular
condition is met. There are several different variations of the IF command: IF EXIST , IF ERRORLEVEL , and IF x == y (yes! it does use two equal signs!) |
Syntax | IF EXIST filename or dirname : used to test for the existance of a file or directory in MS DOS. This test will return true if the file does exist.IF ERRORLEVEL
: After a program has finished executing in MS DOS it returns a value
to the operating system indicating its success or failure. This value
is stored in the variable ERRORLEVEL . By testing this variable, a batch file can deduce the result of the program that just finished running.IF x == y : This version of the IF statement tests two string values. If string x is equal to string y this test is evaluated to be true, otherwise false.All of the above IF statements can also be negated with the NOT command. For example -:IF NOT EXIST filename : Tests to see if the file doesn't exist. This test will return true if the file doesn't exist. |
Typical Use | The IF statement is one of the most useful batch file commands, and as such is probably the most common. The IF EXIST command is used to check if a file exists before it is copied/moved/opened/etc. The IF ERRORLEVEL allows a batch file to check the return value of another program. The IF STRING1 == STRING2 is commonly used to validate command-line parameters. |
Example | IF NOT EXIST %1 MKDIR %1 IF ERRORLEVEL 2 GOTO END IF %1 == "" GOTO ERROR |
Command | SHIFT |
Description | The SHIFT command is possibly, at first, the most confusing batch file command. It needn't be. Simply, the SHIFT command increases the number of command-line parameters accessable by a batch file. Each time SHIFT is called, the value in the 1st parameter is discarded and replaced by the value of the 2nd parameter. The value in the 2nd parameter is replaced by the value in the 3rd parameter, etcetera, etcetera, until the 9th parameter is replaced by the previously unavailable 10th parameter. |
Syntax | SHIFT |
Typical Use | The SHIFT command provides considerable power to batch files. It allows a batch file to operate on an unknown number of parameters. The SHIFT command is often used in situations where an operation needs to be performed on several files or directories. |
Example | The following example displays the contents of the files typed after the batch file name one page at a time.
|
Command | CALL |
Description | The CALL command is
used to run another batch file from within a batch file. Execution of
the current batch file is paused and the called batch file is run.
After the called batch file has finished running, the original batch
file is resumed at the line after the CALL statement.Note: If another batch file is run from within a batch file by simply using its name, after the called batch file finishes executing, control is returned to the Command Line, NOT the original batch file. |
Syntax | CALL batchfilename [parameters] [switches] |
Typical Use | The CALL command is
used to provide modularity to batch files. Batch files can be re-used
effortlessly if they are written with modularity in mind. |
Example | IF %1 == A: CALL FLOPPY.BAT |
Command | FOR |
Description | The FOR command was an invaluable addition to the DOS Batch File Command suite. FOR repeats a command for a number of files, directories, or text-strings. |
Syntax | FOR variable IN list DO command [parameters] [switches] Where -:
|
Typical Use | The FOR command
performs the same command for each element of a list. Prior to its
introduction, the same effect had to be achieved with GOTO s and IF s, which were messy and sometimes difficult to follow. Use a FOR to do any necessary looping in your batch files. |
Example | The following is an implementation of the same example presented in the SHIFT example of displaying many files to the screen with MORE .
A lot neater, huh?! |
Command | CHOICE |
Description | The CHOICE command is perhaps the best addition to MS DOS Batch File commands. CHOICE
makes it possible to accept various user-responses. Before now, users
were presented with crude either/or choices in batch files. The CHOICE command allows a batch file to detect a users choice from a lits of options. |
Syntax | CHOICE [/C:choices] [/N] [/S] [/T:choice,timeout] [TEXT] Where -:
|
Typical Use | The CHOICE command has
its obvious use in batch files. It is now possible to easily get a
users response, thus allowing batch files to be much more interactive,
and therefore more useful. |
Example | The following batch file snippet
displays a simple menu (without a question-mark at the end of the
prompt) and prompts for the users choice, defaulting to option 2 after
5 seconds :
|