Learn The Programming Commands Batch

17/06/2011 18:32

Hello readers, I'm back with the batch of programming tutorials. In my last post I told you about what programs and need lots. All those who have not yet read this message, read pleast for a clearer picture of the subject.

In this post we will code our first batch program. I will also tell you lots more used commands and their uses.

The first command you need to know is the echo. All ECHO does is simply print something on the screen. It's like "printf" in C or "Print" Basic. Anyway, that's how we use it.

ECHO Hello World!

Well, now save the above line as a. Bat and double click. This should output -

C: \ WINDOWS \ Desktop \> ECHO Hello World!

Hello world!

Have you noticed that shows the command before executing it. But we are programmers right? We do not want our code seems so messy that you can simply add the @ sign before ECHO and execute it. The code looks much better. The @ sign tells DOS to hide what the user commands being executed.

Now, what if I want to write to the file? This I will do -

@ Echo Hello World> hello.txt

Simple is not it? Remember, ">" create or replace file and ">>" connect (insert at the end) of an existing file. Guess why this program is used to work as desired, and -

@ Echo Hello World> hello.txt

@ ECHO Hello World Again hello.txt>

As for himself, you'll see that the program should write two lines, one after another, but it will not work because the first line will create a file called hello.txt and write the words "Hello World" it, and the second line to write a little more, the text above. So, in fact, what it does is it creates a file and write it and then over-writes what he had written to change what I just added a ">". The additional ">" is added to the DOS files. So here is the improved form of the program -

@ Echo Hello World> hello.txt

@ ECHO Hello World Again>> hello.txt

Save the code above as a. Bat and run it, works flawlessly.

The next thing we must learn is the GOTO statement. GOTO is exactly the same as it is in BASIC or for that fact any programming language, but the only difference between the labels.

This is a label in C or BASIC - Label:

This is a batch label -: label

In C or BASIC, the ":" comes after the label and is on the lot before the tag. Note on the fly. Here's an example of the GOTO statement -

: LabelOn

@ ECHO LoL

GOTO LabelOn

If you run this code, you will see that a loop is unlimited, it will keep the print on the screen until the end of the world if not to interrupt Smile The GOTO statement is very useful when it comes to building big Batch programs.

Now let's learn the SI and existing controls. The IF command is usually used to check if a file exists, as -

@ If exist C: \ WINDOWS \ EXPLORER.EXE ECHO

Please note that I did not use quotation marks (") as I would in BASIC or C.

EXIST command is only in Lot or in any other language. EXIST command can also be used to check if the file does not exist, like this -

@ If there is no C: \ WINDOWS \ EXPLORER.EXE ECHO

Remember, the lot is the language like C or BASIC or Pascal, can not do math. Lot, all the things you can do is control DOS. In the above example, note that there is no THEN command would not be in multiple languages.

Sick and tired of using the @ sign before every command? Do a little research, go to the DOS prompt and type in ECHO /? and press Enter. Interestingly, in this way, when you hear of a new DOS command, you do not know, just write "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 done, if you type ECHO OFF you no longer need to write an @ sign before every command.

Wait! just add an @ before ECHO OFF so that no message is displayed - ECHO is off.

The next command we are going to learn is the CLS command. Means clear. If you know BASIC, have no problem understanding this command. All this is clear proof. Here's an example -

@ ECHO OFF

CLS

ECHO This is DOS

This command requires no further explanation, but the type of CLS /? For more help on the order.

The following, we will learn is CD. What is the current directory. Displays the current directory is, if you type "CD" but if you type "CD C: \ Windows \ Desktop" which will be the desktop. Here's an example -

@ ECHO OFF

CD C: Windows Desktop

Echo test .. > Test.txt

Test.txt ECHO Testing ...>>

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

@ ECHO OFF

ECHO tests .. > C: \ Windows \ Desktop \ test.txt

ECHO Testing ...>> C: \ Windows \ Desktop \ test.txt

See the difference? Anyway, it's too batch file programming basics. Remember that each one any DOS command used in batch mode ..