MS-DOS Commands Batch Files
(courtesy B. Vachon)

If an operation or set of operations is performed repeatedly from the DOS prompt, the command or set of commands can be stored in a special type of file called a batch file. You could then execute the batch file as you would a program file.

A batch file or batch program is a user defined ASCII (text) file that contains one or more MS-DOS commands. It is assigned a .BAT extension. When you type the name of the batch program at the command prompt, the commands are carried out as a group.

Any MS-DOS command you use at the command prompt can also be put in a batch program.

 

Batch File Commands

The following MS-DOS commands are specially designed for batch programs:

Echo - Displays or hides the text in batch programs when the program is running. When you run a batch program, MS-DOS typically displays (echoes) the batch program's commands on the screen. i.e.,

Command

Will Display

ECHO HI THERE

HI THERE

ECHO. (no space)

(A Blank Line) (Good way of spacing a message)

ECHO OFF

(Will not display any commands)

@ECHO OFF

(Same as above but will not display this line either)

ECHO ON

(Will display any commands. Default setting)

 

Pause - Suspends processing of a batch program and displays a message that prompts the user to "Press any key to continue . . .".

 

Rem - Enables you to include comments in a batch file or in your CONFIG.SYS file. The REM command is also useful for disabling commands.

 

If - Performs conditional processing in batch programs. If the condition specified by an IF command is true, MS-DOS carries out the command that follows the condition. If the condition is false, MS-DOS ignores the command. You can use this command only in batch programs.

Other Advanced Commands

Call - Calls a batch file from another without causing the first one to stop.

Choice - Prompts the user to make a choice in a batch program. Displays a specified prompt and pauses for the user to choose from among a specified set of keys. You can use this command only in batch programs.

Goto - Directs MS-DOS to a line in a batch file that is marked by a label you specify. You can use this command only in batch programs. The GOTO command directs MS-DOS within a batch program to a line identified by a label. When MS-DOS finds the label, it processes the commands beginning on the next line.

Shift - Changes the position of replaceable parameters in a batch program.

 

 

Creating a Batch File

There are four ways of creating batch files. These are:

COPY CON Copy from the Console. This is a great way to create a quick batch file. It’s fast and easy to use. The drawback is that you cannot correct mistakes on previous lines after you have pressed ENTER.

EDLIN Line Editor. Is the predecessor to the Edit command. Quirky and hard to work with. Has become extinct (Thank God!).

EDIT Starts MS-DOS Editor. This is a preferred method. MICROSOFT-DOS Editor is a full screen text editor you can use to create and edit ASCII text files. It allows you to create, edit, save, and print ASCII text files.

WORD PROCESSOR

You can use a word processor and save as an ASCII file. The advantage is that you have a very powerful text editing and in some cases line drawing program. The disadvantage is that it’s a bit of a pain to get into and save as.

 

 

 

To stop a batch file

To stop a batch file while it is executing, you could press CTRL+C or CTRL-Break. DOS will then ask you if you want to terminate the batch file.

The Autoexec.bat Batch File

Instead of having to type in the same commands every time you start your computer, DOS provides us with the opportunity to create a special batch file, which automatically executes every time the system boots up. This batch file is called AUTOEXEC.BAT.

AUTOEXEC.BAT can contain various commands like setting the path command, system prompt and starting application programs or menu utilities.

In order for it to work, it must reside in the root directory of your systems boot disk, which is usually your hard disk (C:\).

Some AUTOEXEC.BAT files are pretty complex; however, the following is a simple example of one.

 

AUTOEXEC.BAT (simple)

@ECHO OFF

PROMPT $P$G

PATH C:\;C:\DOS\;C:\WINDOWS\

CLS

WIN

 

 

Batch File Examples

STARTUP.BAT

@ECHO OFF

REM This batch file is a simple example of a batch

REM file which doesn’t do much.

CLS

TYPE INTRO.TXT

DIR A:/W

CHKDSK A:

GO1.BAT

@ECHO OFF

REM This batch file will copy the contents of the

REM directory TEMP from drive C: to the root directory

REM of the disk in drive A: including subdirectories.

CLS

ECHO PUT A DISK IN DRIVE A: AND PRESS ENTER WHEN READY

PAUSE

XCOPY C:\TEMP\. A:\ /S

GO1

Note: Notice that the last line of the batch file is GO1, which means that the batch file would be in an endless loop (would keep repeating forever). To stop the execution, the user would have to press CTRL-C or CTRL-Break.

 

FMT-1.BAT

@ECHO OFF

REM THIS BATCH PROGRAM FORMATS AND CHECKS NEW DISKS.

CLS

ECHO **** INSERT NEW DISK IN DRIVE A: ****

PAUSE

FORMAT A: /S

CHKDSK A:

Note: A problem with this batch file is that it always formats drive A:. There is a method where we could make the batch file interactive; that is, the user can specify which drive to format.

Interactive Batch File Examples

 

The following is an example of an interactive batch file. The user can specify which drive to format and check. For example, if the user were to type in FMT-1 A:

FMT-1.BAT

@ECHO OFF

REM THIS BATCH PROGRAM FORMATS AND CHECKS NEW DISKS.

