Fun with Batch Programming
By: Dirigo
Note: This article was written quite some time ago, hence it may not reflect my best efforts of any of my present abilities.
Legal Warning: Contents of this file are for educational purposes only. It is strongly suggested that you do not use this knowledge for illegal purposes. Mediocracy is Unacceptable (c) and the author of this content do not take any responsibility for the content's usage.
As you have wandered the endless void we refer to as the Internet, and explored numerous programs, you may have stumbled across varied programming languages and web-based languages. At some point you may have thought to yourself, "where the heck do I start". Perhaps you don't want to venture head-on into a complex, object-oriented programming language, such as C++, or maybe you just want to learn a language that is simple and can do numerous functions. One of those reasons may simply be that you want to impress your non-suspecting friends that you are indeed a programmer. Well, batch programming isn't really a "real" programming language, but it is a start down the long road of programming. Windows Shell Scripting is very close to the programming language BASIC, so users wanting to learn a linear programming language should consider taking a look at it in my next tutorial, "Windows Shell Scripting: A Step-up from Batch". The goal of this tutorial is to aid you in better understanding Window's Command Prompt, as well as teaching you how to shell script for yourself.
Ok, so let's get started. First, open up a blank text document (.txt) in Notepad. This will be the program that we shall use to write and edit our scripts. Next, type the following into your text file:
MsgBox "Hello, World"
If you are familiar with programming at all, you'll notice that "Hello, World" is the first message that is printed with your first program, almost like a programming ritual. Now save this file as HelloWorld.vbs. Locate this file on your computer and execute it by clicking on it. What you should see is a small message box that prints "Hello, World". This is all fine and dandy, but what does it mean? Well, MsgBox tell the interpreter to create a Message Box to print text in and "Hello, World" is the text that is to be printed within the Message Box. When the file extension of .vbs is added to a filename, it compiles the file as a Visual Basic Script. You have now learned a simple shell script, but what about a batch file. Batches are created in a way very similar to VBS, create a new text file and type the following text:
Msg * "Hello, World"
Now, instead of ending your file with the extension .vbs, end it with the extension .bat. Execute the batch in the same manner as the VBS and a message box should appear on the screen, almost identical to the previous one. I would like to mention now that this tutorial will be focused on batch programming, so I am sorry to disappoint the hopefuls' wanting to learn Windows Shell Scripting. There is no need to fret though, because shortly after the release of this tutorial, I will start working on my previously mentioned tutorial. I showed this minor demonstration of VBS to illustrate the idea that the same solutions can be accomplished using different methods.
Ok, now back to business. Our first demonstration was pretty self-explanatory, common sense should have told you that the command printed Hello, World to the screen. Let's learn some more difficult techniques shall we? Ok, so we now will answer the question, "how do we access other batch files with our current batch file." While this may seem unimportant to you now, trust me, it will come in handy later. The answer to this question is simple using the call command. The syntax of this command is listed below.
call [drive:][path] filename [batch-parameters]
If you are unfamiliar with programming, this may look and sound like another language to you, and in fact, it is ;), so let's break it down into small pieces to understand better.
First call tells the batch file to access execute another batch file, this is simple enough. Next we have [drive:]. This is the drive where the batch file is supposed to find the other batch file. The [path] gives the batch more specific directions as to where to find the other batch file. The filename should be the name of your other batch file. Finally, the [batch-parameters] is just the file extension of your other batch file. Now that you understand the call command (hopefully), let's look an example for clarity.
call C:\WINDOWS\SYSTEM32\CONFIG\Original\restore.bat
Since you have your first command under your belt, the others should seem easier to you and with that known, let's move on to chdir. If you hadn't guessed, chdir changes the directory that you are currently occupying.
chdir [[/d] [Drive:][Path] [..]] [[/d] [Drive:][Path] [..]]
/d changes the directory to a drive, and you now know what the function of [Drive:][Path] is. The chdir is a simple as that, so let's have an example.
chdir C:\WINDOWS
This command is also very useful, and you will probably be using it frequently in your batch files. Ok, we are moving rapidly now, if ever you do not understand, or are encountering errors, or simply want more information that what I've provided, you can visit the Microsoft Website that has a section devoted to the Command Prompt at the below URL.
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx
After a while of staring at a dull black and white Command Prompt, you are probably starting to get very tired of it. This can be simply fixed with the following command:
color
You can set these colors to anything that you desire, within the predefined colors of the Command Prompt.
Value|Color
0 | Black
1 | Blue
2 | Green
3 | Aqua
4 | Red
5 | Purple
6 | Yellow
7 | White
8 | Gray
9 | Light Blue
A | Light Green
B | Light Aqua
C | Light Red
D | Light Purple
E | Light Yellow
F | Bright White
The first value is the value to which the background is set, and the second is the foreground color.
Example: color 5D
This should produce the below background and foreground colors.
As you can see, the command produces a dark purple background, with a light purple foreground. Well, this is great if we want a visually please command prompt, but perhaps we are aiming for something else other than a suave-looking batch file The if command is indeed, a very, very useful one, as it is in nearly any programming language, so we are going to study it for a rather long time.
If has a small variety of parameters, the below syntax demonstrates each of them.
if [not] errorlevel number command [else expression]
if [not] string1==string2 command [else expression]
if [not] exist FileName command [else expression]
Wow, scary huh? Don't worry friend, it's really not that complicated at all, so let's break it down as we did in our previous command. We see in the first syntax that the if command initiates and we encounter the errorlevel number parameter. This usage of the if command is for controlling errors that occur within the batch. This command checks to see what errorlevel signal the program is emitting and executes accordingly.
Example:
if errorlevel 1 msg * "Failure."
else
msg * "Success!"
We see here that the above code checks whether the errorlevel signal is 1. If so, it will print the message "Failure" in a message box, but if not the program will print "Success". Well, I think the rest of the usages of the if command are pretty self-explanatory, but perhaps I will add a bit of detail about them, if people are having difficulties.