Batch File Scripting

Batch files are plain text files with the extension .bat. They are executable, and contain commands. They are DOS based, and as such, will open up a command prompt screen when run.

Common commands used are:

cd <directory name> change directory (folder)
move <file name> <destination> moves file to destination
del <file name> deletes file
rd <directory name> emoves directory
md <directory name> makes directory
call <executable file name> runs program
echo <string> outputs string to standard output (screen)
dir outputs contents of current directory

 


They are useful for simple things, like copying of files for back up, or changing file
extensions.

cd prog
copy *dat \backup\*.bak

This script changes to the directory prog, then copies all files (* is a wildcard) with the extension .dat to the directory backup, and changes the extension to .bak.

Comments are added by putting REM in front of the line you wish to become a comment.

Flow Control

Like VBScript, batch file scripting has basic flow control structures. These structures are also commands. More information about each command can usually be found by entering I? after the command at a DOS prompt.

Branching

This script runs an ftp script to download the file. If it is successful then Done will be displayed, otherwise an error message will be displayed. It uses the IF and GOTO commands. Errorlevel is a special variable that is set every time a program ends execution, normally with the value 0 if there were no errors.

@ at the start of a line means that that line is not echoed to the screen when the batch file is run. Setting ECHO to be OFF means that none of the lines after turning ECHO OFF will be echoed to the screen.

: at the start of a line defines a label in the scripted, that can be jumped to using GOTO.

@ECHO OFF
ftp -s : download, ftp
IF terrorlevel% == 0 GOTO log echo An error has occurred GOTO end
:log
echo Done
:end

Looping

For loops are available in batch file scripting. This for ioop counts from 9 to 0, incrementing by -1 at each step, and echos the number that the count is currently at.

FOR /L %%a IN (9, -1, 0) DO ECHO %%a

Variables

Variables in batch files are of the type string (except errorlevel, which is special and of the type byte). They are set using the SET command, and can be used by enclosing the variable name in % signs, as shown in the following example:

SET myVariable=Anything
ECHO %myVariable%

Anything after the equal sign is assigned to the variable.

There are however a few exceptions; variables in for loops are preceded by %% when setting and reading, and input variables are preceded by % when reading, rather than being enclosed.

Input Arguments

A batch file can take any number of input arguments. These arguments become input variables, and are numbered from %0 to %9. %0 is the name of the batch file that was run. To make available more input arguments, the SHIFT command can be used, which shifts all values down by one, so that %0 takes the value of % 1, and so on until %9 become the next argument which was previously unavailable. Input variables are read only.

Enter this script into a text file in notepad. Save it as shifting.bat

@ECHO 1
@ECHO %0
@SHIFT
@ECHO %0
@ECHO %1

This batch file takes two arguments.
Run it by going to the Command Prompt, changing to the location you saved it and entering

shifting first second

the output will be:

first
shifting
first
second

Be careful when echoing input variables, because if you input on or off (both of which ECHO recognises) you may get the commands echoed to the screen when it is not wanted, and ECHO will not echo either of those words.

Output Redirection

The output programs run from batch files can be saved to a file instead of being written to the screen. This is known as output redirection.

echo Save this in a new file > newfile.txt 
echo Append this to the file >> newfile.txt 
dir | more

Use > to create a new file (or overwrite an existing file).
Use>> to append to an existing file.
Use | to pass the output to another program.

Input Redirection

Input redirection is similar to output redirection, except that it will cause the contents of a file to become the input arguments of a command.

date < currentdate.txt
time < currenttime.txt

Running VBScript from Batch Scripts

It is possible to launch any program from a batch file, and so VBScripts can also be launched by running CScript. This is useful when you need to do things that are too complicated in a batch file.

CALL CScript.exe moreProcessing.vbs

Was this article helpful?

Related Articles