Batch files are text files you may create using EDIT, or the DOS
EDIT program, or any other text editor, with a .BAT extension.
Writing Batch files is really high level programming. With the right batch programs you can even write entire applications with batch files.
Batch files may be executed at a command prompt just like a program by
typing the name of the batch file, or you may set up a Windows icon with
the name of the batch file on the command line of the icon.
In their simplest form, batch files may be used to execute more than one
program in sequence.
Batches may also CALL other batches:
PROG1
CALL BAT2.BAT
PROG2
After PROG1 runs, BAT2 is run. When BAT2 finishes, the calling batch
resumes with PROG2.
Each line in a batch file is a statement which may be a batch command, a
label, an internal DOS command or an external program. DOS
provides a number of command line utility programs.
These utilities are designed to replace the DOS program with the same name
and have the same base usage and syntax, but offer extended functionality
and/or performance. In order to make sure the Incode Systems version of
the program runs (and not the DOS version), make sure that the Incode
Systems version is in a path that preceeds the DOS command path in your
PATH statement in the environment table. This is the preferred method.
Other methods are deleting or renaming the DOS version of the program
file (a disadvantage is you have to remember to do this each time you
reinstall DOS (or Windows 95), or every time you use these programs,
include a fully qualified path in front of the program name.
To execute a program in a batch, simply enter the program name on the left
end of a line. If the program requires parameters on the command line,
these may be entered to the right of the program name.
Batch files may be passed parameters on the command line which invokes the
batch. Within the batch, these parameters may be referenced by %1 %2 %3
etc., (through 9) where %1 is the first parameter on the command, %2 the
second, and so on. The SHIFT batch command may be used to reference more
than 9 parameters. When SHIFT is executed, the first parameter is
discarded, and the second becomes %1, etc. Also, the %0 parameter is the
name of the batch itself. In Windows 95, command line parameters may be
delimited (separated) by a space, comma, or semicolon. If a single parameter
contains embedded spaces, you may surround it with double quotes. Double
quoted parameters include the double quotes when dereferenced. Prior to
Windows 95, parameters were separated by spaces only.
SHIFT Example:
:BEGIN
IF "%1"=="" GOTO END
PROG1 %1
SHIFT
GOTO BEGIN
:END
If the example above is in a file named BAT1.bat, the following command
line would execute the command lines shown below it:
The IF batch command may be used to compare strings or with the
ERRORLEVEL syntax to either branch to another place in a
batch to conditionally execute a command line.
ECHO is an internal DOS command which controls whether batch lines are
"echoed" to the screen as they are executed. ECHO ON causes lines to be
echoed, ECHO OFF causes them to NOT be echoed. The "at" character (@)
may also be used at the beginning of a line to cause it to not be echoed.
ECHO is ON by default. ECHO Message displays Message, even if ECHO
is OFF
To ECHO a blank line, use ECHO.
For example:
IF %1==HOWDY GOTO LABEL2
ECHO HOWDY is not parameter 1
GOTO MOREBAT
:LABEL2
ECHO HOWDY is parameter 1
:MOREBAT
The statement above checks to see if the first parameter on the batch file
command line is HOWDY, if it is, the batch jumps to LABEL2, if not it
continues with the next line.
PAUSE is an internal DOS Command which displays:
Press any key to continue . . .
and waits for a keystroke before continuing. If you want to branch based on
which key is pressed, use GETKEY or CONFIRM
FOR may be used to run a program multiple times.
FOR examples:
FOR %%F IN (*.TXT) DO PROG1 %%F
FOR %%F IN (First,Second,Third) DO PROG1 %%F
FOR %%F IN (D:\MyPath\*.txt) DO PROG1 %%F
The first FOR statement above runs the program named PROG1 once for each
instance of a file with a TXT extension. Note that %%F contains two
percent signs. That is because a percent sign used in a batch must be
doubled unless it refers to a parameter (%1 - %9). The same statement
may be run from a command prompt with a single percent sign. If there are
three files matching *.txt in the current directory: 1.txt, 2.txt, and 3.txt,
the following command lines would be executed:
PROG1 1.TXT
PROG1 2.TXT
PROG1 3.TXT
The second FOR statement runs the program named PROG1 three times
with the parameters listed. Note that parameters listed this way need not be
file names. The following command lines would be executed:
PROG1 First
PROG1 Second
PROG1 Third
The third FOR statement would run the following command lines if there were
three TXT files named 1.TXT, 2.TXT, and 3.TXT:
Now, suppose you want to rename them so the number in the file name appears
first. The following line in a BAT file will perform the commands that
follow:
FOR %%v IN (*_*.txt) DO FOR /F "tokens=1-3 delims=_." %%i IN ("%%v") DO REN "%%v" "%%j_%%i.%%k"