Wscript.shell对象手册分类:程序 ProgID Wscript.Shell 文件名 WSHom.Ocx CLSID F935DC22-1CF0-11d0-ADB9-00C04FD58A0B IID F935DC21-1CF0-11d0-ADB9-00C04FD58A0B 下表说明和 WshShell 对象有关的属性。
下表说明和 WshShell 对象有关的方法。
WshShell.Environment Environment 属性返回 WshEnvironment 对象。 语法 WshShell.Environment ( [strType]) = objWshEnvironment 注释 若 strType 指定了环境变量所处的位置,可能值为 "System"、"User"、"Volatile" 和 "Process"。若未提供 strType,则该方法在 Windows NT 中检索系统环境变量或在 Windows 95 中检索进程环境变量。 对于 Windows 95,strType 参数仅支持 "Process"。 下列变量是由 Windows 操作系统提供的。脚本也可获取由其他应用程序设置的环境变量。
示例 '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") 请参阅WshEnvironment对象 WshEnvironment 对象
WshEnvironment 对象未直接给出,可用 WshShell.Environment 属性来访问。 ProgID N/A 文件名 WSHom.Ocx CLSID IID 下表描述与 WshEnvironment 对象关联的属性
下表描述与 WshEnvironment 对象关联的方法。
WshShell.SpecialFolders SpecialFolders 属性提供 WshSpecialFolders 对象以便访问 Windows 的 shell 文件夹,例如桌面文件夹、开始菜单文件夹和个人文档文件夹。 语法 WshShell.SpecialFolders = objWshSpecialFolders 示例 ' This code fragment shows how to access the desktop folder Set WshShell = Wscript.CreateObject("Wscript.Shell") MsgBox "Your desktop is " & WshShell.SpecialFolders("Desktop") 请参阅WshSpecialFolders 对象 WshSpecialFolders 对象 该对象未直接给出。要得到 WshSpecialFolders 对象,请使用 WshShell.SpecialFolders 属性。 ProgID N/A 文件名 WSHom.Ocx CLSID IID 下表描述与 WshSpecialFolders 对象关联的属性。
WshSpecialFolders.Item Item 属性返回由 strFolderName 指定的文件夹的完整路径。它是默认属性。 语法 WshShell.SpecialFolders.Item("strFolderName") = strFolderPath WshShell.SpecialFolders("strFolderName") = strFolderPath 注释 若请求的文件夹 (strFolderName) 不可用,则 WshShell.SpecialFolders("strFolderName") 返回 NULL。例如,Windows 95 没有 AllUsersDesktop 文件夹,如果 strFolderName = AllUsersDesktop,则返回 NULL。 Windows 95 和 Windows NT 4.0 操作系统提供下列指定文件夹: AllUsersDesktop AllUsersStartMenu AllUsersPrograms AllUsersStartup Desktop Favorites Fonts MyDocuments NetHood PrintHoodPrograms Recent SendTo StartMenu Startup Templates 示例 ' 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 请参阅WshShell.SpecialFolders 属性 WshShell.CreateShortcut CreateShortcut 方法创建 WshShortcut 对象并将其返回。如果快捷方式标题以 .url 结尾,就会创建 WshURLShortcut 对象。 语法 WshShell.CreateShortcut(strPathname) = objShortcut 示例 ' 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 Site.URL") oUrlLink.TargetPath = "http://www.microsoft.com" oUrlLink.Save 请参阅WshShortcut 对象、WshUrlShortcut 对象 WshShortcut 对象 该对象未直接给出。要获得 WshShortcut 对象,请使用 WshShell.CreateShortcut 方法。 ProgID N/A 文件名 WSHom.Ocx CLSID F935DC28-1CF0-11d0-ADB9-00C04FD58A0B IID F935DC27-1CF0-11d0-ADB9-00C04FD58A0B 下表说明和 WshShortcut 对象有关的属性。
下表说明与 WshShortcut 对象有关的方法。
WshShortcut.Arguments Arguments 属性提供快捷方式对象的参数。 语法 WshShortcut.Arguments = strArguments WshShortcut.Description Description 属性提供快捷方式对象的说明。 语法 WshShortcut.Description = strDescription WshShortcut.Hotkey HotKey 属性提供快捷方式对象的热键。热键是启动或切换程序的键盘快捷方式。 语法 WshShortcut.HotKey = strHotKey 注释 strHotKey 的BNF语法如下: Hotkey ::= modifier* keyname modifier ::= "ALT+" | "CTRL+" | "SHIFT+" | "EXT+" keyname ::= "A" .. "Z" | "0".. "9" | "Back" | "Tab" | "Clear" | "Return" | "Escape" | "Space" | "Prior" | ... 所有键的名称都可以在 WINUSER.H 中找到。热键不区分大小写。 热键只能激活位于 Windows 桌面或 Windows“开始”菜单的快捷方式。 Windows 资源管理器不接受 ESC、ENTER、TAB、SPACE、PRINT SCREEN 或 BACKSPACE,即使 WshShortcut.Hotkey 遵循 Win32 API 支持它们。因此,建议在快捷方式中不要用这些键。 示例 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" 请参阅WshSpecialFolders 对象 WshShortcut.IconLocation IconLocation 属性提供快捷方式对象的图标位置。图标位置的格式应为 "Path,index"。 语法 WshShortcut.IconLocation = strIconLocation WshShortcut.TargetPath TargetPath 属性提供快捷方式对象的目标路径。 语法 WshShortcut.TargetPath = strTargetPath WshShortcut.WindowStyle WindowStyle 属性提供快捷方式对象的窗口样式。 语法 WshShortcut.WindowStyle = natWindowStyle WshShortcut.WorkingDirectory WorkingDirectory 为一个快捷方式对象提供工作目录。 语法 WshShortcut.WorkingDirectory = strWorkingDirectory WshShortcut.Save Save 方法把快捷方式对象保存到由 FullName 属性指定的位置。 语法 WshShortcut.Save WshUrlShortcut 对象 该对象未直接给出。要获取 WshUrlShortcut 对象,可使用 WshShell.CreateShortcut 方法。 ProgID N/A 文件名 WSHom.Ocx CLSID IID 下表说明了和 WshUrlShortcut 对象有关的属性。
下表说明了和 WshUrlShortcut 对象有关的方法。
WshUrlShortcut.FullName FullName 属性提供快捷方式对象的完整路径。 语法 WshUrlShortcut.FullName = strFullName WshUrlShortcut.TargetPath TargetPath 属性提供快捷方式对象的目标路径。 语法 WshUrlShortcut.TargetPath = strTargetPath WshUrlShortcut.Save Save 方法保存一个快捷方式,该快捷方式指向 FullName 属性指定的位置。 语法 WshUrlShortcut.Save WshShell.ExpandEnvironmentStrings ExpandEnvironmentStrings 方法在 strString 中扩展 PROCESS 环境变量并返回结果字符串。变量被 '%' 字符括起。 环境变量不区分大小写。 语法 WshShell.ExpandEnvironmentStrings(strString) = strExpandedString 示例 MsgBox "Prompt is " & WshShell.ExpandEnviromentStrings("%PROMPT%") WshShell.Popup Popup 方法显示一个弹出式消息框窗口,消息框中包含的消息由 strText 指定。该消息框的窗口标题由 strTitle 指定。若 strTitle 省略,则窗口标题为 Windows Scripting Host。 语法 WshShell.Popup(strText, [natSecondsToWait], [strTitle], [natType]) = intButton 注释 若提供 natSecondsToWait 且其值大于零,则消息框在 natSecondsToWait 秒后关闭。 natType 的含义与其在 Win32? MessageBox 函数中相同。下表显示 natType 中的值及含义。下表中的值可以组合。
以上两个表并不涵盖 natType 的所有值。完整的列表请参阅 Win32 文档。 返回值 intButton 指示用户所单击的按扭编号。若用户在 natSecondsToWait 秒之前不单击按扭,则 intButton 设置为 -1 。
示例 Set WshShell = Wscript.CreateObject("Wscript.Shell") WshShell.Popup "Where do you want to go today?" 请参阅Wscript.Echo 方法 Wscript.Echo Echo 方法在窗口(Wscript.exe 中)或“命令提示符”窗口(Cscript.exe 中)显示参数。 参数用空格分隔。在 Cscript.exe 中,该方法在显示最后一个参数之后输出一对回车/换行(CR LF)。 语法 Wscript.Echo [anyArg...] 示例 Wscript.Echo Wscript.Echo 1, 2, 3 Wscript.Echo "Windows Scripting Host is cool." WshShell.RegDelete RegDelete 从注册表中删除名为 strName 的键或值。 语法 WshShell.RegDelete strName 参数 strName 如果 strName 以反斜杠 () 结束,则该方法删除键而不是值。 strName 参数必须以下列之一的根键名开始:
示例 Set WshShell = Wscript.CreateObject("Wscript.Shell") WshShell.RegDelete "HKCUScriptEngineValue" ' Delete value "Value" WshShell.RegDelete "HKCUScriptEngineKey" ' Delete key "Key" 请参阅WshShell.RegRead 方法、WshShell.RegWrite 方法 WshShell.RegRead RegRead 方法返回名为 strName 的注册表键或值。 语法 WshShell.RegRead(strName) = strValue 参数 strName 如果 strName 以反斜杠 () 结束,则该方法返回键,而不是值。 strName 参数必须以下列根键名开始。
注释 RegRead 方法仅支持 REG_SZ、REG_EXPAND_SZ、REG_DWORD、REG_BINARY 和 REG_MULTI_SZ 数据类型。若注册表有其他数据类型,RegRead 返回 DISP_E_TYPEMISMATCH。 示例 Set WshShell = Wscript.CreateObject("Wscript.Shell") WshShell.RegRead("HKCUScriptEngineVal") ' Read from value "Val" WshShell.RegRead("HKCUScriptEngineKey") ' Read from key "Key" 请参阅WshShell.RegDelete 方法、WshShell.RegWrite 方法 WshShell.RegWrite RegWrite 方法设置名为 strName 的注册表键或值。 语法 WshShell.RegWrite strName, anyValue, [strType] 参数 strName 若 strName 以一个反斜杠 () 结束,则该方法设置键,而不是值。 strName 参数必须以下列根键名开头。
anyValue 当 strType 为 REG_SZ 或 REG_EXPAND_SZ 时,RegWrite 方法自动将 anyValue 转换为字符串。若 strType 为 REG_DWORD,则 anyValue 被转换为整数。若 strType 为 REG_BINARY,则 anyValue 必须是一个整数。 strType RegWrite 方法支持 strType 为 REG_SZ、REG_EXPAND_SZ、REG_DWORD 和 REG_BINARY。若其他的数据类型被作为 strType 传递,RegWrite 返回 E_INVALIDARG。 示例 Set WshShell = Wscript.CreateObject("Wscript.Shell") WshShell.RegWrite "HKCUScriptEngineValue", "Some string value" WshShell.RegWrite "HKCUScriptEngineKey", 1 "REG_DWORD" 请参阅WshShell.RegDelete 方法、WshShell.RegWrite方法 WshShell.Run Run 方法创建一个新的进程,该进程以 intWindowStyle 窗口样式执行 strCommand。 语法 WshShell.Run (strCommand, [intWindowStyle], [blnWaitOnReturn]) 参数 strCommand 在 strCommand 参数内部的环境变量被自动扩展。 intWindowStyle 这是为新进程在 STARTUPINFO 结构内设置的 wShowWindow 元素的值。其意义与 ShowWindow 中的 nCmdShow 参数相同,可取以下值之一。
blnWaitOnReturn 如果未指定 blnWaitOnReturn 或其值为 FALSE,则该方法立即返回到脚本继续执行而不等待进程结束。 若 blnWaitOnReturn 设为 TRUE,则 Run 方法返回由应用程序返回的任何错误代码。如果未指定 blnWaitOnReturn 或其值为 FALSE,则 Run 返回错误代码 0(zero)。 示例 ' 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) |
|