ECHO **** INSERT NEW DISK IN DRIVE %1 ****

PAUSE

FORMAT %1 /S

CHKDSK %1

Note: Notice that A: has been replaced by %1. When the batch file is executed, whatever they typed in after FMT-1 command would replace the %1. This is called a replaceable parameter.

 

Another example of an interactive batch file.

DIRS.BAT

@ECHO OFF

REM This batch file will print the directory structure

REM of a specified disk.

CLS

ECHO.

ECHO.

ECHO.

ECHO *** Be sure the printer is operational ***

ECHO.

ECHO.

ECHO.

PAUSE

CLS

%1

CD \

DIR /S > LPT1:

If the user were to type in:

DIRS A: Print out the Directory structure for the disk in drive A:

DIRS C: Print out the Directory structure for the disk in drive C:

DIRS D: Print out the Directory structure for the disk in drive D:

Interactive Batch File Examples

SAFE-D.BAT

@ECHO OFF

REM This batch file will display the file(s) before

REM deleting them. Provides option of cancelling.

CLS

ECHO *** WILL ERASE THE FOLLOWING FILES ***

DIR %1

ECHO.

ECHO Press CTRL-BREAK to cancel or

PAUSE

DEL %1

If the user were to type in:

SAFE-D A: Displays the files on drive A:, pauses, then deletes them

SAFE-D B: Displays the files on drive B:, pauses, then deletes them

 

Two replaceable parameters

We could also specify more that one replaceable parameter. For example:

LONGDIR.BAT

@ECHO OFF

REM This batch file will display up to 43 lines of

REM text on the screen for the specified drives.

CLS

MODE CON LINES=43

DIR %1 /P

PAUSE

DIR %2 /P

PAUSE

MODE CON LINES=25

 

If the user were to type in:

LONGDIR A: D: Would display the directory of the disk in drive A:, pause, and then display the directory of the disk in drive D: The A: replaces the %1 and the second item entered, D:, replaces the %2.

Conditional Batch File Examples

We could also create batch files to test for conditions by using the IF statement. These are called Conditional Batch files.

If a condition is true, the command in the line with the IF statement would be executed. If the condition is not true, the batch file would ignore the command in the line with the IF statement and continue to the next sequential command.

For example:

HELLO.BAT

@ECHO OFF

REM This batch file will test whether the condition

REM is true or not.

CLS

IF "%1"== "BOB" ECHO Hello Master %1!

IF NOT "%1"== "BOB" ECHO Who are you?

 

If the user were to type in:

HELLO BOB The screen would clear and display "Hello Master BOB!"

HELLO MICHELE The screen would clear and display "Who are you?"

HELLO Bob The screen would clear and display "Who are you?"

In the last example, notice that although we typed in Bob it still did not work because the text is case sensitive. To make it more bullet proof, we would have to include other IF statements which checks for all conditions of Bob. For example:

 

HELLO.BAT

@ECHO OFF

REM This batch file will test whether the condition

REM is true or not.

CLS

IF "%1"== "BOB" ECHO Hello Master %1!

IF "%1"== "Bob" ECHO Hello Master %1!

IF "%1"== "bob" ECHO Hello Master %1!

IF NOT "%1"== "BOB" ECHO Who are you?

IF NOT "%1"== "Bob" ECHO Who are you?

IF NOT "%1"== "bob" ECHO Who are you?

Conditional Batch File Examples

In the following examples we will test to see if a file already exists on a drive.

BAKUP-A.BAT

@ECHO OFF

REM This batch file copies a file from the current

REM directory to Drive A: %1 is the name of the file.

CLS

ECHO To copy the file %1 to diskette, put a disk in

ECHO drive A: and ...

PAUSE

COPY %1 A:

 

If the user were to type in:

BAKUP-A Letter.txt This batch file would copy Letter.txt from the default directory to drive A:.

A problem with this batch file is that the specified file would be copied to drive A; regardless if another file with the same name exists already. To make it safer, we could include a statement to check to see if the file exists on the disk in drive A:.

BAKUP-B.BAT

@ECHO OFF

REM This batch file copies a file from the current

REM directory to Drive A: %1 is the name of the file.

CLS

ECHO To copy the file %1 to diskette, put a disk in

ECHO drive A: and ...

PAUSE

IF NOT EXIST A:%1 GOTO CONTINUE

ECHO *** WARNING!!! The file %1 already exists! ***

ECHO *** Press CTRL-Break or CTRL-C to cancel, or ***

PAUSE

:CONTINUE

COPY %1 A:

 

Note: The IF statement IF NOT EXIST A:%1 GOTO CONTINUE This line checks to see if the specified filename already exists. If it doesn’t, then skip down to a label called CONTINUE. If it does, display a message warning the user.

 

Conditional Batch File Examples

In the following examples we will test to see if the user typed in an additional parameter.

 

BAKUP-C.BAT

@ECHO OFF

REM This batch file copies a file from the current

REM directory to Drive A: %1 is the name of the file.

CLS

ECHO ** BACKUP PROCEDURE **

ECHO.

