Creating Batch Files

Many Computer users ask the question ... How do I create a batch file?

Batch files (or Batch Programs) are simply text files which store a list of DOS and some Windows commands and then execute them one after the other in a Batch operation. They save the user from having to repetitively retype a set of commands. Batch files were much more popular some years ago before the advent of Windows 95/98 however they still have their uses such as an automated backup or deletion of files etc.

All batch files must have a .bat extension in order to run. To start a batch file, you type its name at the command prompt (C:>) and press the ENTER key on the keyboard. You can also start a batch file in Windows 95/98 by double-clicking it in Windows Explorer or using a shortcut to the batch file. During this activity you may be asked to make choices in the form of key presses, Y for Yes, N for No followed by pressing the ENTER key on the keyboard.

The batch file named AUTOEXEC.BAT, located in your hard drive's root directory is regarded by DOS as a start-up file and will be run whenever you start your Computer. This file is not necessarily needed any more these days for Windows 95/98 however it may be used to start some Virus Scanning programs and set variables before Windows loads.

In a batch file the DOS command is always entered first. Some of the DOS commands can be entered using shortcut names. You can enter command as either lower case or upper case or a mix of both. Always leave a space after the command.

Some of the types of DOS commands that can be used in batch files are:

COPY          Copies one or more files to the location you specify

DEL              Deletes the files you specify

ATTRIB        Displays or changes file attributes

MOVE          Moves one or more files to the location you specify

RENAME      Changes the name of the file or files you specify

DELTREE      Deletes a directory and all the files and subdirectories

Some windows commands may also be used in batch files run under Windows 95/98

SNDREC32        Starts Sound Recorder

SNDVOL32        Starts the Volume Control applet

WORDPAD        Starts Wordpad

REGEDIT           Starts the Registry Editor

START /R EXPLORER        Starts Windows Explorer

START C:\WINDOWS\COMMAND.PIF      Starts a new DOS session

Special commands for use in Batch files

call          Calls one batch program from another without causing the first batch program to stop

choice        Prompts the user to make a choice in a batch program

echo          Displays or hides the text in batch programs when the program is running

for          Repeats a command for a group of files or directories

goto          Jumps to a label at another point in the batch file

if          Performs conditional processing in batch programs

pause          Halts processing of a batch program and displays a message "press any key to continue"

rem          Enables you to include comments (remarks)

shift        Changes the position of replaceable parameters in a batch program

To test and create a batch file go to the DOS prompt in Windows 95/98. Type in the set of commands you need the computer to do. If your set of commands work then start Notepad and type those same commands in the same order. Save that Notepad file with a .bat extension. Now instead of having to type those series of commands you can just double-click the batch file.

An example of a simple batch file to delete cookies from the hard drive

Rem Delete Cookie Batch Program
echo off
CLS
echo.
echo ************************************
echo.
echo ****** All COOKIES will be deleted *******
echo.
pause
echo COOKIE Delete in Progress
DEL C:\WINDOWS\COOKIES\*.TXT
echo.
echo.
echo COOKIE Deletion Finished
echo.
PAUSE
CLS

Notes: Any text following the ECHO command will be displayed. ECHO with a period after it will cause a blank line on the screen

Another example: A simple batch file to backup files in the My Documents folder to a diskette.

Note this will only work if less if there is less than a diskette of files to copy.More sophisticated backup batch files will be discussed in a later article.

REM Simple Back of My Documents
ECHO OFF
CLS
echo.
echo ************************************************
echo.
echo **** Ensure a Diskette is in Drive A *******
echo.
PAUSE
CD "C:\My Documents"
XCOPY32 *.* /E /I /D A:
echo.
echo ***** Copying finished .. Remove Diskette ******
echo.
PAUSE
CLS

Notes: The quote marks around C:\My Documents allows spaces to be used in the folder name and also long folder and file names

The "Switches" after the xcopy32 command ensure certain conditions are met.

For information on the switches at the DOS prompt type xcopy32 /?

Try a few of the other special batch commands and combine them with some DOS commands.

Information on the commands is obtained by typing the command followed by a space then the forward slash followed by a question mark. Please be aware that DOS is very powerful and particularly if you are working in native DOS rather than a DOS box within windows you may have the ability to do serious damage to your computer's files if you do not understand the commands.