Learning Resources
 

Introduction to simple batch files


Batch files are files that have an extension ending in .bat. They are simply scripts that contain command prompt commands that will be executed in the order they are listed. To create a batch file, just make a file that ends in .bat, such as test.bat, and inside the file have the commands you would like. Each command should be on its own line and in the order you would like them to execute.

Below is example batch file. It has no real use but will give you an example of how a batch files works. This test batch file contains the following lines of text:

cd
cd \test
dir
cd \

In DOS, OS/2, and also Microsoft Windows, batch file is the name given to a type of script file, a text file containing a series of commands to be executed by the command interpreter.

The commands may be built into the command processor (COPY), supplied with the operating system but not built into it (XCOPY invokes the Microsoft DOS program XCOPY.EXE), or may be any program (cp invokes the program cp.exe if present, an .EXE port of the Unix cp command, with essentially the same functionality as XCOPY.EXE).

Similar to job control language and other systems on mainframe and minicomputer systems, batch files were added to ease the work required for certain regular tasks by allowing the user to set up a script to automate them. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe) reads the file and executes its commands, normally line-by-line. Unix-like operating systems (such as Linux) have a similar type of file called a shell script.

The filename extension .bat was used in DOS, and the Windows 9x family of operating systems. The Microsoft Windows NT-family of operating systems and OS/2 added .cmd. Batch files for other environments may have different extensions, e.g. .btm in 4DOS and 4NT related shells.

Batch file commands are  -

CALL

Call one batch program from another•

CHOICE

Accept keyboard input to a batch file

CLIP

Copy STDIN to the Windows clipboard

CLS

Clear the screen•

CMD

Start a new shell

COLOR

Change colors of the CMD window•

DOSKEY

Edit command line, recall commands, and create macros

ECHO

Display message on screen•

ENDLOCAL

End localisation of environment changes in a batch file•

EVENTCREATE

Add a message to the Windows event log

EXIT

Quit the current script/routine and set an errorlevel•

FOR

/F Loop command: against a set of files•

FOR

/F Loop command: against the results of another command•

FOR

Loopcommand: all options Files, Directory, List•

GOTO

Direct a batch program to jump to a labelled line•

IF

Conditionally perform a command•

IFMEMBER

Is the current user a member of a Workgroup

LOGEVENT

Write text to the event viewer

LOGTIME

Log the date and time in a file

MAPISEND

Send email from the command line

MORE

Display output, one screen at a time

PAUSE

Suspend processing of a batch file and display a message•

PROMPT

Change the command prompt•

REM

Record comments (remarks) in a batch file•

RUN

Start | commands

RUNAS

Execute a program under a different user account

SET

Display, set, or remove session environment variables•

SETLOCAL

Control the visibility of environment variables•

SETX

Set environment variables

SORT

Sort input

SHIFT

Shift the position of batch file parameters•

SLEEP

Wait for x seconds

START

Start a program, command or batch file•

TIMEOUT

Delay processing of a batch file

TITLE

Set the window title for a CMD.EXE session•

WAITFOR

Wait for or send a signal

WMIC

WMI Commands

::

Comment / Remark•