Common Batch File Commands

Common Batch File Commands  Common Batch File Commands contains a list of common batch file commands with examples of the command use. Command  @ Des...
Author: Lucas Booth
0 downloads 1 Views 181KB Size
Common Batch File Commands  Common Batch File Commands contains a list of common batch file commands with examples of the command use.

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. 

Example 

echo This line will be echoed twice to the screen,  @echo Whereas this line will occur only once. 

Command  ECHO 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  Description  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

The command @ECHO OFF is almost always placed at the top of a batch file to  Typical Use  switch off subsequent command echo.  ECHO is also the only way a batch file can communicate information to a user. Example 

@ECHO OFF ECHO Please insert a disk in drive A: and press any key when ready.

Common Batch File Commands.doc 

Page 1 

11/16/2014 

Common Batch File Commands  Command  REM (short for remark) 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  Description  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.

REM should be used at the top of every batch file to provide a description and  example use.   Typical Use  However, too many REM lines are not an example of good programming style!  Don't provide a comment for obvious commands ‐ only the complex routines

Example 

REM SFORMAT.BAT : Safe Format REM REM This batch file implements a safe version of the format command. REM The C: drive can not be formatted with this command.

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!)

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  Typical Use  the ECHO command, the PAUSE command waited for the user to read them and  respond appropriately. ECHO Please insert the disk in drive A: and Example  PAUSE

Common Batch File Commands.doc 

Page 2 

11/16/2014 

Common Batch File Commands  Command  GOTO 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  Description  placed after the GOTO command. This label must conform to several guidelines for  it to be a valid batch file label. Syntax 

GOTO label

Until MS DOS 6.x introduced the FOR command, the GOTO command was a batch  files only mechanism of performing a command repeatedly. GOTOs are still the  Typical Use  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

Common Batch File Commands.doc 

Page 3 

11/16/2014 

Common Batch File Commands  Command  IF 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  Description  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 existence 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.

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  Typical Use  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. IF NOT EXIST %1 MKDIR %1 Example 

IF ERRORLEVEL 2 GOTO END  IF %1 == "" GOTO ERROR

Common Batch File Commands.doc 

Page 4 

11/16/2014 

Common Batch File Commands  Command  SHIFT 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 accessible by a batch file. Each time SHIFT is called, the value in the  Description  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

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  Typical Use  often used in situations where an operation needs to be performed on several files  or directories. The following example displays the contents of the files typed after the batch file  name one page at a time.  Example 

:LOOP TYPE %1 | MORE SHIFT IF "%1" == "" GOTO END GOTO LOOP :END

Command  CALL 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  Description  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

Common Batch File Commands.doc 

Page 5 

11/16/2014 

Common Batch File Commands  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 ‐:   variable is substituted for each element in the list and passed to  command. Variable has a special format in batch files.   list is a list of filenames (wildcards allowed), directory names, or text‐ strings that are to be processed by command one at a time.    command is a DOS internal or external command to be performed for each  element of the list. 

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 GOTOs and IFs, which  Typical Use  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.  FOR %%f IN (*.*) DO TYPE %%f | MORE

Common Batch File Commands.doc 

Page 6 

11/16/2014 

Common Batch File Commands  Command  CHOICE The CHOICE command is perhaps the best addition to MS DOS Batch File  commands. CHOICE makes it possible to accept various user‐responses. Before  Description  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 list of  options.

Syntax 

CHOICE [/C:choices] [/N] [/S] [/T:choice,timeout] [TEXT] Where ‐:   /C:choices : specifies the choices that the user can choose from. The  choices can only be single characters.    /N : Do not display choices and the '?' at the end of the TEXT prompt.    /S : Treat the choices as case sensitive, meaning that 'a' is a different  choice from 'A'. By default, case is not sensitive ‐ 'a' is equivalent to 'A'.    /T:choice,timeout : Default to choice after timeout seconds.    TEXT : The text to display as the prompt of the choice. 

The CHOICE command has its obvious use in batch files. It is now possible to easily  Typical Use  get a users response, thus allowing batch files to be much more interactive, and  therefore more useful. 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 : 

Example 

ECHO 1. MS-DOS Editor. ECHO 2. MS-Windows. (default) ECHO 3. Defrag the hard-drive. ECHO 4. Quit. CHOICE /C:1234 /N /T:2,5 Please choose a menu option. IF ERRORLEVEL == 4 GOTO QUIT_MENU IF ERRORLEVEL == 3 GOTO DEFRAG_HD IF ERRORLEVEL == 2 GOTO RUN_WIN IF ERRORLEVEL == 1 GOTO RUN_EDIT :RUN_EDIT CALL EDIT :RUN_WIN CALL WIN :DEFRAG_HD DEFRAG c: :QUIT_MENU ECHO Safe to switch off machine now...

Common Batch File Commands.doc 

Page 7 

11/16/2014