Introduction to Scripting

Many tasks in the Windows environment can be accomplished through a series of simple clicks.  However, there occasionally arises a time in which certain tasks need to occur over a period of time or a task in which Windows cannot effectively handle it in a series of clicks or checkboxes.  Seasoned system administrators use scripts to handle these and other similar issues.

 Today we are going to discuss the basics to scripting, starting with the simple “batch file”.

In its simplest form, a batch file is nothing more than a .txt file that has been renamed .bat or .cmd and contains at least one command.

Batch files use a special program called a command processor to execute.  The older versions of Windows (95/98/ME) used a command processor which could execute .bat files.  Windows NT, 2000, and XP can also execute .bat files, but they are equipped to handle newer .cmd files as well.  The .cmd command processor can handle more powerful batch features.

 In the Windows NT, XP, or Vista environments you can bring up a Dos Virtual Machine (DVM) by going to RUN and typing CMD.  I’m sure many of you are familiar with the black command line prompt screen that appears when you do this.  Any command that you type in this window can be scripted, or saved in a .bat or .cmd file, to be run at a later time.

 One special thing to remember about batch files, is that they run sequentially.  In order words, the batch file waits to run any command until the preceding command completes successfully.

What Did You Say?

As children, many of us found delight in yelling down a long hallway or into a large area and listening to our voices echoing back to us.  Likewise, in a command environment, Windows will echo back to us what we have asked it to do.  The echo command is a tool that stops this behavior, and for this reason, it is often the first command found in a batch file.

 @ECHO OFF

By passing the @ECHO command the OFF parameter, we are effectively saying, “just do what I say, and don’t talk back to me about it” – a similar phrase I found myself telling my own children from time to time.

However, the echo command was not created simply for the purpose of stopping Windows from telling you what it was doing.  You can use the echo command to print information back to the command window.  For example:

ECHO Hello World

This command will print the line “Hello World” back to the command window, when executed.

Variables

It’s rather difficult to talk about scripting in-depth without quickly running into variables.  Many of us remember variables from our days in High School algebra class, where we ran into 3x+1 for the first time.  In this example, the “x” stood for something that we had to figure out by solving the problem.

In programming, and yes, scripting is like programming “lite”, variables are used to store specific information from one part of the program to another.  Variables enable the programmer to perform mathematical operations, store data, or even easily compare different data.

Windows has something called “Environment Variables” which store information about certain aspects of the Windows environment.  These can be easily viewed by calling the SET command from a Dos Virtual Machine.

For example, when I run the SET command in a DVM on my Windows XP computer, I am presented with an environment variable called HOMEDRIVE.  The SET command reports the following about this variable:

HOMEDRIVE=C:

If I wanted to, I could reference the C: drive by calling this variable in a script.  But what if you want to create a variable of your own?  This is also accomplished by the SET command.  For example, let’s assume that I want to create a variable to store my favorite color.  I would use the following command:

SET myFavColor=green

Now when I call the SET command, I will see a new variable called myFavColor and it will report it’s value to be green.

If I wanted to clear the value of the variable myFavColor, I would simple issue the command:

SET myFavColor=

By typing the command SET in the DVM, Windows reports the value of the variable myFavColor is equal to nothing.

Bringing IT Together

Now, let’s combine the ECHO command and the variable.  Let’s assume that you want to echo the variable you created.  This is accomplished by typing in the following command:

ECHO % myFavColor %

Assuming you had set the variable myFavColor = green, the output would look like this:

green

Notice that when referring to the variable myFavColor, we enclosed it with leading and trailing “%” signs.  This is because we needed to let the command processor know that we wanted to use a variable.  If we hadn’t enclosed the variable name with “%” signs, then the output of the ECHO command would have been the name of the variable, not what the variable had been set to.