Learn Batch Programming Commands

Hey readers I am back with the Batch Programming tutorials. In my last post I told you guys about What is Batch Programming And Need For It. All those who havn’t read that post , pleast read it to get a more clear picture of the topic.

In this post we are going to code our first Batch Program. I am also going to tell you the most commonly used Batch Commands and their uses.

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:\\WINDOWS\\Desktop\\>ECHO Hello World!
Hello World!

Did you notice that it shows the command before executing it. But we’re coders right? We don’t want our code to look so untidy so just add an @ sign before ECHO and execute it. The code looks 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 language 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 –

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 don’t interrupt it Smile The GOTO statement is very useful 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 –


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 –


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 don’t 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 This is DOS

This command needs 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:\\Windows\\Desktop” it will take you to the Desktop. Here’s an example –

CD C:\Windows\Desktop
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 Testing.. > C:\\Windows\\Desktop\\test.txt
ECHO Testing…>> C:\\Windows\\Desktop\\test.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..

I Hope you liked it.  I am sure you learnt something new.

If you want to Learn more then make sure you subscribe to Learn Hacking as I would be posting regular updates of the topic.:)

2 Responses to “Learn Batch Programming Commands”

  1. lovenel

    Aug 30. 2011

    please send me all programming commands…i want to know all of it

    Reply to this comment
  2. Punisher

    Dec 03. 2011

    tanx @lot

    Reply to this comment

Leave a Reply