The VBScript Network and Systems Administrator's Cafe:

working with variables

Dec 29 2008   3:21PM GMT

Using special characters in VBScript strings



Posted by: Jerry Lees
strings, String manipulation, string, working with variables, Special Characters, ASCII, VBScipt

I just realized I’ve been using Visual Basic String constants in my scripts with little to no explanation. We’ll rectify that situation right now! ;-)

When dealing with strings in VBScript, you occasionally need to add formatting characters to a string, so that when it is displayed on the screen or in a file it looks correct. If you know your ASCII chart you can certainly preform this with a “& chr(X) &”.However, if you don’t know what ASCII value a tab is (it’s 9) then you can simply use Visual Basic’s string constants in your scripts and have the added benefit if making it easier to read!

The constants and their meanings are listed below:

VBTab A Tab character [Chr(9)]
VBCr A carriage return [Chr(13)]
VBCrLf A carriage return and line feed [Chr(13) + Chr(10)]
vbBack A backspace character [Chr(8)]
vbLf A linefeed [Chr(10)]
vbNewLine A platform-specific new line character, either [Chr(13) + Chr(10)] or [Chr(13)]
vbNullChar A null character of value 0 [Chr(0)]
vbNullString A string of value 0 [no Chr code]; note that this is not the same as “”

Oct 30 2008   12:00AM GMT

A new found bug in VBScript that causes if then statements to return wrong comparisons (or is it?)



Posted by: Jerry Lees
VBScript, Variable Types, string, working with variables, Integer, variant

In the past I’ve cautioned you on always initializing your variables and encouraged you to always declare your variable types, rather than using the default variant type in VBScript. I’ll be the first to admit I don’t follow my own advice in my examples! However, variants can be dangerous!

Recently I had a situation where if statements were returning completely not correct results and for the life of me I couldn’t figure it out! Here is an example that illustrates what was happening to me.

one = 1
two = 2
three = 3
OneBillion = 1000000000

MyVar = inputbox(”Enter a number”)If one < MyVar Then
     WScript.Echo (one & ” is lessthan ” & MyVar)
End If
If two < MyVar Then
     WScript.Echo (two & ” is lessthan ” & MyVar)
End If
If three < MyVar Then
     WScript.Echo (three & ” is lessthan ” & MyVar)
End If
If OneBillion < MyVar Then
     WScript.Echo (OneBillion & ” is lessthan ” & MyVar)
End If

The basis of the problem is that I was taking input from a file that was a number– however I was reading the file and it was coming into the script and being used as a string by the variant variable. I then was comparing it to a number and the comparisons were not working 100% correctly. Check it out by entering several numbers into the script. Try entering 0, 1, 2, 3, and 1000000001.

The moral of the story here is to always cast your varables as a specific type or use the cint() function to convert your variant varables to numbers before doing number comparisons with a literal number and a variant variable that is supposed to contain a number.


Aug 1 2008   10:00PM GMT

VBScript Statements: Explanation of the Set Statement



Posted by: Jerry Lees
VBScript, VBScript Statements, Objects, set, VBScript Objects, working with objects, set command, working with variables

The set statement is used to assign any value to any type of variable. For the most part this command is optional. However, it needs to be used when you are assigning an object reference to a variable, such as when you use a variable to hold the return of a CreateObject function for later reference in your code.

An example of the set command would look like:

Option explicit
Dim objDictionary
Set objDictionary = CreateObject(”Scripting.Dictionary”)