BATCH FILE PROGRAMMING
Today we explore what Ms Dos (Batch file) programming is.
Everyone has been in use of the Ms Dos program in windows platform (what is commonly refered to as CMD)
Let's begin by understanding what Batch means:
A batch file is simply a text file edited in the notepad or other basic editing tool like EDIT command in Ms Dos but lacks any editing.
Uniquely, the file is stored with a .BAT extension such as virus.bat etc..
Batch files are basically programs containing MS-DOS commands written in separate lines and using the correct syntax:
How a batch file operates!!
We shall accomplish this by explaining example batch codes
Example 1
// the three line code below is a basic batch file. save the file as teln.bat (or any other name of your choice)
C:
cd windows
telnet
explanation:
The first line instructs command.com to go to directory/ drive C:
The next line tells the command.com to change the current directory to Windows while the last lines instructs it to launch telnet client.
Example 2
md \newdir
copy \dos\*.exe \newdir
cd \newdir
dir
cd \
Executing commands at DOS prompt:
Normally, you can execute only one MS-DOS command at one time (except you use a "trick").
You cannot give another instruction before DOS has done your current command
If you manually instruct DOS to execute the above commands, you have to type each command at the DOS prompt one after another.
Executing commands in a batch file:
However, if you put all of the commands in a text file in the same manner as in the above box, it becomes an executable program.
Let's call it anyname.bat Similar to a COM or EXE command, you can simply type the name of this batch file at the DOS prompt to start your instructions.
i.e. C:\>anyname or C:\>anyname.bat (note: the extension bat is optional here. It makes no difference, no matter you put it or not.)
DOS will then execute the commands automatically in the same order as written in the anyname.bat The followings are details of what DOS will do for you:-
- Creates a new directory under the root called newdir
- Copies all files under the DOS directory with an extension of EXE to the newly created newdir directory.
- Changes the current directory to newdir directory
- Display the directory listing of newdir directory
- Changes the current directory to root directory
Commands that can be used in a batch file
All commands that you can use at the DOS prompt can be used in a batch file.
You have to follow the same syntax of the commands as you use it at the DOS prompt.
For advanced application of batch programs, the following nine (9) special commands are commonly used. Some of them are used together with special characters (details coming soon)
CALL
|
FOR
|
PAUSE
|
CHOICE *
|
GOTO
|
REM
|
ECHO
|
IF
|
SHIFT
|
Note: * CHOICE is an external DOS command.
Please ensure the current path statement contains
C:\DOS when you need to run a batch program in which the CHOICE command
is used. |
Special characters commonly used in a batch file
Special characters
|
@
|
:
|
%
|
%%
|
Name: |
batch display suppression
operator
|
batch file label operator |
batch replaceable
parameter |
batch replaceable
parameter
|
Syntax: |
@<command>
where command is any MS-DOS command
|
:<label>
where label is a string up to 8 characters long |
%n
where n is a single digit 1 through 9 |
%%n
where n is a single digit 1 through 9 |
Example: |
@echo off
|
:option1 |
%1, %2, %3 etc. |
%%1, %%2 etc. |
Remarks |
-- |
-- |
-- |
used with FOR command only |
-
Batch display-suppression operator
-
@ can be applied to any command as the first character on the command line
in a batch program.
-
It's used to prevent an individual command from being displayed when it
is executed.
-
Batch file label operator
-
: is used to identify a location in a batch file. It is not
a command but a prefix of a label.
-
A label is a companion of the GOTO command.
i.e. GOTO <label>
:<label>
-
A label is used to force MS-DOS to move to a specified location within
the batch program.
-
Normally, when MS-DOS runs a batch program, it executes commands in the
order as they appear in the program. However, when MS-DOS comes across
the GOTO command (e.g. GOTO option1), it will move to the position that
is identified by :option1 within
the batch program.
-
Batch replaceable parameter
-
Consider the following commands frequently used at the DOS prompt:-
-
copy autoexec.bat
autoexec.bak
This command line creates a backup file of the
autoexec.bat file.
copy is a command,
autoexec.bat and
autoexec.bak
are parameters. These are required parameters. You must state both of them
to make the command work.
-
dir /w
This command line displays a directory entries of the current directory
in wide screen format.
dir is a command, /w
is a parameter. It is an optional parameter for qualifying the output of
the dir command.
-
Similarly, you can make use of parameters to help you achieve the same
capability in executing a batch program. This way, you can execute the
same batch program with different data at different time. They are called
replaceable parameters.
-
%n and %%n are the special characters which represent the
parameter in a batch program, where n is a single digit, from 1 through
9.
-
The replaceable parameters are positional. The digit, n, of the special
characters represents the position of the parameter you type with the batch
command.
For example:
A sample batch file named
greeting.bat |
Batch file executed in
DOS prompt |
@echo off
echo %1 is %2
echo |
c:\>greeting
IOS100 fun
IOS100 is fun
(<-- i.e. the output)
|
Replacement of % n by given parameters:
|
batch filename (a.k.a. %0)
|
greeting
|
%1
|
IOS100
|
%2
|
fun
|
How to create a batch file
-
EDIT command
-
Usage: EDIT filename.bat
where filename.bat is an optional parameter.
You are recommended to give a name when you open the editor because it
will simplify some steps in saving the file when it's done.
-
Run EDIT command at the command prompt, you will
go to an editor mode, normally in blue screen. Make sure you have saved
the file upon completion.
-
Remember to give an extension of .BAT in your batch
file.
-
COPY command
-
Usage: COPY con
filename.bat
where filename.bat
is a required parameter if you want to save the file. You must give a name
when you run COPY command.
-
This method is useful when you are writing a short
batch file.
-
"COPY con filename"
-
Similar to backup a file using COPY command, "COPY
con filename" means copying from "con" (the source) to a file called filename.bat
(the target).
-
CON is a reserved name for console. The keyboard
is known to MS-DOS as console.
-
By running the command, you instruct MS-DOS to copy
what you type from the keyboard to a file filename.bat
. When MS-DOS display a blinking cursor
instead of a command prompt, it means it's ready for you to type in any
text.
-
Remember to use Ctrl-Z (i.e. hold down the ctrl key
and hit Z) or press the function key F6 to end the file. MS-DOS will display
^Z on
the screen. It is the end of file marker.
-
And then, press Enter to save the file. MS-DOS will
signal back "1 file(s) copied"
and return to the command prompt.
-
DOSKEY command
-
Usage: DOSKEY /h > filename.bat
-
This method is useful for a "write-and-test" approach.
While you are writing a batch file, you are working on the command prompt
directly. By the way, you can know the result of every command immediately.
-
The DOSKEY program, once loaded into memory, keeps
track of the commands you have typed through the keyboard.
-
DOSKEY /h -- displays all commands stored in memory.
-
By running the command, you instruct MS-DOS to redirect
the history of commands to a batch file filename.bat
.
-
Steps in using this method:
-
Make sure DOSKEY program has been loaded into memory.
-
Press Alt-F7 to clear the memory buffer before you
"write-and-test" your file.
-
Run the commands that you want to put in the batch
file.
-
Upon completion, run DOSKEY /h > filename.bat
-
Run EDIT filename.bat
to delete the last line "i.e. DOSKEY
/h > filename.bat" and all other unnecessary lines, if necessary.