Making Batch Files
A batch file is a regular text file (*.txt) except that it has been given a *.bat extension. It can be created with any ASCII text editor such as Notepad or the DOS Editor. You simply type DOS commands into the file, one command per line. When you run the batch file, the series of DOS commands will be executed as though the commands were typed at a DOS prompt in a Command window.
Basic DOS Commands:
REM [comment]
Records comments (remarks) in a batch file.
Copies one or more files to another location.
[/A | /B]] [/V]
[/Y | /-Y]
source Specifies the file or files to be
copied.
/A Indicates an ASCII text file.
/B Indicates a binary file.
destination Specifies the directory and/or filename for
the new file(s).
/V Verifies that new files are written
correctly.
/Y Suppresses prompting to confirm you
want to overwrite an
existing destination file.
/-Y Causes prompting to confirm you want
to overwrite an
existing destination file.
The switch /Y may be preset in the COPYCMD environment
variable.
This may be overridden with /-Y on the command line
To append files, specify a single file for destination,
but multiple files
for source (using wildcards or file1+file2+file3 format).
Moves files and renames files and directories.
To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination
To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2
[drive:][path]filename1 Specifies the location and name of the file
or files you want to move.
destination Specifies the new location of the
file. Destination
can consist of a drive letter and colon, a directory
name, or a combination. If you are moving only one
file, you can also include a filename if you want
to rename the file when you move it.
[drive:][path]dirname1 Specifies
the directory you want to rename.
dirname2 Specifies the new name of the
directory.
/Y Suppresses prompting to confirm
creation of a directory
or overwriting of the destination.
/-Y Causes prompting to confirm
creation of a directory or
overwriting of the destination.
The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.
The MOVE command moves a file to a new location.
Deletes one or more files.
DEL [drive:][path]filename [/P]
ERASE [drive:][path]filename [/P]
[drive:][path]filename Specifies
the file(s) to delete. Specify multiple
files by using wildcards.
/P Prompts for confirmation before
deleting each file.
Clears the screen.
CLS
Suspends processing of a batch program and displays the
message:
Press any key to continue....
PAUSE
Displays messages, or turns command-echoing on or off.
ECHO [ON | OFF]
ECHO [message]
Type ECHO without parameters to display the current echo
setting.
The @ symbol means don’t echo a line if echo is on. So, typing ECHO OFF prevents the user from watching the batch program execute and typing:
@echo off
will keep the user from seeing the ECHO OFF message. It is common for batch programs to start with the @ECHO OFF command followed by CLS. If you turn ECHO OFF in your batch program, be sure to put ECHO ON at the end of the program. Otherwise other DOS programs may not show important messages.
*To launch an executable from a batch file, simply enter the path of the executable, just like you would do at a command prompt. If you use a relative path, it will be relative to the directory that the batch file was launched from.
Using Command line Arguments:
Command line arguments are parameters which follow the program to be launched at the command prompt. For instance, lets say my batch file is called example.bat. At a command prompt, I might type:
Example.bat John Smith
Here, “John” is the first command line argument and “Smith” is the second. My Batch file then might appear as:
@echo off
cls
echo My first name is %1
echo My Last name is %2
pause
echo on
The %1 and %2 act as place holders for the command line arguments.
Usually, commands in a batch file will execute sequentially. However, it is sometimes desirable to carry out commands in a different order. DOS has several programming structures which are similar to BASIC programming for manipulating your batch file.
Directs Windows to a labelled line in a batch program.
GOTO label
label Specifies a text string used in the batch program as a label.
You type a label on a line by itself, beginning with a colon.
The GOTO command can be used to jump from one point in a program to another. It can be used as a simple method for skipping forward or backwards in your program. You need to place labels in your program to act as reference points. For example:
echo off
REM print COOL all over the screen
:start
echo COOL
goto start
echo on
“:start” is the label . This program will type COOL over and over again in an infinite loop until you stop the program by pressing CTRL + C.
FOR...IN...DO
Runs a specified command for each file in a set of files.
FOR %variable IN (set) DO command [command-parameters]
%variable Specifies a replaceable parameter.
(set) Specifies a set of one or more
files. Wildcards may be used.
command Specifies the command to carry out for
each file.
command-parameters
Specifies parameters or switches for the specified command.
To use the FOR command in a batch program, specify
%%variable instead of
%variable.
The FOR statement does not have to be used with files, it can also be used with strings.
Example:
REM command that prints out a list of names
FOR %%a IN (Andy Bob Carl Darren Eric) DO echo %%a
The loop will execute the echo command 5 times because there are 5 items in the argument list. Notice how you can use the variable %%a as a substitute for each of the names.
Performs conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT Specifies that Windows should
carry out the command only
if the condition is false.
ERRORLEVEL
number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
command Specifies the command to carry out
if the condition is
met.
string1==string2 Specifies a
true condition if the specified text strings
match.
EXIST
filename Specifies a true condition
if the specified filename
exists.
The IF statement will execute the “command” if the condition is TRUE. It can optionally be used with the NOT modifier to reverse the condition.
Example:
ECHO OFF
REM check whether a file called 'test.txt' exists
IF EXIST test.txt GOTO :success
IF NOT EXIST test.txt GOTO :error
:success
ECHO file test.txt exists
GOTO :end
:error
ECHO Error - can't find the test.txt file
GOTO :end
Waits for the user to choose one of a set of choices.
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
/C[:]choices Specifies allowable keys. Default is YN
/N Do
not display choices and ? at end of prompt string.
/S Treat
choice keys as case sensitive.
/T[:]c,nn
Default choice to c after nn seconds
text
Prompt string to display
ERRORLEVEL is set to offset of key user presses in
choices.
Example:
CHOICE/C:yn Do you want to install MyTest %1
IF ERRORLEVEL 2 GOTO :no
IF ERRORLEVEL 1 GOTO :yes
:yes
setup.exe
GOTO :end
:no
echo operation cancelled
:end
You can use the CHOICE statement with as many possible choices as you want.
CHOICE/C:abcd choose a letter
IF ERRORLEVEL 4 GOTO choice_d
IF ERRORLEVEL 3 GOTO choice_c
IF ERRORLEVEL 2 GOTO choice_b
IF ERRORLEVEL 1 GOTO choice_a
Much more can be done with DOS and batch files, but what is offered here should be sufficient for most basic installers. If you would like more information about creating batch files, consult a DOS manual.
*Note: Text written in italics was extracted directly from DOS help. For help with any DOS command, type /? after the command at a DOS prompt.