Category:Program
Wshshell object
Progid wscript. Shell
File Name wshom. ocx
CLSID F935DC22-1CF0-11d0-ADB9-00C04FD58A0B
IID F935DC21-1CF0-11d0-ADB9-00C04FD58A0B
The following table describes the properties related to the wshshell object.
Attribute description
Environment returns the wshenvironment collection object.
Specialfolders uses the wshspecialfolders object to provide access to Windows Shell Folders, such as desktop folders, Start Menu folders, and personal document folders.
The following table describes the methods related to the wshshell object.
Method description
Createshortcut is created and the wshshortcut object is returned.
Expandenvironmentstrings extends the process environment variables and returns the result string.
Popup displays the message window containing the specified message.
Regdelete fromRegistry
To delete the specified key or value.
Regread fromRegistry
Returns the specified key or value.
Regwrite inRegistry
Set the specified key or value.
Run creates a new process and runs the specified command in the specified window style.
Wshshell. Environment
The environment attribute returns the wshenvironment object.
Syntax
Wshshell. Environment ([strtype]) = ob1_shenvironment
Note
If strtype specifies the location of the environment variable, the values may be "system", "user", "volatile", and "process ". If strtype is not provided, this method retrieves system environment variables in Windows NT or process environment variables in Windows 95.
For Windows 95, the strtype parameter only supports "process ".
The following variables are provided by the Windows operating system. The script can also obtain environment variables set by other applications.
Description
Number_of_processors: Number of processors running on the computer.
Processor_architecture the type of processor used by the user workstation.
Processor_identifier the processor ID of the user workstation.
Processor_level the processor level of the user workstation.
Processor_revision the processor version of the user workstation.
The operating system used by the OS user workstation.
Comspec is the command used to run the "command prompt" window (usually cmd.exe ).
Homedrive local master drive (usually C drive ).
The default path of the homepath user (usually usersdefault on Windows NT ).
PATH environment variable.
The extension of the pathext executable file (usually. com,. EXE,. bat, or. cmd ).
Prompt command prompt (usually $ p $ g ).
The local drive where the systemdrive system is located (for example, C :).
Systemroot system directory (for example, C: WINNT ). Same as WINDIR.
WINDIR system directory (for example, C: WINNT ). Same as systemroot.
Temp stores the directory of temporary files (for example, C: temp ). Users can change the value.
Tmp directory for storing temporary files (for example, C: temp ). Users can change the value.
Example
''Retrieve the number_of_processors system environment variable
Set wshshell = wscript. Createobject ("wscript. Shell ")
Set wshsysenv = wshshell. Environment ("system ")
Wscript. Echo wshsysenv ("number_of_processors ")
See
Wshenvironment object
Wshenvironment object
The wshenvironment object is not provided directly and can be accessed using the wshshell. Environment attribute.
Progid N/
File Name wshom. ocx
CLSID
IID
The following table describes the attributes associated with the wshenvironment object.
Attribute description
Item.
Count refers to the number of enumerated items.
The number of length enumeration items (jscript ).
The following table describes the methods associated with the wshenvironment object.
Method description
Remove deletes the specified environment variable.
Wshshell. specialfolders
The specialfolders property provides the wshspecialfolders object to Access Windows Shell Folders, such as desktop folders, Start Menu folders, and personal document folders.
Syntax
Wshshell. specialfolders = obw.shspecialfolders
Example
''This code fragment shows howAccess
The Desktop Folder
Set wshshell = wscript. Createobject ("wscript. Shell ")
Msgbox "your desktop is" & wshshell. specialfolders ("desktop ")
See
Wshspecialfolders object
Wshspecialfolders object
This object is not provided directly. To obtain the wshspecialfolders object, use the wshshell. specialfolders attribute.
Progid N/
File Name wshom. ocx
CLSID
IID
The following table describes the attributes associated with the wshspecialfolders object.
Attribute description
Item specifies the full path of the folder (default ).
Count refers to the number of enumerated items.
The number of length enumeration items (jscript ).
Wshspecialfolders. Item
The full path of the folder specified by strfoldername is returned. It is the default property.
Syntax
Wshshell. specialfolders. Item ("strfoldername") = strfolderpath
Wshshell. specialfolders ("strfoldername") = strfolderpath
Note
If the requested folder (strfoldername) is unavailable, wshshell. specialfolders ("strfoldername") returns NULL. For example, Windows 95 does not have the allusersdesktop folder. If strfoldername = allusersdesktop, null is returned.
Windows 95 and Windows NT 4.0 provide the following specified folders:
Allusersdesktop
Allusersstartmenu
Allusersprograms
Allusersstartup
Desktop
Favorites
Fonts
Mydocuments
Nethood
Printhood
Programs
Recent
Sendto
Startmenu
Startup
Templates
Example
''This fragment returns the full path for the Windows Desktop Folder
Set wshshell = wscript. Createobject ("wscript. Shell ")
Strmydesktop = wshshell. specialfolders ("desktop ")
''List all special folders
For each strfolder in wshshell. specialfolders
Msgbox strfolder
Next
See
Wshshell. specialfolders attributes
wshshell. createshortcut
the createshortcut method creates a wshshortcut object and returns it. If the shortcut title ends with a. URL, the wshurlshortcut object is created.
syntax
wshshell. createshortcut (strpathname) = objshortcut
example
''this code fragment creates a shortcut
''to the currently executing script
set wshshell = wscript. createobject ("wscript. shell ")
set oshelllink = wshshell. createshortcut ("Current script. lnk ")
oshelllink. targetpath = wscript. scriptfullname
oshelllink. save
set ourllink = wshshell. createshortcut ("Microsoft Web s ITE. URL ")
ourllink. targetpath = "http://www.microsoft.com"
ourllink. save
See
wshshortcut object and wshurlshortcut object
wshshortcut object
This object is not provided directly. To obtain the wshshortcut object, use the wshshell. createshortcut method.
progid N/A
file name wshom. ocx
CLSID F935DC28-1CF0-11d0-ADB9-00C04FD58A0B
IID F935DC27-1CF0-11d0-ADB9-00C04FD58A0B
The following table describes the properties related to the wshshortcut object.
Attribute description
Parameters of the arguments shortcut object.
Description Description of the shortcut object.
The hotkey of the shortcut object.
The icon position of the iconlocation shortcut object.
The target path of the targetpath shortcut object.
Window style of the windowstyle shortcut object.
The working directory of the workingdirectory shortcut object.
The following table describes the methods related to the wshshortcut object.
Method description
Save saves shortcuts to the specified file system.
Wshshortcut. Arguments
The arguments attribute provides the parameters of the shortcut object.
Syntax
Wshshortcut. Arguments = strarguments
Wshshortcut. Description
The description attribute describes the shortcut objects.
Syntax
Wshshortcut. Description = strdescription
Wshshortcut. hotkey
The hotkey attribute provides the hotkey of the shortcut object. The hotkey is the keyboard shortcut for starting or Switching programs.
Syntax
Wshshortcut. hotkey = strhotkey
Note
The strhotkey BNF syntax is as follows:
Hotkey: = modifier * keyname
Modifier: = "Alt +" | "Ctrl +" | "Shift +" | "ext +"
Keyname: = "A" .. "Z" |
"0"... "9" |
"Back" | "tab" | "clear" | "return" |
"Escape" | "space" | "prior" |...
The names of all keys can be found in winuser. h. The hotkeys are case insensitive.
The hotkey can only activate shortcuts on Windows desktop or Windows Start Menu.
Windows resource manager does not accept ESC, enter, tab, space, print screen, or backspace, even if wshshortcut. hotkey follows Win32 API support. Therefore, we recommend that you do not use these keys in shortcuts.
Example
Set wshshell = wscript. Createobject ("wscript. wshshell ")
Strdesktop = wshshell. specialfolders ("desktop ")
Set omyshortcut = wshshell. createshortcut (strdesktop & "a_key.lnk ")
Omyshortcut. targetpath = "%windir#notepad.exe"
Omyshortcut. hotkey = "Alt + Ctrl + F"
Omyshortcut. Save
Wscript. Echo omyshortcut. hotkey = "Alt + Ctrl + F"
See
Wshspecialfolders object
Wshshortcut. iconlocation
The iconlocation attribute provides the icon location of the shortcut object. The format of the icon position should be "path, index ".
Syntax
Wshshortcut. iconlocation = striconlocation
Wshshortcut. targetpath
The targetpath attribute provides the target path of the shortcut object.
Syntax
Wshshortcut. targetpath = strtargetpath
Wshshortcut. windowstyle
The windowstyle attribute provides the window style of the shortcut object.
Syntax
Wshshortcut. windowstyle = natwindowstyle
Wshshortcut. workingdirectory
Workingdirectory provides a working directory for a shortcut object.
Syntax
Wshshortcut. workingdirectory = strworkingdirectory
Wshshortcut. Save
The Save method saves the shortcut object to the location specified by the fullname attribute.
Syntax
Wshshortcut. Save
Wshurlshortcut object
This object is not provided directly. To obtain the wshurlshortcut object, you can use the wshshell. createshortcut method.
Progid N/
File Name wshom. ocx
CLSID
IID
The following table describes the properties related to the wshurlshortcut object.
Attribute description
Full path of the fullname URL Shortcut object.
The target path of the targetpath URL Shortcut object.
The following table describes the methods related to the wshurlshortcut object.
Method description
Save saves the shortcut to the specified file system.
Wshurlshortcut. fullname
The fullname attribute provides the complete path of the shortcut object.
Syntax
Wshurlshortcut. fullname = strfullname
Wshurlshortcut. targetpath
The targetpath attribute provides the target path of the shortcut object.
Syntax
Wshurlshortcut. targetpath = strtargetpath
Wshurlshortcut. Save
The Save method saves a shortcut that points to the location specified by the fullname attribute.
Syntax
Wshurlshortcut. Save
Wshshell. expandenvironmentstrings
The expandenvironmentstrings method extends the process environment variable in strstring and returns the result string. The variable is enclosed by the '%' character.
Environment variables are case insensitive.
Syntax
Wshshell. expandenvironmentstrings (strstring) = strexpandedstring
Example
Msgbox "prompt is" & wshshell. expandenviromentstrings ("% prompt % ")
Wshshell. Popup
The popup method displays a pop-up message box window. The messages contained in the message box are specified by strtext. The title of the message box is specified by strtitle. If strtitle is omitted, the window title is Windows Scripting host.
Syntax
Wshshell. Popup (strtext, [natsecondstowait], [strtitle], [nattype]) = intbutton
Note
If natsecondstowait is provided and its value is greater than zero, the message box is closed after natsecondstowait seconds.
What is nattype in Win32? The MessageBox function is the same. The following table lists the values and meanings of nattype. The values in the following table can be combined.
Button type
Value description
0. The "OK" button is displayed.
1. The "OK" and "cancel" buttons are displayed.
2. The "Terminate", "retry", and "Ignore" buttons are displayed.
3. the "yes", "no", and "cancel" buttons are displayed.
4. the "yes" and "no" buttons are displayed.
5. The "retry" and "cancel" buttons are displayed.
Icon type
Value description
16 show the stop icon
32 display question mark icon
48 display exclamation point icon
64 Display Information Mark icon
The above two tables do not cover all nattype values. For a complete list, see the Win32 documentation.
The returned value intbutton indicates the number of the buttons clicked by the user. If you do not click the button before natsecondstowait seconds, set intbutton to-1.
Value description
1 "OK" button
2 "cancel" button
3 "stop" button
4 "retry" button
5 "Ignore" button
6 "yes" button
7 "no" button
example
set wshshell = wscript. Createobject ("wscript. Shell")
wshshell. Popup "Where do you want to go today? "
See
wscript. Echo method
wscript. Echo
. Echo parameters are displayed in the window (in wscript.exe, the escape command prompt dialog box (in cscript.exe.
parameters are separated by spaces. In cscript.exe, this method outputs a carriage return/line feed (cr lf) after the last parameter is displayed ).
syntax
wscript. echo [anyarg...]
example
wscript. echo
wscript. echo 1, 2, 3
wscript. echo "Windows Scripting host is cool. "
wshshell. regdelete
regdelete deletes a key or value named strname from registry
.
syntax
wshshell. regdelete strname
parameter
strname
If strname ends with a backslash (), this method deletes the key instead of the value.
the strname parameter must start with the root key name of one of the following columns:
short root key name long root key name
hkcu HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
hkcr hkey_classes_root
HKEY_USERS
hkey_current_config
Example
Set wshshell = wscript. Createobject ("wscript. Shell ")
Wshshell. regdelete "hkcuscriptenginevalue" ''delete value "value"
Wshshell. regdelete "hkcuscriptenginekey" ''delete key "key"
See
Wshshell. regread method, wshshell. regwrite Method
Wshshell. regread
The regread method returns the strnameRegistry
Key or value.
Syntax
Wshshell. regread (strname) = strvalue
Parameters
Strname
If strname ends with a backslash (), this method returns the key instead of the value.
The strname parameter must start with the root key name of the following column.
Short long
Hkcu HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
Hkcr hkey_classes_root
HKEY_USERS
Hkey_current_config
Note
The regread method only supports the REG_SZ, reg_expand_sz, REG_DWORD, REG_BINARY, and reg_multi_sz data types. IfRegistry
If there are other data types, regread returns disp_e_typemismatch.
Example
Set wshshell = wscript. Createobject ("wscript. Shell ")
Wshshell. regread ("hkcuscriptengineval") ''read from Value" Val"
Wshshell. regread ("hkcuscriptenginekey") ''read from key" key"
See
Wshshell. regdelete method, wshshell. regwrite Method
Wshshell. regwrite
The regwrite method is set to strnameRegistry
Key or value.
Syntax
Wshshell. regwrite strname, anyvalue, [strtype]
Parameters
Strname
If strname ends with a backslash (), this method sets the key instead of the value.
The strname parameter must start with the root key of the following column.
Short long
Hkcu HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
Hkcr hkey_classes_root
HKEY_USERS
Hkey_current_config
anyvalue
when strtype is REG_SZ or reg_expand_sz, The regwrite method automatically converts anyvalue to a string. If strtype is REG_DWORD, anyvalue is converted to an integer. If strtype is REG_BINARY, anyvalue must be an integer.
strtype
the regwrite method supports the following strtypes: REG_SZ, reg_expand_sz, REG_DWORD, and REG_BINARY. If other data types are passed as strtype, regwrite returns e_invalidarg.
example
set wshshell = wscript. createobject ("wscript. shell ")
wshshell. regwrite "hkcuscriptenginevalue", "some string value"
wshshell. regwrite "hkcuscriptenginekey", 1 "REG_DWORD"
See
wshshell. regdelete method, wshshell. regwrite method
wshshell. Run
the run method creates a new process that runs strcommand in the intwindowstyle window.
syntax
wshshell. run (strcommand, [intwindowstyle], [blnwaitonreturn])
parameter
strcommand
the environment variables inside the strcommand parameter are automatically extended.
intwindowstyle
This is the value of the wshowwindow element set in the startupinfo structure for the new process. The meaning is the same as that of the ncmdshow parameter in showwindow. One of the following values is recommended.
sw_hide
0. Hide the window and activate another window.
sw_minimize
6. Minimize the specified window and activate the next top-level window sorted in Z order.
sw_restore
9 activation and display window. If the window is minimized or maximized, It is restored to the original size and position. Specify this flag when restoring the minimal window of the application.
sw_show
5. Activate and display the window in the current size and position.
sw_showmaximized
3. Activate the window and maximize the display.
sw_showminimized
2. Activate the window and minimize the display.
sw_showminnoactive
7. Minimize the display window. The activity window maintains the activity.
sw_showna
8 shows the window in the current status. The activity window maintains the activity.
sw_shownoactivate
4. display by the size and position closest to the window. The activity window maintains the activity.
sw_shownormal
1. Activate and display a window. If the window is minimized or maximized, It is restored to its original size and position.
blnwaitonreturn
If blnwaitonreturn is not specified or its value is false, this method returns immediately to the script for further execution without waiting for the process to end.
If blnwaitonreturn is set to true, the run method returns any error returned by the application Code . If blnwaitonreturn is not specified or its value is false, run Returns Error code 0 (zero ).
example
''this fragment launches notepad with the current executed script
set wshshell = wscript. createobject ("wscript. shell ")
wshshell. run ("Notepad" & wscript. scriptfullname)
wshshell. run ("% WINDIR % Notepad" & wscript. scriptfullname)
''this fragment returns the error code from the executed application
return = wshshell. run ("Notepad" & wscript. scriptfullname, 1, true)