Batch Programming Basics

Huge area to cover, we have assembled and written tutorials that have proven helpful over time.
Post Reply
User avatar
weazy
Ex-Admin
Posts: 1688
Joined: Sun Jul 07, 2002 10:02 am
Location: any given
Contact:

Batch Programming Basics

Post by weazy » Fri May 30, 2003 5:40 pm

A tutorial by Nikhil on The Basic's of Batch file programming. Explains you what batch file programming is and hot to create batch files.
The Basics of Batch File Programming
By Nikhil Ribeiro

Batch file programming is nothing but a batch of DOS ( Disk Operating System ) commands, hence the name Batch. If you code a lot and know many languages you are sure to notice that Operating System ( OS )specific langauges ( languages that work only on a particular operating system, eg: Visual Basic Scripting works only in Windows ) give you amazing control over the system. This is why Batch is so powerfull, it gives you absolute control over DOS. Batch isnt reccomended at all because it is OS specific, but it is fun and easy to learn. This tutorial will not only teach you Batch file programming but also how to fend for yourself and learn more commands that tutorials dont teach you.
The first command you should know is ECHO. All ECHO does is simply print something onto the screen. It's like "printf" in C or "PRINT" in Basic. Anyway, this is how we use it.

ECHO Hello World!

All right, now save the above line as a .bat file and double click it. This should be the output -

C:WINDOWSDesktop>ECHO Hello World!
Hello World!

Hmmm, notice that it shows the command before executing it. But we're coders right? We dont want our code to look so untidy so just add an @ sign before ECHO and execute it. Woohoo! much better. The @ sign tells DOS to hide from the user whatever commands it is executing. Now, what if I want to write to a file? This is how I do it -

@ECHO Hello World > hello.txt

Simple huh? Remember, ">" to create or overwrite a file and ">>" to append ( write at the end ) of a file that already exists. Guess why this program wont work as desired to -

@ECHO Hello World > hello.txt
@ECHO Hello World Again > hello.txt

Looking at it, you will see that the program is supposed to write two lines one after another but it wont work because in the first line it will create a file called hello.txt and write the words "Hello World" to it, and in the second line it just over-writes the earlier text. So actually what it is doing is that it creates a file and writes to it and then over-writes what it had earlier written, to change this we just add a ">". The additional ">" will make DOS append to the file. So here's the improved form of the program -

@ECHO Hello World > hello.txt
@ECHO Hello World Again >> hello.txt

Save the above code as a .bat file and execute it, it will work without a hitch. The next thing we should learn is the GOTO statement. GOTO is just the same as it is in BASIC or for that fact any programming langauge but the only difference is between the labels.

This is a label in C or BASIC - label:

This is a label in batch - :label

In C or BASIC, the ":" comes after the label and in Batch it comes before the label. Bear this in mind as you proceed. Here's an example of the GOTO statement -

:labelone
@ECHO LoL
GOTO labelone

If you execute this code, you will see that it is an unlimited loop; it will keep printing to the screen till the end of time if you dont interupt it :) The GOTO statement is very usefull when it comes to building big Batch programs. Now, we will learn the IF and EXIST commands. The IF command is usually used for checking if a file exists, like this -

@IF EXIST C:WINDOWSEXPLORER.EXE ECHO It exists

Observe that I have not used inverted commas ( " ) as I would in BASIC or C. The EXIST command is only found in Batch and not in any other language. The EXIST command can also be used to check if a file does not exist, like this -

@IF NOT EXIST C:WINDOWSEXPLORER.EXE ECHO It does not exist

Remember, Batch is not a language like C or BASIC or Pascal, it cannot do mathematical functions. In Batch, all you can do is control DOS. In the above example notice that there is no THEN command as there would be in most languages.
Sick and tired off using the @ sign before each and every command ? Let's do some research, go to the DOS prompt and type in ECHO /? and press enter. Interesting, in this way, when you hear of a new DOS command you dont know about, just type in "command /?" and you can get help on it. Now back to ECHO. According to the help we received by typing in ECHO /? you must have concluded if you type in ECHO OFF you no longer need to type an @ sign before every command.
Wait! just add an @ before ECHO OFF so that it does not display the message - ECHO is off.

The next command we are going to learn about is the CLS command. It stands for CLear Screen. If you know BASIC, you will have no problem understanding this command. All it does is clear the screen. Here's an example -

@ECHO OFF
CLS
ECHO This is DOS

This command need's no further explanation but type in CLS /? to get more help on the command.

The next command we are going to learn is CD. It stands for Current Directory. It displays the current directory in which you are if you just type in "CD" but if you type in"CD C:WindowsDesktop" it will take you to the Desktop. Here's an example -

@ECHO OFF
CD C:WindowsDesktop
ECHO Testing.. > test.txt
ECHO Testing...>>test.txt

This will change the directory to the Desktop and create a file there called test.txt and write to it. If we had not used the CD command, this is how the program would have looked.

@ECHO OFF
ECHO Testing.. > C:WindowsDesktoptest.txt
ECHO Testing...>> C:WindowsDesktoptest.txt

See the difference? Anyway that's all for the The Basics of Batch File Programming. Remember, each an every DOS command can be used in Batch.
--The Devil is in the Details--

Post Reply