String archives - The VBScript Network and Systems Administrator's Cafe

The VBScript Network and Systems Administrator's Cafe:

string

Jan 30 2009   1:30AM GMT

Easy String Searches with the VBScript Instr function



Posted by: Jerry Lees
InStr function, String manipulation, strings, string, VBScript, Scripting

I recently had a situation where I needed to find string inside a string to parse the larger string into usable and separate chunks of data. In writing this script, I realized I’ve not shared this gem of a function with you previously… and it really makes matters easier when you are manipulating strings.

That function is the Instr function. When configured with the proper parameters, by default, it will return the FIRST location in a string where a sub-string occurs. (Hint: this is where a mid() function would come in handy after you have that starting location). If the sub-string is not found, the function returns 0. The syntax is as follows:

Instr(StartPostion, StringToSearch,StringtoFind, Compare)

 

 The script I wrote really is a blog entry of it’s own, and will be, but I wanted to first introduce you to this great function and let you have a chance to play with it and see some code in action. Below is an example script that takes a string and searched for specific sub strings, then returns the location in the string where the sub string occurs.

 

Option explicit
Dim StrSource

StrSource = “Now I can find a word In a long string of words in a sentance.”

WScript.Echo(InStr(1,StrSource,”In”))
WScript.Echo(InStr(1,StrSource,”in”))
WScript.Echo(InStr(1,StrSource,” in “))
WScript.Echo(InStr(1,StrSource,”now”))

Below is the output from the script. Notice, the following:

  • The case of in and In in the sub-string being searched for and the positions it returns?
  • Notice the substring “in ” returns something entirely different?
  • Notice the substring now returns 0?

 

23
12
48
0

 

Enjoy!

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 “”


Dec 2 2008   12:55AM GMT

Very simple encryption example with VBScript



Posted by: Jerry Lees
encryption, VBScript, 3des, string, encrypt, Toolkit, String manipulation, decrytption, mid, StrReverse, Reverse Strings, decrypt, RSA

I previously mentioned a routine that I wrote to encrypt a string. Now, before the security folks look at the code… understand this:

This is intended only to obscure a string from a casual prying eye. It is NOT intended to be a replacement for true encryption like 3DES and RSA encryption. Please do NOT assume this routine is in any way secure or uncrackable. It is intended to only be an exercise in working with strings and is only as secure as the price you have paid for it. Nothing. ;-) Furthermore, it is provided as-is without warranties and by using it you agree that all risk from it’s use is transferred to you.

….Now that we’ve gotten the unpleasant legal disclaimer out of the way… Lets discuss the code!

Essentially, The code uses a variable length key to obscure the original string by iterating through the string you want obscured and adding the ASCII value of the character at each position of the original string with the ASCII value of a rotating “key character” in the key provided to generate a new ASCII value. This new ASCII value is then converted to a character and added to the newly encrypted string. The obscured string is further obscured by the fact that the original string is reversed prior to being changed. 

This key position changes after each character in the original string is obscured. The result is the key is iterated through sequentially as the original string is encrypted and when the end of the key string is encountered the iteration through the key string is started again from the beginning of the key string until the original string is completely encrypted.

This process works because the ASCII values in the typical string and the typical key string when added together do not exceed 255. (The highest possible ASCII character) Essentially, Strings and Keys with ASCII values higher than 126 should not be used or the result could be unpredictable– or worse yet, an unencryptable string.

Now that I’ve explained a bit about the premise… Lets look at the code!

Option Explicit

Dim temp, key

temp = “Now is the time for all good men To come To the aid of their fellow countrymen.”
key = “huasHIYhkasdho1″

temp = Encrypt(temp,key)
WScript.Echo temp
temp = Decrypt(temp,key)
WScript.Echo temp

Function encrypt(Str, key)
 Dim lenKey, KeyPos, LenStr, x, Newstr
 
 Newstr = “”
 lenKey = Len(key)
 KeyPos = 1
 LenStr = Len(Str)
 str = StrReverse(str)
 For x = 1 To LenStr
      Newstr = Newstr & chr(asc(Mid(str,x,1)) + Asc(Mid(key,KeyPos,1)))
      KeyPos = keypos+1
      If KeyPos > lenKey Then KeyPos = 1
 Next
 encrypt = Newstr
End Function

Function Decrypt(str,key)
 Dim lenKey, KeyPos, LenStr, x, Newstr
 
 Newstr = “”
 lenKey = Len(key)
 KeyPos = 1
 LenStr = Len(Str)
 
 str=StrReverse(str)
 For x = LenStr To 1 Step -1
      Newstr = Newstr & chr(asc(Mid(str,x,1)) - Asc(Mid(key,KeyPos,1)))
      KeyPos = KeyPos+1
      If KeyPos > lenKey Then KeyPos = 1
      Next
      Newstr=StrReverse(Newstr)
      Decrypt = Newstr
End Function


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.