Batch Programming Basics  

Thursday, February 28, 2008

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

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!

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 - :labelIn 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 -
@IF EXIST C:WINDOWSEXPLORER.EXE ECHO It exists

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.

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.

AddThis Social Bookmark Button

0 comments: to “ Batch Programming Basics

Design by Amanda @ Blogger Buster