9.8. Shell Object
The WSH Shell object
provides the ability to create Windows shortcuts, read environment
variables, manipulate registry settings, and run external programs.To create an instance of the WSH Shell object,
pass the argument Wscript.Shell to the
Wscript.CreateObject method:
Set objShell = Wscript.CreateObject("Wscript.Shell")
9.8.1. ExpandEnvironmentVariables Method
Environment variables are information
stored by the Windows operating system. You can list the environment
variables currently set by executing the DOS Set
command from the command prompt. You can interpolate their values
into your script using the
ExpandEnvironmentVariables method of a
Shell object. Its syntax is:
strValue = objShell.ExpandEnvironmentVariables(strString)Any strings in the strString parameter
that are enclosed with % symbols will be expanded
using the corresponding environment variable value.
Set objShell = Wscript.CreateObject("Wscript.shell")
Wscript.Echo _
objShell.ExpandEnvironmentStrings( _
"Your temp directory is %TEMP%")
9.8.2. Run Method
The Run method executes an external program.
This can be any Windows executable or command-line program. If you
don't specify an explicit path to the application,
the Run method will search the paths specified in
the PATH environment variable. The following example executes
Notepad:
Set objShell = Wscript.CreateObject("Wscript.shell")
objShell.Run ("Notepad.exe")
9.8.3. SpecialFolders Collection
The
SpecialFolders collection returns the path
to a specified system folder:
strPath = objShell.SpecialFolders(strFolderName)The strFolderName parameter can be any one
of the following values: Desktop,
Favorites, Fonts,
MyDocuments, NetHood,
PrintHood, Programs,
Recent, SendTo,
StartMenu, Startup, and
Templates.
Set objShell = Wscript.CreateObject("Wscript.shell")
strDesktop = objShell.SpecialFolders("Desktop")