Copyright B Brown, 1988-2000. All rights reserved.
Using MSDOS Batch Commands in
The following section explains the use of each of the batch file commands.
The call command
To run a batch program from within the current batch program, use the call command followed by the name of the batch program you wish to run. After the second program is finished, it will return to the command which follows the call command.
If you invoke a batch file without using the call command (from within a batch file that is), control passes over to the new batch program and does not return.
@echo off rem this is the main batch file, main.bat echo Calling second call second echo Returned from second @echo off rem this is second.bat echo Running second batch file
When the above batch file main.bat is executed, the call statement passes execution to the batch file second.bat. After this batch file has finished, control is returned to main.bat, and execution continues with the statement following the call command. The resultant output is,
Calling second Running second batch file Returned from second
The echo command
The echo command is used to display messages or enable/disables the echo feature. When the echo command is enabled, subsequent batch file commands are displayed on the screen as they are executed. This provides a means of testing batch files for correct execution.
Consider the following batch file program, which illustrates the usage of the echo command.
@echo off echo This is a sample batch file for echo commands set name=sue echo The name is %name% echo on set name=bruce echo The name is %name%
When the batch file is run, the resultant output is
This is a sample batch file for echo commands The name is sue D:\>set name=bruce D:\>echo The name is bruce The name is bruce D:\>
The for command
The for command repeats a command for a group of files or directories. Its syntax is,
FOR %%variable IN (set) DO command
variable is a single character (a-z, or 0-9). set can include file and pathnames, as well as the global filename characters * or ?. command is a valid MSDOS command or batch command.
The following example prints all files with the extension .DOC on the printer connected to LPT1.
for %%f in (*.doc) do print %%f
The following example types the three .C files (test1.c, sample.c, work.c) to the screen display.
for %%i in ( test1 sample work ) do type %%i.c
The goto command
The goto command jumps to a label at another point in the batch file. Labels in batch files are preceeded using a : symbol. Consider the following example.
@echo off set display=vga goto end set display=mono :end echo display type=%display%
When the batch file is executed, the resultant display is,
D:\>setdisp display type=vga D:\>
The if command
The if command is used for conditional execution of a batch file command.
IF ERRORLEVEL number example
MSDOS applications and commands return a value which is copied into the errorlevel variable on return to MSDOS. If this has a value greater than 1 it indicates an error has occurred. The following batch file uses the errorlevel value returned by the MSDOS command diskcopy to check whether the command executed successfully.
REM using the errorlevel indicator returned from an MSDOS application diskcopy a: b: if errorlevel 1 goto error echo disk copied goto end :error echo diskcopy has failed :end
IF string1==string2 example
The parameter being tested and the text it is being compared to must be enclosed in quotation marks ("), and must match for the command to be executed.
The following batch file tests for the shell variable display. If this is defined as "vga", it invokes lotus 1-2-3 using the display driver "vga.set".
If the display type is "mono", it invokes lotus 1-2-3 using the display driver "mono.set". If the display type is defined as something other than 'vga' or 'mono', or is not defined, the message "display type is not supported" is displayed.
REM testing for string equality IF "%display%"=="vga" goto lotusvga IF "%display%"=="mono" goto lotusmono echo display type is not supported goto end :lotusvga 123 vga.set goto end :lotusmono 123 mono.set goto end :end
IF exist filename example
This format is used to test for the existance of a file. The following example tests for the existance of the file "c:\work\data.dat", and if it exists, copies it to drive A:\
rem testing to see if a file exists if exist c:\work\data.dat goto filefound echo file does not exist goto end :filefound copy c:\work\data.dat a:\ :end
IF NOT example
The NOT command reverses the truth of the condition from true to false or from false to true. In the following batch file, the shell variable has been set to "vga". The first conditional test evaluates as TRUE ("vga"=="vga"), but the NOT command inverts this to FALSE and the command does not succeed.
The second conditional test evaluates as FALSE ("vga"=="mono"), but the NOT command inverts this to true and the command succeeds.
rem inverting the conditional statements using NOT set display=vga if NOT "%display%"=="vga" goto message1 if NOT "%display%"=="mono" goto message2 goto end :message1 echo display type is vga goto end :message2 echo display type is NOT vga goto end :end
The pause command
The pause command halts the batch file, displays the message
Press any key to continue.....
and continues when a key is pressed.
The rem command
The rem command is used to insert comments into batch files.
The shift command
The shift command allows you to change the position of parameters supplied to the batch file. Batch files are limited to handling 10 parameters (%0 - %9). Using the shift command, each parameter is shifted one position to the left, so that parameter %1 becomes %0, parameter %2 becomes %1 and so on. Each shift command causes all parameters to be shifted to the left by one position.
Once shift is executed, the %0 parameter that existed prior to the shift is no longer available.
The following batch file (shift1.bat) echoes each argument supplied to the batch file.
@echo off REM sample batch file to echo all parameters on command line using shift :LOOP if "%1"=="" goto DONE echo %1 shift goto loop :done
Below is a sample output of running the previous batch file.
C:\>shift1 ten this and that hello sam ten this and that hello sam C:\>