Previous Page
Next Page

Creating the WshShell Object

To use the Reg.exe tool to back up the registry, it is necessary to create an instance of the WshShell class. This enables you to launch programs that are not part of Windows Scripting Host. The following program, RegBack.vbs, illustrates using the WshShell object.

RegBack.vbs

Option Explicit
Dim objShell

WScript.Echo("beginning " & Now)
Set objShell = CreateObject("WScript.Shell")
objShell.Exec "%comspec% /k reg.exe EXPORT HKLM c:\hklm.reg"
WScript.Echo("completed " & Now)

As you can see in RegBack.vbs, you declare a variable called objShell and use it to hold the WshShell object. After you have this object, you use the Exec method to launch a commandline interpreter with the /k option.

Note

The /k when used with the Cmd.exe program means to leave the command window open so that you can examine anything written to the window by using the program you are executing. However, it seems the behavior of /k and /c (which means to close the command window after the script is finished executing) is largely dependant upon the command being executed, and it therefore could seem to be erratic and unpredictable. As always, if something is important to you, test it in a lab.


Setting the comspec Variable

The way that you obtain the command interpreter in RegBack.vbs is by using a well-known system variable called %comspec%. If you are in doubt as to the value of %comspec% on your computer, open a system prompt and type the following:

Echo %comspec%

If you are running on a Windows Server 2003, Windows 2000, or Windows XP machine, the value returned is C:\WINDOWS\system32\cmd.exe.

Defining the Command

When you use the Exec method of WshShell, the command to be executed is placed inside the quotation marks. Because Reg.exe is a command-line program in the preceding code, there really was no need to include %comspec%. Our command line could have simply been the following:

objShell.Exec "reg.exe EXPORT HKLM c:\hklm.reg"

If, on the other hand, you need to use a command line for a command that is internal to the command processor (cmd.exe or command.com), such as the dir command, you need to launch a command shell interpreter, either by using the %comspec% system variable or by using Cmd.exe, as illustrated in the CmdDir.vbs script, which follows. If you run the CmdDir.vbs script, keep in mind it could take a minute or two before it returns any information. The CmdDir.vbs script will find all files that end with the extension of .dat. Most likely, it will fine ntuser.dat, which is the current user profile setting. The issue of when to supply a command processor name and when not to can at times be a bit confusing.

CmdDir.vbs

Option Explicit
Dim objShell
Dim objExec
Dim strLine
Dim dirTxt
Dim dirFile

dirFile = "ntuser.dat"
WScript.Echo("beginning " & Now)
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec("%comspec% /c dir /aH c:\*.dat /s")

Do Until objExec.StdOut.AtEndOfStream
  strLine = objExec.StdOut.ReadLine()
  dirTxt = Instr(strLine,dirFile)
  If dirTxt <> 0 Then
    WScript.Echo strLine
  End If
Loop
WScript.Echo("all done " & Now)

Tip

With the WshShell Exec method, everything inside the outer quotation marks is executed. One quick way to make sure that you are getting the results you want and that the code is running properly is to paste your executable code into a Start\Run dialog box. This approach will not work, however, if you are using embedded quotes in strings. In this case, it is better to use WScript.Echo to echo out the value of your variable, enabling you to ensure you are sending the correct commands to VBScript.



Previous Page
Next Page