Windows Tip: Batch Files
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”.

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”.

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
:DirOK
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!
Start Slide Show with PicLens LiteRyu Says 'Read These Related Posts!'
[...] create a batch file with the following [...]
[...] a new batch file and write a shutdown command. All you really need [...]
[...] could write a batch file to automate the backup process, or navigate to the directory you want to back up, [...]
[...] all know that I love the Command Prompt and creating fanciness through Batch Files - and here’s a good one for you: Set your IP address via a batch [...]
i have a Question
how do you end the shell process in a MS-DOS prompt?
i try but it always comes back on in a few seconds
this site is very GOOD
but i have a ? ho to add a local computer in domain via bath file plz let us know on my mail ID i am fresher in this fild
I am happy to have this community, I have two more questions to ask …
1. How can i add more ten 9 set parameters in a batch file e.g.
SET String1=%1
SET String2=%2
SET String3=%3
SET String4=%4
SET String5=%5
SET String6=%6
SET String7=%7
SET String8=%8
SET String9=%9
2. And the second one is how can I add a string which includes a comma in between (treating comma as a space) e.g.
This is my single string including some numbers, bracket and comma (-6.26620974,53.34899072)