I was searching my gmail for some files – funny enough – one of the results dated back to Dec 6, 2004. Intrigued, I clicked and read – it was one of the tutorials/posts I wrote 7.5 years ago for a site i used to run with an old friend of mine.
It was such a trip down the memory lane back to the teen days ! It was fun to re-read some of the stuff we wrote back then and how cool we thought they are and such….
For the fun of it, since that post still stands and the tutorial can be useful – here it is – a Vintage tutorial I wrote 7.5 years ago on Batch Programming ( I copied/pasted as it is – hold your laughter or else ! )
Batch programming is Super cool, Easy and I classify it as Very Useful.
It is an OS specific language for DOS, it can give you an amazing control over it, this is why it is so Powerful.
Another great thing about Batch is that all you need is a Text Editor, no need for installing anything on your HDD, so you can practically sit anywhere and C0de.
Unlike .exe progs, the source code is always at your disposal, that makes batch programs more flexible: you can edit your prog, add a line or two, save and exit.
There are plenty of other things, like saving time and effort… but remember that
Being OS specific is a bad thing for a programming language, so some people don’t recommend it! And it is not that powerful compared to some other languages, it has limitations… but I think Batch is very easy to learn, it is interesting and useful, and still you can do lot of things with it, you only have to use your imagination, so why not learn it, it won’t take long :P
Note: if you know absolutely nothing about DOS, click here to go to the OS section and read the first 15 lines abut DOS, it is enough; don’t be scared it is not close to Chinese, if you know little about it keep reading…or you can simply copy paste this and see what each one does, learn by test.
First what u need is a text editor; you already have some text editors on your computer like DOS Edit, and Notepad.
To launch Edit all you have to do is go to the run menu or press window+r and type edit then enter
The same goes to notepad, go to run type notepad, or startàall programs à accessoriesà notepad
If you want to ask what about WordPad and Microsoft Word, can I use them?.
The answer is that you can, but you have to do the following, after saving your program you have to edit it to delete all the set of chinese ugly characters.
But I don’t see why you should bother…
So after choosing your editor, you can start typing your commands, one command at a line, and when you save you have to change the extension into .bat instead of .txt
To run your prog all you have to do is double click it in windows mode or type its name at the dos prompt if you are in the right directory.
Now what to type into the batch program:
First let us start by the “hello world!” program.
Rem this is the famous hello world program
Echo hello world!
Save it as .bat and run the prog
If u run in windows mode, you will only see a blink, so run it from the command prompt, you will see the code of your program, and the result, that means Hello world!…
If you want to hide the code written for this command, all u have to do is type @ before your command (@echo hello world!)…
Try it with 2 echos, one with @ and another without it.
Of course you won’t have to type @ at the beginning of each command to hide all your prog, all you have to do is type @ echo off before the commands you want to hide. Remember if you are doing your progs to be executed on other machines and you know that the other machines belong to an intelligent guy; remember to reset the echo by putting @echo on at the end of your prog.
Of course you can echo some other useful stuff other than the hello world, combined with other commands like “if” it can be useful for example to echo the result of a search( found, not found)…
You wanna have more fun with echo, try it with arguments:
echo ARGS: %1 %2 %3 %4
echo REV: %4 %3 %2 %1
echo MIXED: %2 %1 %4 %3
type at the prompt: urprog.bat I love rocknroll
oh, I almost forgot, rem stands for reminder, that is if you want to put notes in your programs, very useful when they get complicated
now you can try typing all sort of commands in ur progs, and one click, u execute them all and sit watching the output on the screen
I recommend typing these: :P
netstat -e -s
wow! Did you see it…
well a good advice use the “>” to create or overwrite a file and “>>” to append ( write at the end ) of a file that already exists. Here is an example:
tree /f /a >c:\\mytree.txt
this will provide you a better way for reading the answers now or later( you can add a command to copy or cut the file to floppy drive for example)… use your immagination for what you can do, do not mess with the format command :P
The IF command
It”s easy to find out what it is for. The if command evaluates a condition and, in case of true result, it executes a command. There are three ways to use IF in the following syntaxes:
IF [NOT] EXIST file command
IF [NOT] string1==string2 command
IF [NOT] ERRORLEVEL number command
Where command is the order (only one) you want to execute. The NOT word is optional, and it makes the condition inverse
You must be thinking, one order, this sucks, just wait a solution will be proposed later.
<span style="text-decoration: underline;">**IF [NOT] EXIST file command.</span>
What it does is to find out if there is or not a file. If the file exist (or not, if the NOT word is typed) then the command is executed. Otherwise, the command is ignored.
Maybe you don”t want to verify a file, but a drive or a directory. In this case, you have to look for the “file” NULL, that is present in any directory. For example, if you want to verify the c:\\folder0 directory, type this:
IF EXIST C:\\folder0\
Maybe your command will be to delete it :P
To test if a floppy disk is in, type this:
IF EXIST A:\<br>ULL command
<span style="text-decoration: underline;"> </span><span style="text-decoration: underline;">**IF [NOT] string1==string2 command</span>
Compares the two strings: string1 and string2.
In case that every character is equal in both (case sensitive and blank spaces ignored), the command is executed.
<span style="text-decoration: underline;">**IF [NOT] ERRORLEVEL number command</span>
This evaluates the last error level number present. Error levels are generated by programs to inform about the way they finished their execution. For example, format returns error level 3 when is Ctrl-C is pressed, and error level 0 when it finish normally. Not all the programs return error levels, and error levels are lost when another program is runned.
This is the most useful feature of IF command. We”ll explain it later, with the CHOICE command.
<strong><span style="text-decoration: underline;">Labels and GOTO command.</span></strong>
Labels are used to identify a line of the batch file. The way to put a label inside .bat progam is simply to put : before the label name.
For example, to create the “example” label, do this:
And that”s all.
Labels are really useful when combined with GOTO.
GOTO simply goes to the labeled line you want.
For example, to go to the :example label just type:
That makes the execution of the .bat file continue at that line, no matter if it was far below the current line or before it.
A good example of the GOTO command is this
net send pcname hi
That will create an interminable loop, and will keep sending the message hi to ur friend over and over again(don’t do it, this is not nice)
To stop your program, you have to press ctrl+c
To understand yourself labels, try the net send prog with 2 messages, like hi and how are you, watch what happens!
You can type at the end of your prog :end, and whenever you want to finish the execution of the prog just type :end
Do you remember that in the IF you can only put one order, But now, you can create a label and make this only one command be GOTO :label. Then, in this :label you can have as many commands as you want, and finish the execution or return to the program point you wanted with :end or :new_label
The command FOR
Now we”ll se how to make a batch file smaller. The for order is not very useful, but sometimes is exactly what you need. So lets see it.
The FOR command makes a “variable” change it”s value between the possibilities you gave it, and executes a command every time it changes. The
syntax is that:
FOR %%A IN (list of values) DO command
Here %%A is the name of the variable. (list of values) is just the list of values (easy, uh?) between which will change the variable %%A. The different values are separed by a blank space, and are only considered as strings.
To understand it, type this :
FOR %%B IN (www. Xim. uni.cc ) DO ECHO %%B
This will make the variable %%B change it”s value. First time will be www. Second time Xim. , and third uni.cc . Every time the %%B value is changed, the command after the DO word (ECHO %%B in our example) is executed.
So this line in a batch file will print in our screns:
There are other stuff to cover, there”s still choice, call, shift, and pause aka the real important stuff but I guess that will have to wait for a second tutorial Read More