Incode Systems Home Page Incode Systems, Inc.


Batch Files



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.

DOS Commands

Note: You may see your DOS version's usage of the internal commands by clicking on the commands listed below. This will open a DOS windows, display the usage and wait for keystroke to close the window. Be sure to open only one window at a time. The internal and external commands listed correspond to version 7.0. If you are running an older version of DOS, or the command file is not installed on your computer, you will see the message:
Bad command or file name.

Internal DOS Commands are:

BREAK
CALL
CD
CHCP
CHDIR
CLS
COPY
CTTY
DATE
DEL
DIR
ECHO
ERASE
EXIT
FOR
GOTO
IF
LFNFOR
LOADHIGH
LOCK
MD
MKDIR
PATH
PAUSE
PROMPT
RD
REM
REN
RENAME
RMDIR
SET
SHIFT
TIME
TYPE
UNLOCK
VER
VERIFY
VOL

External DOS Commands are:

ATTRIB
CHKDSK
CHOICE
DEBUG
DELTREE
DISKCOPY
DOSKEY
EDIT
EXTRACT
FC
FDISK
FIND
FORMAT
KEYB
LABEL
MEM
MODE
MORE
MOVE
MSCDEX
NLSFUNC
SCANDISK
SHARE
SORT
START
SUBST
SYS
XCOPY

For usage on each of internal or external commands, type the command followed by /? at a command prompt. For example:

ECHO/?

will display the usage for ECHO.

To find external commands look in the C:\DOS directory, or the C:\WINDOWS\COMMAND directory and follow the same procedure to discover the usage for the command.

Note: Incode Systems offers the following batch utilities which have the same name as DOS utilities:

SORT
MORE

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:

BAT1 Parm1 Parm2 Parm3 Parm4 Parm5 Parm6
PROG1 Parm1
PROG1 Parm2
PROG1 Parm3
PROG1 Parm4
PROG1 Parm5
PROG1 Parm6

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:

PROG1 D:\MyPath\1.txt
PROG1 D:\MyPath\2.txt
PROG1 D:\MyPath\3.txt

If Command Extensions are available in your version of the OS and are enabled, the FOR statement can do much more (run FOR /? | MORE for details)

Command Extensions FOR example:

Suppose you have a collection of files with the following names:

BOOK1_001.txt
BOOK1_002.txt
BOOK1_003.txt
.
.
.
BOOK200_001.txt
BOOK200_002.txt
BOOK200_003.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"

REN "BOOK1_001.txt" "001_BOOK1.txt"
REN "BOOK1_002.txt" "002_BOOK1.txt"
REN "BOOK1_003.txt" "003_BOOK1.txt"
.
.
.
REN "BOOK200_001.txt" "001_BOOK200.txt"
REN "BOOK200_002.txt" "002_BOOK200.txt"
REN "BOOK200_003.txt" "003_BOOK200.txt"