MS-DOS Batches

download ms_dos_batches.zip (10 kb)
 

Advantages of using batch files
Batch file commands
Creating a batch file
MSDOS Batch filenames
MSDOS Shell Variables
Creating and assigning values to shell variables
Displaying the environment table shell variables
Accessing shell variables within batch files
Using MSDOS batch file Parameters

Creating Menus using MSDOS batch files

Using MSDOS Batch Commands
The call command
The echo command
The for command
The goto command
The if command
The pause command
The rem command
The shift command

MSDOS Batch File Examples

Batch files contain a sequence of DOS commands which are executed in sequential order from a specified file in place of keystrokes from the keyboard. Batch files are identified with the .BAT extension. They are invoked from the command line in DOS by typing the filename, or may appear as commands in other batch files.

The sequence of commands contained in the batch file are executed sequentially in order and can be displayed on the screen.

 

[ top ]

 

Advantages of using batch files

Batch files speed up your work. By placing a sequence of commands in a batch file (for instance to backup the system data files), you only need to type one command.

Batch files enable you to create commands which perform desired tasks. You can create batch files to backup the system, customize the system, support several environments each different on a per user basis, etc.

Batch files are also used to create menu systems, making the computer easier to use for beginners by displaying menu options and acting upon user selections.

 

[ top ]

 

Batch file commands

Any command typed on the MSDOS command line can be contained in a batch file. MSDOS created a special mini-language for batch files (OS/2 has extended this to a macro language called REXX). There are eight batch commands,

Command

Effect

call

executes another batch program, then returns and continues
executing the remaining commands in the current batch file

echo

displays messages or enables / disables the echo feature

for

repeats a command for a group of files or directories

goto

jumps to a label at another point in the batch file

if

conditional execution of a batch file command

pause

halt the batch file, display a message, and continue when a key
is pressed

rem

used to insert comments into batch files

shift

allows you to change the position of parameters supplied to the
batch file

To prevent a single command from being displayed, preceed the command with the @ symbol.

 

[ top ]

 

Creating a batch file

A batch file must be saved in plain ASCII text. The editor provided with MSDOS (edlin or edit) saves files in this mode. In addition, the copy con command in MSDOS can also be used to create small batch files.

 

[ top ]

 

MSDOS Batch filenames

Batch files have the .BAT extension. They should not have the same name as existing MSDOS commands. When MSDOS looks for files, it runs files in .COM or .EXE first.

If you create a batch file called DISKCOPY.BAT, the file may not be executed by MSDOS as it could find the diskcopy.exe program first. Careful construction of the PATH command will be necessary if you intend to use batch files with names of existing MSDOS commands.

 

[ top ]

 

MSDOS Shell Variables

The command interpreter program command.com supports user and application defined variables. These are called shell variables and are held by MSDOS in an environment table. The size of this table can be abjusted using the shell= statement in a config.sys file. This has been covered in the OS100m1.ref handout/module.

 

[ top ]

 

Creating and assigning values to shell variables

Within a batch file, or at the command prompt, a shell variable is created using the set command. The following example sets the shell variable name to the text string "sue".

set name="sue"

 

[ top ]

 

Displaying the value of the environment table shell variables

To display the contents of the environment table, use the set command without any parameters.

