Batch Files 101

A batch file (a file with a .bat extension i.e., monkeys.bat) is a basically a text file with a bunch of commands written in it. When a .bat file is run, the shell program reads the file and runs each command line by line. Batch files originated in DOS and the current Windows shell uses command files (a file with a .cmd extension) but can still run a .bat file.

Usage

I use batch files for repetitive and mundane tasks like backing up files from one drive to another – like coping my Firefox profile from my laptop to my flash drive for Portable Firefox or coping files from my desktop to my laptop. I find it’s mainly beneficial in situations where directories do not change. Although, opening the file and changing some variables is not difficult, it defeats the purpose of a unattended program that runs quickly.

How To Create A Batch File

Open Notepad. Hit File -> Save As. Type a file name with the .bat extension and change the document type to “All Files”.

Monkeys!

How To Edit A Batch File

If you double click the batch file, the computer will execute the commands in the file. To edit it, you need to right-click the file and select “Edit”.

Who's Your Daddy!

My First Batch

Create a batch file and type the following:

@echo off
echo Hello World!
pause

Save the file and run it (double-click the file).

The “@echo off” hides all of the processing the computer does. You can remove this if you need to troubleshoot.

“echo” on the second line, tells the computer what to do. In this case, print the following text: “Hello World!

The “pause” at the end generates the “Press any key to continue . . .” prompt. If this is left off, the file would run then close the window immediately.

Labels

Labels allow you to name a block of code. You can set conditions like: “if this is true, goto this block” The syntax for declaring a label is:

:name

So, I could make a label called “Error” by typing:

:Error

To goto a label, type the “GOTO” command and the label’s name:

GOTO Error

Variables

You can set variables in a batch file using the following syntax:

set variablename

“set” dictates that we are setting a variable and “variablename” would be the name of the variable!

To call the variable, you need to surround it in percent signs:

%variablename%

Example

Lets write a script that will copy the “stuff” directory from c:\stuff to j:\backup.

ECHO Backing Up Stuff Folder
set varSource=c:\stuff
set varDestination=j:\backup\stuff\
cd\
cd %varSource%
if errorlevel = 1 goto Error
goto DirOK
:Error
ECHO %varSource% directory does not exist!
Pause
Exit
:D irOK
cd\
xcopy "%varSource%" "%varDestination%" /D /Y
pause

Explanation

What happens in the above code is, first we tell the user what we are doing:

ECHO Backing Up Stuff Folder

Next we set the variables that will be using:

set varSource=c:\stuff
set varDestination=j:\backup\stuff\

Now we tell the computer where to start. We reset the current location to the C:\ drive, then we change directories to our source:

cd\
cd %varSource%

We do this for error checking. If the source we specified does not exist, we need to let the user know. We used an “If Statement”. It reads: If we get an error, then the “errorlevel” returns true, which equals 1, then goto the “:Error” label.

if errorlevel = 1 goto Error

If there is an error, the program would jump down tho the “:Error” label and would print: “stuff directory does not exist!” and pause, then exit the program.

If there is no error, it bypasses the error check and goes to the “DirOK” label.

goto DirOK

Since there is no error (the directory does exists), we switch back to the parent directory (c:\), we call the “xcopy” command and tell it to copy the folder! The switch “/D” copies only those files whose source time is newer then the destination time. The “/Y” overwrites existing files without prompting.

This is just the beginning of what you can do with batch & command files! Look for more tricks coming soon!