Batch Files

A batch file is to DOS what a macro is to Microsoft word. Basically it's a set of preprogrammed commands. So instead of having to open DOS and type in all the commands manualy you can write a batch file to do it for you.

In this tutorial I will be listing most Batch file commands. As a Batch file is just a list of DOS commands this will also be a list of DOS commands :) Two for the price of One. I will include some minor examples of how to use some of the commands.

CD [folder]
-Change Folder. Use 'CD ..' to go up a folder.
ProgamName
Just type the name of a program to run it.
ECHO [OFF/ON]
Stop/Start displaying each command on the screen. eg. 'ECHO OFF'
ECHO Text
Display the following so in this case it would display the word Text.
@command
Stops that line being displayed on the screen.
DEL [filename]
Delete the named file eg. 'DEL telnet.exe' or all files of a certain type
eg. 'Del *.tmp' to delete all .tmp files in the current directory. Can also use full path 'Del C:\Windows\command.exe'
PAUSE
Pauses the program.
IF EXIST file command
Checks to see IF a file exists in the current folder and if it does it carries out the command.
Can also do 'IF NOT EXIST'.
GOTO marker
Makes the code jump to the marker named. A marker must have a : before it such as :HERE so 'GOTO HERE' would take you to :HERE.
COPY file1 file2
Make a copy of file1 called file2.
MORE<file
Display the contents of the file to the screen.
COMMAND>file
Save the output to a file. Just replace file with a file name and and extension. So 'ECHO Test>c:\windows\desktop\test.doc' would make a text file on the desktop called test.doc. If you open test.doc it would show the text 'Test'. You can also do DIR>test.txt to save directory listings.
MD name
Make a Directory called name.
RD name
Delete the Directory called name. Directory must be empty.
DIR
List the contents of the current directory. 'DIR name' will display the contents of the named directory.
CLS
Clears the screen.
START filename
Open the file. Used for .txt, .bat, .doc, .bmp, and all none exe files really.
Remember - anywhere you have to supply a file or folder name, either for creation, reading, or deleting, you can put the full path such as 'DEL C:\windows\desktop\*.txt' would delete all .txt files on the desktop (remember that different versions of windows have different paths).

Parameters
To do parameters in a batch file we use %x (%1, %2, %3 etc). So ECHO %1 %2 %3 would display parameter 1 parameter 2 parameter 3. To better understand make a batch file called abc (just open up notepad and save the file with a .bat extension) with the following code:

@ECHO 1 = %1 2 = %2 3 = %3
Now place the batch file in your C drive and goto Run. Now type in
C:\abc.bat x y z.
Displayed we see
1 = x 2 = y 3 = z
(if it closes to fast put the PAUSE command on a new line after the '@ECHO 1 = %1 2 = %2 3 = %3'). We give our parameters in our call to run the bat file. The parameters are seperated by spaces. Goto Run again but this time put:
C:\abc.bat abc cry oz
Displayed we see:
1 = abc 2 = cry 3 = oz
You should know understand how parameters work.

As you can guess we can only have 9 parameters (1 - 9). But there is a way to use more. We do this using the SHIFT command. What shift does is delete parameter 1, then make parameter 2 parameter 1, parameter 3 becomes parameter 2. to demonstrate we're gonna make the ABC.BAT batch file again with the following commands

@ECHO OFF
ECHO 1 = %1 2 = %2 3 = %3
SHIFT
ECHO 1 = %1 2 = %2 3 = %3
Now goto run and type
C:\abc.bat abc 123 xyz 456
Yes the file only uses 3 parameters but it will ignore any over the required amount. Displayed we see this:
1 = abc 2 = 123 3 = xyz
1 = 123 2 = xyz 3 = 456
Hopefully this should explain how shift works. The first parameter is emptied then all the others are moved up a place.

There are a lot more commands you can use in Batch files and in DOS but this is just meant as a introduction and not a full blown tutorial.

Carbonize


Have Linux and want to learn more or just fancy trying it? Read our Linux tutorial.