D:\>set
COMSPEC=C:\DOS\COMMAND.COM
PROMPT=$p$g$E[33;44;1m
PATH=C:\WINDOWS;C:\DOS;C:\BIN;C:\XYWRITE;C:\XTREE;C:\TP;
TEMP=c:\windows\temp
windir=C:\WINDOWS
NAME="sue"

 

[ top ]

 

Accessing shell variables within batch files

Shell variables can be set and displayed within batch files. To set shell variables within a batch file, use the same syntax as that of the command line.

To display shell variables within a batch file, the shell variable is enclosed within two % symbols.

@echo off
REM sample batch file to set and display a shell variable
set display=vga
echo display type is set to %display%


The output of the above batch file is

D:\>setsam
display type is set to vga
D:\>

 

[ top ]

 

Using MSDOS batch file Parameters

Batch files can accept parameters to work with when they are run. The parameters are typed on the command line following the batch file name. In the example below, the batch file compile.bat is run with the parameters hello.c and B:\

compile hello.c B:\

MSDOS numbers each parameter using a % symbol. The name of the batch file is %0 (in the above case %0==compile.bat). The first parameter which follows the batch filename is known as %1, the second as %2 and so on.

Consider the following batch file sample.bat.

@echo off
REM sample.bat
copy %1 %2

This batch file, when invoked with the parameters hello.c and B:\ copies the file specified in the first parameter (hello.c) to the drive and directory specified as the second parameter (B:\).

Up to ten parameters (%0 - %9) are available to a batch file program. More than nine may be specified on a command line when invoking the batch file, and to access the remaining parameters above %9, the shift command must be used.

 

Using MSDOS Batch Commands in batch files

The following section explains the use of each of the batch file commands.

 

[ top ]

 

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

 

[ top ]

 

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:\>

 

[ top ]

 

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

 

[ top ]

 

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:\>

 

[ top ]

 

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

 

[ top ]

 

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.

 

[ top ]

 

The rem command

The rem command is used to insert comments into batch files.

 

[ top ]

 

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:\>

 

[ top ]

 

Creating Menus using MSDOS batch files

Batch files are often used to create menus. To handle keyboard input, a public domain program scankey has been used in the following examples. This program returns the keypress as an ASCII value into the errorlevel variable used by MSDOS.

The public domain program beep is used to sound the PC speaker when a user selection is invalid. For example, the ASCII code for 'A' is 65, thus if the user presses 'A' in response to the scankey program, the errorlevel variable is set to 65. Consider the following batch file program, which utilises a simple menu system to implement a menu system.


@echo off
REM simple menu system
CLS

:start
echo .
echo Bobs Machine Menu
echo ===============
echo .
echo 1 Run Windows
echo 2 Format disk in drive A
echo 3 Copy work files to floppy in drive A
echo 0 Exit menu system
echo .
echo Enter option (0-3)
scankey

if errorlevel 51 goto copyfiles
if errorlevel 50 goto formatdsk
if errorlevel 49 goto runwin
if errorlevel 48 goto exit
beep

goto start

:copyfiles
echo insert disk in drive A: for work files
pause
copy \work\*.* a:\
echo work files copied.
goto start

:formatdsk
format a:
goto start

:runwin
win
goto start

:exit

 

[ top ]

 

MSDOS Batch File Examples


not display commands as they are executed

echo off

 

to stop a single command from being displayed when executed

@echo off


to insert comments into batch files

rem display1


to display an argument passed to batch file

rem display the first argument
echo %1


but what if no argument is passed, so lets add a test to check argument and if present, display it


rem display and check if there is an argument
if "%1"=="" goto noparameter
echo the parameter is %1
goto exit

:noparameter
echo you must supply an argument to this batch file

:exit

 

display all names of files in current directory

@echo off
rem list files in the current directory
for %%f in ( *.*) do echo %%f

 

test to see if a file exists

@echo off
rem check to see if a file exists
if exist %1 goto fileexists
goto nofile

:fileexists
echo the file %1 exists
rem other commands here .....
goto end

:nofile
echo the file %1 does not exist
goto end

:end


check whether a batch file command executed okay

@echo off
rem this batch file runs the next command "format"
rem if this command was successful, it sets the MSDOS variable
rem ErrorLevel to 0, else if it fails, to a non-zero value

echo Insert new disk in drive A:
pause
format a:
if errorlevel 1 goto error
echo disk was formatted okay
goto end

:error
echo format was unsuccessful
goto end

:end


format a drive passed an argument to batch file

@echo off
rem this batch file runs the next command "format"
rem if this command was successful, it sets the MSDOS variable
rem ErrorLevel to 0, else if it fails, to a non-zero value
rem the drive to format is passed as the first argument

echo Insert new disk in drive %1
pause
format %1
if errorlevel 1 goto error
echo disk in drive %1 was formatted okay
goto end

:error
echo format of disk in drive %1 was unsuccessful
goto end

:end

 

format a drive passed an argument to batch file and check for the drive argument

@echo off
rem this batch file runs the next command "format"
rem if this command was successful, it sets the MSDOS variable
rem ErrorLevel to 0, else if it fails, to a non-zero value
rem the drive to format is passed as the first argument
rem and checks to make sure the drive argument is specified

if "%1"=="" goto nodrive
echo Insert new disk in drive %1
pause

format %1
if errorlevel 1 goto error
echo disk in drive %1 was formatted okay
goto end

:error
echo format of disk in drive %1 was unsuccessful
goto end

:nodrive
echo You must specify a drive to format
goto end

:end


format a drive passed an argument to batch file and check for the drive argument but dont format drive c:


@echo off
rem this batch file runs the next command "format"
rem if this command was successful, it sets the MSDOS variable
rem ErrorLevel to 0, else if it fails, to a non-zero value
rem the drive to format is passed as the first argument
rem and checks to make sure the drive argument is specified

if "%1"=="" goto nodrive
if "%1"=="C:" goto drivec
echo Insert new disk in drive %1
pause
format %1

if errorlevel 1 goto error
echo disk in drive %1 was formatted okay
goto end

:error
echo format of disk in drive %1 was unsuccessful
goto end

:nodrive
echo You must specify a drive to format
goto end

:drivec
echo You cannot format the hard disk
goto end

:end