money

Windows Scripting Basics

Microsoft has built an incredibly powerful scripting language that can control almost every aspect of the Windows operating system. What I will be discussing here are only the basics that apply mostly to windows XP. Windows scripting has been around since Windows 98 but as the operating systems have progressed the scripting language has as well.

All the tools that we need to created windows scripts are included in windows and the is simply notepad. You can get other editors to use such as Script Editor, Scintilla and SciTE, and many more. But all that you need is Notepad and you are good to go. When you create a scripting file you will create any name you want and you can give it the extension of .VBS (Visual Basic Script).

Lets write out first script, the famous Hello World script, by opening up notepad and simply typing:wscript.echo “Hello World”

Now simply double click on the file that you have created and you will see a pop up box that will display the text “Hello World” give it a try. This script can also be executed by a command prompt by typing “cscript hello.vbs” from the command prompt.

All good programmers comment their programs so that years from now they can look back at a program they wrote and tell what it is doing based on the comments. A comment is simply text in the program that is only seen by the programmer and never by the person running the program. You can type anything you want in a comment and it will not effect the program in any way. Professional programmers comment on every aspect of their program so that others who read the code can understand what it is doing. To add a comment in a VBS file simply type REM or ‘ at the beginning of a line and every thing on that line will be a remark and will have no effect on the program. Lets look at our Hello.vbs program and lets add a couple of remarks in the program so that we know what is going on.

‘ hello.vbs
‘ This is just your typical Hello World program

wscript.echo “Hello World”

If you happen to type something wrong windows will give you an error telling you that you have done something wrong and will even give you the line that the error is on. Lets mess up our program and see what kind of error we receive.

‘ hello.vbs
‘ This is just your typical Hello World program

wscript.echo “Hello World”
wscript.ech “I messed up this line”

Now lets run the program and see what kind of error we get. The first line is executed just fine but we receive a error on the second line:
Script: c:usersmikedesktophello.vbs
Line: 5
Char: 1
Error: Object doesn’t support this property or method: ‘wscript.ech’
Code: 800A01B6
Source: Microsoft VBScript runtime error

So as you can see from this error that we have a problem on line 5 character 1 of our code. The error Object doesn’t support this property or method: ‘wscript.ech’ usually means that you have a misspelling somewhere. The Code is not really that important to know just the location of the problem and what the “Error” actually says.

As with any programming language VBScript uses variables to hold values and strings. To assign a value to a variable simply use the variable name you would like and then the = sign. One examble would be ‘mike = 24’ now the variable mike has a value of 24, now lets add this to our Hello World program and show that the variable mike has a value of 24.

‘ hello.vbs
‘ This is your typical Hello World program

wscript.echo “Hello World”

mike = 24
jake = 30
diff = jake – mike

wscript.echo “Mike is “, mike
wscript.echo “Jake is “, jake
wscript.echo “The difference between their age is “, diff

As you can see here we assigned the value 24 to the variable mike and we assigned the value of 30 to the variable jake. We were then able to perform and mathmatical operation on the two values to get the difference between them. Then by using the wscript.echo command we were able to display the variables. Lets continue on with variables and lets store a string which is stored as text and we will get input from our user.

‘ hello.vbs
‘ This is your typical Hello World program

name = InputBox(“What is your name?”)
age = InputBox(“How old are you?”)

wscript.echo “Hello “, name
wscript.echo age, ” is a good age”

Do get the input box to pop up we will use the InputBox command and use the = sign to assign what the user types in to a variable and then we are able to print the results on the screen. Now lets go a little further and lets have the program make a decision about what to do based on what the user types. To make this program make a decision we will be using the IF… ELSE IF… ELSE… statements.

‘ hello.vbs
‘ This is your typical Hello World program

name = InputBox(“What is your name?”)
age = InputBox(“How old are you?”)

wscript.echo “Hello “, name

If age < 18 Then

wscript.echo “You’re Illegal!!”
ElseIf age > 17 AND age < 24 Then

wscript.echo “You’re a young one”
ElseIF age = 24 Then

wscript.echo “You are at the best age! (same as my age)”
Else

wscript.echo “WoW you are old!”
End If

With the script above we were able to receive input from the user and then make a decision on what to do based on what the user typed in. I will go through each line and describe exactly what it is doing so that you can fully understand the program.

name = InputBox(“What is your name?”)
age = InputBox(“How old are you?”)

Displays and input box asking the user “What is your name” then it stores the value in the variable name. Then is displays another input box asking the user “How old are you?” and stores the inputed value into the variable age. For this explaination we will say that the user typed in 24 for the age.

wscript.echo “Hello”, name
This line simply displays that variable name which is what the user typed in the first input box.

If age < 18 then

wscript.echo “You’re Illegal!!”
This line is is executed exactly as it is read, If the variable age is less than 18 then print on the screen “You’re Illegal!!”. As we stated above the variable age is going to be 24 so the variable age is not less than 18 so the line directly below will now be executed.

ElseIf age > 17 AND age < 24 Then

wscript.echo “You’re a young one”
The above statments is using the ElseIf statement which will only be looked at if the above statement was false. We are now checking to see if the age variable is between 17 and 24 and if it is we will print “You’re a young one”. But since the variable age is 24 then it is not between 17 and 24 so this statement will not be executed and we will move on to the next.

ElseIf age = 24 Then

wscript.echo “You are at the best age! (same as my age)”
This statement is the same as the previous one except we are seeing if the variable age is equal to 24 and if it is we will execute the line directly below it. In our example since we did type in 24 so the variable age equals 24 the line directly below will be executed and a windows will pop up saying “You are at the best age! (same as my age)” and the entire IF statement will be finished. Now we will continue on with this example and check on the last line.

Else

wscript.echo “WoW you are old!”
End If
The Else statement is only executed if none of the above criteria are met and in our case since the age variable equals 24 the Else statement will not be executed. We then have to add the End If statement at the very end to tell the program at the entire IF statement is over at this point.

We have covered a lot of ground here with VBScript so please review everything we have talked about and play with the scripts. Experiment as see what you can do with the script and think about how you can mix multiple variables and input boxes and IF statements to put together a very useful program that can make your life easier.

I stated in the beginning I was going to discuss a script that I created to modernize an older piece of software to save money on upgrading. What I will discuss here was not covered in this article but it will give you an idea of the kinds of things you can do with scripting.

I was asked to undertake a task at work to migrate all of our alerting systems from older pagers to our newer cell phones utilizing text messaging. This wasn’t a big problem with most of our software because of the ability to send an email to the cell phones as a text message. But we had two very important pieces of software that were about $10,000 a piece to upgrade to a version that would be able to send emails and upgrading was not an option for the time frame that we had to implement our new cell phones.

Both programs had the ability to print to a printer when an alert happened. I setup the programs to print to a file and put it in a specific directory whenever an alert happened. The script I wrote looped every 10 seconds and would look for a new file in that particular directory. When the script found a new file in folder it would then email the contents of the file to the appropriate cell phone and then delete the file and wait for another new file.

Please keep watching for new articles for more detail about windows scripting. If you have any questions or would like more detail about my custom emailing script please feel free to contact me. Thanks for reading