Batch Programming Basics

This tutorial explains you what batch file programming is and how to create batch files. Lets not waste time anymore, and start learning.

batch programming basics

The Basics of Batch 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 languages ( 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 powerful, it gives you absolute control over DOS. Batch isn’t recommended 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 don’t 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!

Hmm… 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 a “@” sign before ECHO and execute it. Woo hoo! 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 a “@” 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:WindowsDesktop” it will take you to the Desktop. Here’s an example –

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 Testing.. > C:WindowsDesktoptest.txt
ECHO Testing…>> C:WindowsDesktoptest.txt

Recommended: [How to] Telnet Lessons

See the difference? Anyway that’s all in the tutorial of Batch Programming Basics. Remember, each an every DOS command can be used in Batch.

11 thoughts on “Batch Programming Basics

  1. im trying to create a file that could let me shutdown or restart any pc on my LAN. The problem is that when i start the file, it automatically stops and closes itself. I managed to screen-cap it and it says “the syntax of the command is incorrect” I’ll post my code here so you can tell me what’s wrong with it.

    @echo off
    color 70
    Title Remote Control by Fish
    echo type “S” for Shutdown
    echo type “R” for Restart
    set /P K=What do you want me to do, GM?(S/R)
    if %K%==S goto :A
    if %K%==R goto :B

    net view
    set /P G=what PC number do you want me to sutdown, GM?
    timeout /t 3
    shutdown -m %G% -s
    msg * Goodbye, noob!
    exit /b

    net view
    set /P E =What PC Number do you want me to restart, GM?
    timeout /t 3
    shutdown-m %E% -r
    msg * Goodbye, noob!
    exit /b

  2. For you ones asking about ms dos commands: of course if you’re seeing this is because you have internet conection but imagine you don’t. MAny years ago my best companion on these ms dos and windows related stuff was (XP) windows help. Just press F1 when on the desktop or at an open explorer window like My Computer. You have a search box and from there you can read about windows system. Type “batch”, “echo” – same command you read here in this page a minute ago – “IF”, “goto”, “label” “MS DOS” “commands”, shell, glossary terms, syntax etc. Before the internet I learned many windows stuff from there. The source is Microsoft, you won’t see ads nor pub and everything is related (“related topics” – always useful); it’s like a book! Remember if you want to study let’s say batch and you have no internet you can read from there. There’s plenty of juice in windows help and it’s so underrated…

  3. Start all your programs with a single click using batch file « The HeaVen for tEcHy PeopLe says:

    [...] Batch Programming Basics [...]

  4. whenever i am running batch file i suddenly come up and go off ,
    i am unable to see output what should i do let me know help me

  5. Please I need this tutorial in my box.

  6. I make dootles on EVERYTHING like this.

  7. parabéns
    sou iniciante isso me ajudou muito
    @ ECHO funfa msm> UP.txt



  8. Deepak gandotra says:

    respected sir,

    i am an engineering student. i am in search of a good batch programming tutorial. please refer me some material that will be of great help to me. i am in great need of that. i will be highly thamkfull.

  9. Fantastic this guide has given me a good understanding of DOS and Batch files. it would be great if you could do some more on different commands like a list of dos commands and how they can be used in batch files

Leave a Reply

Your email address will not be published. Required fields are marked *