REM Tests condition. If yes goto MESSAGE, else continue

IF "%1"== "" GOTO MESSAGE

COPY %1 A:

GOTO END

:MESSAGE

ECHO Error – You may have forgotten to enter a filename

ECHO after the command.

:END

ECHO Thanks for your co-operation.

 

Note: Instead of using: IF "%1"== "" GOTO MESSAGE

We could have used: IF %1.==. GOTO MESSAGE

 

Conditional Batch File Examples

We could also combine test conditions to create a pretty sophisticated batch file which would test to see if the user entered a parameter after the command and to test to see if the specified file already exists on drive A:

 

BAKUP-D.BAT

@ECHO OFF

CLS

ECHO ** BACKUP PROCEDURE **

ECHO.

REM Checks to see if the user entered a parameter

REM If yes goto MESSAGE1, else continue

IF "%1"== "" GOTO MESSAGE1

REM Checks to see if the file doesn’t already exist

REM If it doesn’t exist, goto MESSAGE2, else continue

IF NOT EXIST A:%1 GOTO MESSAGE2

REM If the file already exists, it will end the batch

ECHO *** WARNING!!! The file %1 already exists! ***

PAUSE

GOTO END

:MESSAGE1

ECHO Error – You may have forgotten to enter a filename

ECHO after the command.

GOTO END

:MESSAGE2

COPY %1 A:

GOTO END

:END

ECHO Thanks for your co-operation.

 

BAD Batch File

The following is an example of a bad bath file. The intent is to provide students with the opportunity of troubleshooting and fixing a bad batch file.

 

Do the following:

a. On a blank disk in drive A: create a directory called \TEST and another called \DATA.

b. Create two files in the TEST directory and call them LETTER1.TXT and LETTER2.TXT. The contents of these files are not important.

c. Copy these two files to the \DATA directory.

d. Enter the following batch file as you see it.

 

BAD.BAT

@ECHO OFF

REM This is a Bad (as in no good) Batch File.

REM *** Section 1 ***

REM Copies files from the TEST directory to the ROOT

CD \TEST

COPY LET*.*

CD \

REM *** Section 2 ***

REM Removes the subdirectory called DATA

RD \DATA

REM *** Section 3 ***

REM Reminds user to have a disk in drive A: and pauses

ECHO Please make sure that you have a disk in A:\> and

PAUSE

REM *** Section 4 ***

REM Enters a DOSKEY command called BAD

BAD = DIR A:\ /S

 

e. Now run the macro.

f. You will notice that the batch file has many errors and keeps on looping. Find the four errors made to this file and explain what DOS is attempting to do.

BAD Batch File - Solution

As mentioned, BAD.BAT has many errors. The following is a solution to BAD.BAT.

BAD.BAT (fixed up)

@ECHO OFF

REM This is a Bad (as in GOOD) Batch File.

REM *** Section 1 ***

REM Copies files from the TEST directory to the ROOT

CD \TEST

COPY LET*.* \

CD \

REM *** Section 2 ***

REM Removes the subdirectory called DATA

DEL \DATA\*.*

RD \DATA

REM *** Section 3 ***

REM Reminds user to have a disk in drive A: and pauses

ECHO Please make sure that you have a disk in A: and

PAUSE

REM *** Section 4 ***

REM Enters a DOSKEY command called BAD

DOSKEY BAD = DIR A:\ /S

Section 1:

The problem in Section 1 is that we had not specified where to copy the files to. The solution is to add the destination such as: \ or A:\, to the COPY command.

Section 2:

The problem in Section 2 is that the directory is not empty (the files Letter1.txt and Letter2.txt are in \DATA). By adding the command: DEL \DATA\*.* before the RD line, the DATA directory could then be removed. (Note: the batch file would stop to ask if you are sure that you want to delete all of the files)

Section 3:

Although not immediately noticeable, the problem with Section 3 is that the line "ECHO Please make sure that you have a disk in A:\> and" wasn’t being displayed on screen. The reason is because of the operator > in the line, It was redirecting the first part of the line into a file called AND. By removing the > operator, the line would display on screen.

Section 4:

The problem in Section 4 is that we had not stated the DOSKEY command and therefore the line BAD = DIR A:\ /S was simply executing the first part, BAD, which would redo the entire BAD.BAT batch file, therefore creating and endless loop. By adding the DOSKEY command in front, the command would then be properly executed.

Sample Menu Batch File

 

MENU.BAT (simple)

@echo off

rem

rem This batch file will display a menu of three

rem choices on screen. These choices are MS-DOS

rem Editor, MS-DOS Anti-Virus, or MS-DOS Backup.

Rem

Cls

Echo.

Echo A Microsoft Editor

Echo B Microsoft Anti-Virus

Echo C Microsoft Backup

Echo.

Choice /c:abc Choose an option

if errorlevel 3 goto MSBackup

if errorlevel 2 goto Msav

if errorlevel 1 goto Edit

rem MS-DOS Editor Routine

:Edit

edit

goto End

rem MS-DOS Anti-Virus Routine

:Msav

msav

goto End

rem MS-DOS Backup Routine

:Msbackup

msbackup

goto End

:End