Flag This Hub

Searching for a String within a Registry Key

By


Strings and Registry Keys

Software programs write data to strings within a computer's registry keys. A VBScript programmer can take advantage of this to determine the state of a program prior to executing further instructions within their code. This can be accomplished by accessing the WScript.Shell object within the Windows Script Host.

The registry is full of information such as hardware, software and user settings. Although a programmer should use caution when working with the registry, especially if one is attempting to write to the registry or make changes, a programmer can write a VBScript that can read these strings of data within the registry to determine everything from operating system level to what license server an application is pointing to. The options are endless .

Things You'll Need Windows OS (98 or Newer)

String Search VBScript Code

VBScript Code
VBScript Code
Source: who10

Microsoft WSH and VBScript Programming for the Absolute Beginner is a great entry level book.

Microsoft WSH and VBScript Programming for the Absolute Beginner
Amazon Price: $22.24
List Price: $34.99
VBScript Programmer's Reference
Amazon Price: $14.48
List Price: $39.99
VBScript in a Nutshell, 2nd Edition
Amazon Price: $18.55
List Price: $49.99

Create Test Key and String

Go to “Start”-->”Run”, type “regedit” in the “Open:” box, then select “OK”. This will bring up the Registry Editor.

Right-click on "HKEY_CURRENT_USER" and select “New”-->”Key”. This will create a new key with an edit box ready for the input of a new name. Name this new key “Test”.

Select the new “Test” key. Right-click and select “New”-->”String Value”. This will create a new string value with an edit box ready for the input of a new name. Name this new string “Test”.

Create VBScript File

Create a VBScript file by going to “Start”-->”All Programs”-->”Accessories”-->”Notepad”. Save this file as “C:\Temp\DoesKeyExist.vbs”. Replacing the .txt file extension with a .vbs file extension creates an executable VBScript file.

Copy the following code. Do this by moving the mouse cursor just before the “o” in “option explicit”. Hold down the left mouse button and drag the mouse cursor just past the “)” in “WScript.Quit()”.

option explicit

Dim WshShl, regKey, RegRead

Set WshShl = WScript.CreateObject("WScript.Shell")

regKey = WshShl.RegRead ("HKCU\Test\Test")

If regKey <> "Test" then

wscript.echo "Key does not exist."

Else

wscript.echo regKey

End If

WScript.Quit()

Paste the code into the DoesKeyExist.vbs file created in the first step. Do this by selecting the open DoesKeyExist.vbs file. Right-click in the white space on the first line of the file and select “Paste”.

Save the DoesKeyExist.vbs file and exit Notepad.

Successful VBScript Run
Successful VBScript Run
Source: who10

Run the VBScript and Look at Message

Run the DoesKeyExist.vbs script. Do this by browsing to “C:\Temp\DoesKeyExist.vbs” using My Computer or Windows Explorer. My Computer is located at “Start”-->”My Computer”. Windows Explorer can be accessed by right-clicking on “Start” and selecting “Explore”.

Double-click on the DoesKeyExist.vbs file. A message box stating the word “Test” will appear. Select “OK”. This shows that the string within the key created earlier does exist and that the “WshShl.RegRead” portion of the VBScript is reading that value and displaying it using the “wscript.echo regKey”.

String Does Not Exist
String Does Not Exist
Source: who10

Run the VBScript Again After Changing Registry

Go back into the registry as shown earlier and double click on the “Test” string. This will open a window with the word “Test” under “Value data:”. Delete the word “Test” in this field and exit the registry. Double-click on the DoesKeyExist.vbs file. This will create a condition where the string within the key does not exist. The “WshShl.RegRead” portion of the script will reflect this and display a message stating that the “Key does not exist.”

Comments

No comments yet.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working