-
VbsSample(01)
2008-07-23 09:32:00
'*********************************
'@funcname: ProcessExist
'@brief: 判定进程列表中是否有某个进程
'@param: sProcessName 进程名
'*********************************
Function ProcessExist(sProcessName)
Dim ws
ProcessExist = False
Set ws = Wscrīpt.CreateObject("wscrīpt.shell")
For each proc in GetObject("winmgmts:").instancesof("win32_process")
If UCase(proc.Name) = UCase(sProcessName) Then
ProcessExist = True
Exit For
End If
Next
Set ws = Nothing
End Function'*********************************
'@funcname: SendKey
'@brief: 向系统发送键盘键
'@param: nKey 键盘按键
'*********************************
Function SendKey(nKey)
Dim oWshShell
Set ōWshShell =CreateObject("Wscrīpt.Shell")
oWshShell.SendKeys nKey
Wait(1)
Set ōWshShell = Nothing
End Function'*********************************
'@funcname: ReadReg
'@brief:读取注册表的某一键值
'@param: sRegPath 注册表路径
'*********************************
Function ReadReg(sRegPath)
Dim oRegistry
Set ōRegistry = CreateObject("Wscrīpt.Shell")
ReadReg=oRegistry.RegRead(sRegPath)
Set ōRegistry = Nothing
End Function -
禁用&启动网卡
2008-07-23 09:31:16
Const ssfCONTROLS = 3
sConnectionName = "本地连接" '可改成需要控制的连接名称,如"无线网络连接"等
sEnableVerb = "启用(&A)"
sDisableVerb = "停用(&B)" '2003系统中应为 "禁用(&B)"
set shellApp = createobject("shell.application")
set ōControlPanel = shellApp.Namespace(ssfCONTROLS)
set ōNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = "网络连接" then
set ōNetConnections = folderitem.getfolder: exit for
end if
next
if oNetConnections is nothing then
msgbox "未找到网络连接文件夹"
wscrīpt.quit
end if
set ōLanConnection = nothing
for each folderitem in oNetConnections.items
if lcase(folderitem.name) = lcase(sConnectionName) then
set ōLanConnection = folderitem: exit for
end if
next
if oLanConnection is nothing then
msgbox "未找到 '" & sConnectionName & "' item"
wscrīpt.quit
end if
bEnabled = true
set ōEnableVerb = nothing
set ōDisableVerb = nothing
s = "Verbs: " & vbcrlf
for each verb in oLanConnection.verbs
s = s & vbcrlf & verb.name
if verb.name = sEnableVerb then
set ōEnableVerb = verb
bEnabled = false
end if
if verb.name = sDisableVerb then
set ōDisableVerb = verb
end if
next
if bEnabled then
oDisableVerb.DoIt
else
oEnableVerb.DoIt
end if
wscrīpt.sleep 400 -
Excel相关操作
2008-07-23 09:30:35
直接调用Excel中的函数的方法,示例如下:
源码:
close;
<%
Set ōExcel= CreateObject("excel.application")
oExcel.Visible = True 'Excel是否可视,若赋值为True则可视,但速度会慢得多
oExcel.Workbo感谢对我们的支持~s.Add
Set ōSheet = oExcel.Workbo感谢对我们的支持~s(1).Worksheets("sheet1")
oSheet.Activate
oSheet.Cells(1, 1) = 1
oSheet.Cells(1, 2) = 2
oSheet.Cells(1, 3) = "=SUM(A1:B1)"
oSheet.Cells(1, 4) = "=AVERAGE(A1:B1)"
Set ōExcel = Nothing%>
VBS控制Excel的一些常见方法:(一) 使用动态创建的方法
'首先创建 Excel 对象,使用ComObj:
oExcel = CreateObject( "Excel.Application" )1) 显示当前窗口:
oExcel.Visible = True2) 更改 Excel 标题栏:
oExcel.Caption = "应用程序调用 Microsoft Excel"3) 添加新工作簿:
oExcel.WorkBo感谢对我们的支持~s.Add4) 打开已存在的工作簿:
oExcel.WorkBo感谢对我们的支持~s.Open( "C:\Excel\Demo.xls" )5) 设置第2个工作表为活动工作表:
oExcel.WorkSheets(2).Activate
或
oExcel.WorksSheets( "Sheet2" ).Activate6) 给单元格赋值:
oExcel.Cells(1,4).Value = "第一行第四列"7) 设置指定列的宽度(单位:字符个数),以第一列为例:
oExcel.ActiveSheet.Columns(1).ColumnsWidth = 58) 设置指定行的高度(单位:磅)(1磅=0.035厘米),以第二行为例:
oExcel.ActiveSheet.Rows(2).RowHeight = 1/0.035 ' 1厘米9) 在第8行之前插入分页符:
oExcel.WorkSheets(1).Rows(8).PageBreak = 110) 在第8列之前删除分页符:
oExcel.ActiveSheet.Columns(4).PageBreak = 011) 指定边框线宽度:
oExcel.ActiveSheet.Range( "B3:D4" ).Borders(2).Weight = 3
1-左 2-右 3-顶 4-底 5-斜( \ ) 6-斜( / )12) 清除第一行第四列单元格公式:
oExcel.ActiveSheet.Cells(1,4).ClearContents13) 设置第一行字体属性:
oExcel.ActiveSheet.Rows(1).Font.Name = "隶书"
oExcel.ActiveSheet.Rows(1).Font.Color = clBlue
oExcel.ActiveSheet.Rows(1).Font.Bold = True
oExcel.ActiveSheet.Rows(1).Font.UnderLine = True14) 进行页面设置:
a.页眉:
oExcel.ActiveSheet.PageSetup.CenterHeader = "报表演示"
b.页脚:
oExcel.ActiveSheet.PageSetup.CenterFooter = "第&P页"
c.页眉到顶端边距2cm:
oExcel.ActiveSheet.PageSetup.HeaderMargin = 2/0.035
d.页脚到底端边距3cm:
oExcel.ActiveSheet.PageSetup.HeaderMargin = 3/0.035
e.顶边距2cm:
oExcel.ActiveSheet.PageSetup.TopMargin = 2/0.035
f.底边距2cm:
oExcel.ActiveSheet.PageSetup.BottomMargin = 2/0.035
g.左边距2cm:
oExcel.ActiveSheet.PageSetup.LeftMargin = 2/0.035
h.右边距2cm:
oExcel.ActiveSheet.PageSetup.RightMargin = 2/0.035
i.页面水平居中:
oExcel.ActiveSheet.PageSetup.CenterHorizontally = 2/0.035
j.页面垂直居中:
oExcel.ActiveSheet.PageSetup.CenterVertically = 2/0.035
k.打印单元格网线:
oExcel.ActiveSheet.PageSetup.PrintGridLines = True15) 拷贝操作:
a.拷贝整个工作表:
oExcel.ActiveSheet.Used.Range.Copy
b.拷贝指定区域:
oExcel.ActiveSheet.Range( "A1:E2" ).Copy
c.从A1位置开始粘贴:
oExcel.ActiveSheet.Range.( "A1" ).PasteSpecial
d.从文件尾部开始粘贴:
oExcel.ActiveSheet.Range.PasteSpecial
e.复制
oExcel.cells(1,2).copy
f.粘贴
oExcel.cells(1,4).PasteSpecial16) 插入一行或一列:
a. oExcel.ActiveSheet.Rows(2).Insert
b. oExcel.ActiveSheet.Columns(1).Insert17) 删除一行或一列:
a. oExcel.ActiveSheet.Rows(2).Delete
b. oExcel.ActiveSheet.Columns(1).Delete18) 打印预览工作表:
oExcel.ActiveSheet.PrintPreview19) 打印输出工作表:
oExcel.ActiveSheet.PrintOut20) 工作表保存:
if not oExcel.ActiveWorkBo感谢对我们的支持~.Saved then
oExcel.ActiveSheet.PrintPreview21) 工作表另存为:
oExcel.SaveAs( "C:\Excel\Demo1.xls" )22) 放弃存盘:
oExcel.ActiveWorkBo感谢对我们的支持~.Saved = True23) 关闭工作簿:
oExcel.WorkBo感谢对我们的支持~s.Close24) 退出 Excel:
oExcel.Quit(二) 使用VBS 控制Excle二维图
1)选择当第一个工作薄第一个工作表
set ōSheet=oExcel.Workbo感谢对我们的支持~s(1).Worksheets(1)2)增加一个二维图
achart=oSheet.chartobjects.add(100,100,200,200)3)选择二维图的形态
achart.chart.charttype=44)给二维图赋值
set series=achart.chart.seriescollection
range="sheet1!r2c3:r3c9"
series.add range,true5)加上二维图的标题
achart.Chart.HasTitle=True
achart.Chart.ChartTitle.Characters.Text=" Excle二维图"6)改变二维图的标题字体大小
achart.Chart.ChartTitle.Font.size=187)给二维图加下标说明
achart.Chart.Axes(xlCategory, xlPrimary).HasTitle = True
achart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "下标说明"8)给二维图加左标说明
achart.Chart.Axes(xlValue, xlPrimary).HasTitle = True
achart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "左标说明"9)给二维图加右标说明
achart.Chart.Axes(xlValue, xlSecondary).HasTitle = True
achart.Chart.Axes(xlValue, xlSecondary).AxisTitle.Characters.Text = "右标说明"10)改变二维图的显示区大小
achart.Chart.PlotArea.Left = 5
achart.Chart.PlotArea.Width = 223
achart.Chart.PlotArea.Height = 108(三) 应用实例
1)批量转换某文件夹下的Excel成Txt
Dim gFileName
Dim strPath
Dim strFolder
Dim xlApp
Dim fso, f, f1, fc, s
Dim i, j
Dim arryFiles()
Dim moveFilePath
i = 0Set xlApp = CreateObject("excel.application")
Set ōbjShell = CreateObject("Wscrīpt.Shell")
strFolder = Wscrīpt.scrīptFullName '获取文件所驻留的文件夹的完整路径
Set fso = CreateObject("scrīpting.FileSystemObject")
strPath = strFolder & "\..\Res\TestResult\"Set f = fso.GetFolder(strPath)
Set fc = f.Files
Redim arryFiles(fc.count)
For Each f1 in fc
s = strPath & f1.Name
arryFiles(i) = s
i = i + 1
NextFor j = 0 to i - 1
xlApp.Application.Workbooks.Open arryFiles(j)
xlApp.Application.ActiveSheet.Rows(1).Delete
xlApp.Application.Workbooks.Item(1).SaveAs arryFiles(j) & ".txt",42
xlApp.Application.Workbooks.Item(1).Close 0
Next -
VBScript中SendKeys的妙用
2007-08-26 02:42:53
【转载自(sidneylover)的空间】
什么是VBscrīpt呢?
VBscrīpt的全称是:Microsoft Visual Basic scrīpt Editon.(微软公司可视化BASIC脚本版). VBS(VBscrīpt的进一步简写)是基于Visual Basic的脚本语言,其不编译成二进制文件, 而是直接由宿主(host)解释源代码并执行, 简单点说就是你写的程序不需要编译成.exe, 而是直接给用户发送.vbs的源程序, 用户就能执行了.
SendKeys 方法
模拟键盘操作,将一个或多个按键指令发送到指定Windows窗口来控制应用程序运行,
其使用格式为:object.SendKeys(string)
“object”:表示WshShell对象
“string”:表示要发送的按键指令字符串,需要放在英文双引号中。
-----------------------------------
1.基本键
一般来说,要发送的按键指令都可以直接用该按键字符本身来表示,例如要发送字母“x”,使用“WshShell.SendKeys "x"”即可。当然,也可直接发送多个按键指令,只需要将按键字符按顺序排列在一起即可,例如,要发送按键“happy”,可以使用“ WshShell.SendKeys "happy" ”。
2.特殊功能键
对于需要与Shift、Ctrl、Alt三个控制键组合的按键,SendKeys使用特殊字符来表示:
Shift---------WshShell.SendKeys "+"
Ctrl---------WshShell.SendKeys "^"
Alt---------WshShell.SendKeys "%" (注意 这样使用时,不用大括号括起这些特殊字符。)
由于“+”、“^”这些字符用来表示特殊的控制按键了,如何表示这些按键呢? 只要用大括号括住这些字符即可。例如: 要发送加号“+”,可使用“WshShell.SendKeys "{+}"”
另外对于一些不会生成字符的控制功能按键,也同样需要使用大括号括起来按键的名称。
例如要发送回车键,需要用“ WshShell.SendKeys "{ENTER}" ”表示;
发送向下的方向键用“ Wshell.SendKeys "{DOWN}" ”表示。
Space---------WshShell.SendKeys " "
Enter---------WshShell.SendKeys "{ENTER}"
←---------WshShell.SendKeys "{RIGHT}"
↑---------WshShell.SendKeys "{UP}"
F1---------WshShell.SendKeys "{F1}"
按键 代码
BACKSPACE {BACKSPACE}, {BS}, 或 {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or Delete {Delete} 或 {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER}或 ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or Insert {Insert} 或 {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
Tips:如果需要发送多个重复的单字母按键,不必重复输入该字母,SendKeys允许使用简化格式进行描述,使用格式为“{按键 数字}”。例如要发送10个字母“x”,则输入“WshShell.SendKeys "{x 10}"”即可。
-----------------------------------
按下F5刷新桌面
Dim WshShell,Path,i
Set WshShell = Wscrīpt.CreateObject("Wscrīpt.Shell")
WshShell.SendKeys "{F5}"
----------------------------------------------------
电脑的自动重启
set WshShell = CreateObject("Wscrīpt.Shell")
WshShell.SendKeys "^{ESC}u"
WshShell.SendKeys "R"
----------------------------------------------------
启动任务管理器
set WshShell = CreateObject("Wscrīpt.Shell")
WshShell.SendKeys "^+{ESC}"
----------------------------------------------------
QQ消息连发
Dim WshShell
Set WshShell= Wscrīpt.createObject("Wscrīpt.Shell")
WshShell.AppActivate "bomb"
for i=1 to 60
Wscrīpt.Sleep 800
WshShell.SendKeys "What do you say"
WshShell.SendKeys i
WshShell.SendKeys "%s"
next
----------------------------------------------------
自动到百度搜索歌曲:white flag
Dim WshShell,Path,i
Set WshShell = Wscrīpt.CreateObject("Wscrīpt.Shell")
WshShell.Run("IEXPLORE.EXE")
Wscrīpt.Sleep 2000
WshShell.AppActivate "about:blank-Microsoft Internet Explorer"
WshShell.SendKeys "+{TAB}"
WshShell.SendKeys "http://mp3.baidu.com"
Wscrīpt.Sleep 800
WshShell.SendKeys "{ENTER}"
Wscrīpt.Sleep 3000
WshShell.SendKeys "white flag"
Wscrīpt.Sleep 800
WshShell.SendKeys "{ENTER}"
----------------------------------------------------
自动关机
Dim WshShell
Set WshShell=Wscrīpt.CreateObject("Wscrīpt.Shell")
Wscrīpt.Sleep 2000
WshShell.Run "shutdown -r -t 120"
wscrīpt.sleep 6000
WshShell.Run "shutdown -a"
----------------------------------------------------
在记事本中输入Happy Birthday!并保存为birth.txt
Dim WshShell
Set WshShell=Wscrīpt.CreateObject("Wscrīpt.Shell")
WshShell.Run "notepad"
Wscrīpt.Sleep 1500
WshShell.AppActivate "无标题 - 记事本"
WshShell.SendKeys "H"
Wscrīpt.Sleep 500
WshShell.SendKeys "a"
Wscrīpt.Sleep 500
WshShell.SendKeys "p"
Wscrīpt.Sleep 500
WshShell.SendKeys "p"
Wscrīpt.Sleep 500
WshShell.SendKeys "y"
Wscrīpt.Sleep 500
WshShell.SendKeys " "
Wscrīpt.Sleep 500
WshShell.SendKeys "B"
Wscrīpt.Sleep 500
WshShell.SendKeys "i"
Wscrīpt.Sleep 500
WshShell.SendKeys "r"
Wscrīpt.Sleep 500
WshShell.SendKeys "t"
Wscrīpt.Sleep 500
WshShell.SendKeys "h"
Wscrīpt.Sleep 500
WshShell.SendKeys "d"
Wscrīpt.Sleep 500
WshShell.SendKeys "a"
Wscrīpt.Sleep 500
WshShell.SendKeys "y"
Wscrīpt.Sleep 500
WshShell.SendKeys "!"
Wscrīpt.Sleep 500
WshShell.SendKeys "%FS"
Wscrīpt.Sleep 500
WshShell.SendKeys "b"
Wscrīpt.Sleep 500
WshShell.SendKeys "i"
Wscrīpt.Sleep 500
WshShell.SendKeys "r"
Wscrīpt.Sleep 500
WshShell.SendKeys "t"
Wscrīpt.Sleep 500
WshShell.SendKeys "h"
Wscrīpt.Sleep 500
WshShell.SendKeys "%S"
Wscrīpt.Sleep 500
WshShell.SendKeys "%FX"
----------------------------------------------------
制作能自动定时存盘的记事本
'第一部分:定义变量和对象
Dim WshShell, AutoSaveTime, TXTFileName
AutoSaveTime=300000
Set WshShell=Wscrīpt.CreateObject("Wscrīpt.Shell")
TXTFileName=InputBox("请输入你要创建的文件名(不能用中文和纯数字):")
'第二部分:打开并激活记事本
WshShell.Run "notepad"
Wscrīpt.Sleep 200
WshShell.AppActivate "无标题 - 记事本"
'第三部分:用输入的文件名存盘
WshShell.SendKeys "^s"
Wscrīpt.Sleep 300
WshShell.SendKeys TXTFileName
Wscrīpt.Sleep 300
WshShell.SendKeys "%s"
Wscrīpt.Sleep AutoSaveTime
'第四部分:自动定时存盘
While WshShell.AppActivate (TXTFileName)=True
WshShell.SendKeys "^s"
Wscrīpt.Sleep AutoSaveTime
Wend
Wscrīpt.Quit自动死机o(∩_∩)o...
DIM WSHSHELL
SET WSHSHELL=Wscrīpt.CreateOBJECT("Wscrīpt.SHELL")
'WSHSHELL.RUN " "
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"
'Wscrīpt.SLEEP 1000
WSHSHELL.SENDKEYS "{ENTER}"[/quote]
-
Wscrīpt.Shell 对象详细介绍
2007-08-26 02:39:03
【转载自(sidneylover)的空间】
Wscrīpt.Shell 对象详细介绍
WshShell 对象
ProgID Wscrīpt.Shell
文件名 WSHom.Ocx
CLSID F935DC22-1CF0-11d0-ADB9-00C04FD58A0B
IID F935DC21-1CF0-11d0-ADB9-00C04FD58A0B
下表说明和 WshShell 对象有关的属性。属性 说明
Environment 返回 WshEnvironment 集合对象。
SpecialFolders 使用 WshSpecialFolders 对象提供对 Windows shell 文件夹的访问,如桌面文件夹,开始菜单文件夹和个人文档文件夹。
下表说明和 WshShell 对象有关的方法。方法 说明
CreateShortcut 创建并返回 WshShortcut 对象。
ExpandEnvironmentStrings 扩展 PROCESS 环境变量并返回结果字符串。
Popup 显示包含指定消息的消息窗口。
RegDelete 从注册表中删除指定的键或值。
RegRead 从注册表中返回指定的键或值。
RegWrite 在注册表中设置指定的键或值。
Run 创建新的进程,该进程用指定的窗口样式执行指定的命令。
WshShell.Environment
Environment 属性返回 WshEnvironment 对象。语法
WshShell.Environment ( [strType]) = objWshEnvironment注释
若 strType 指定了环境变量所处的位置,可能值为 \"System\"、\"User\"、\"Volatile\" 和 \"Process\"。若未提供 strType,则该方法在 Windows NT 中检索系统环境变量或在 Windows 95 中检索进程环境变量。对于 Windows 95,strType 参数仅支持 \"Process\"。
下列变量是由 Windows 操作系统提供的。脚本也可获取由其他应用程序设置的环境变量。
名称 说明
NUMBER_OF_PROCESSORS 计算机上运行的处理器数目。
PROCESSOR_ARCHITECTURE 用户工作站使用的处理器类型。
PROCESSOR_IDENTIFIER 用户工作站的处理器 ID。
PROCESSOR_LEVEL 用户工作站的处理器级。
PROCESSOR_REVISION 用户工作站的处理器版本。
OS 用户工作站所用的操作系统。
COMSPEC 用于运行“命令提示”窗口的命令(通常为 cmd.exe)。
HOMEDRIVE 本地主驱动器(通常为 C 驱动器)。
HOMEPATH 用户的默认路径(在 Windows NT 上通常为 \\users\\default)。
PATH 路径环境变量。
PATHEXT 可执行文件的扩展名(通常为 .com、 .exe、.bat 或 .cmd)。
PROMPT 命令提示符(通常为 $P$G)。
SYSTEMDRIVE 系统所在的本地驱动器(例如,c:\\)。
SYSTEMROOT 系统目录(例如,c:\\winnt)。和 WINDIR 相同。
WINDIR 系统目录(例如 c:\\winnt)。和 SYSTEMROOT 相同。
TEMP 存储临时文件的目录(例如,c:\\temp)。用户可更改。
TMP 存储临时文件的目录(例如,c:\\temp)。用户可更改。
示例
\' Retrieve the NUMBER_OF_PROCESSORS system environment variable
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")Set WshSysEnv = WshShell.Environment(\"SYSTEM\")
Wscrīpt.Echo WshSysEnv(\"NUMBER_OF_PROCESSORS\")请参阅
WshEnvironment 对象
WshEnvironment 对象
WshEnvironment 对象未直接给出,可用 WshShell.Environment 属性来访问。ProgID N/A
文件名 WSHom.Ocx
CLSID
IID
下表描述与 WshEnvironment 对象关联的属性。属性 说明
Item 获取或设置指定的环境变量值。
Count 枚举项的数目。
length 枚举项的数目 (Jscrīpt)。
下表描述与 WshEnvironment 对象关联的方法。方法 说明
Remove 删除指定的环境变量。
WshShell.SpecialFolders
SpecialFolders 属性提供 WshSpecialFolders 对象以便访问 Windows 的 shell 文件夹,例如桌面文件夹、开始菜单文件夹和个人文档文件夹。语法
WshShell.SpecialFolders = objWshSpecialFolders示例
\' This code fragment shows how to access the desktop folder
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")
MsgBox \"Your desktop is \" & WshShell.SpecialFolders(\"Desktop\")
请参阅
WshSpecialFolders 对象WshSpecialFolders 对象
该对象未直接给出。要得到 WshSpecialFolders 对象,请使用 WshShell.SpecialFolders 属性。ProgID N/A
文件名 WSHom.Ocx
CLSID
IID
下表描述与 WshSpecialFolders 对象关联的属性。属性 描述
Item 指定文件夹的完整路径(默认)。
Count 枚举项的数目。
length 枚举项的数目 (Jscrīpt) 。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
AllUsersStartMenuAllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
示例
\' This fragment returns the full path for the Windows Desktop folder
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.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 scrīpt
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")
Set ōShellLink = WshShell.CreateShortcut(\"Current scrīpt.lnk\")
oShellLink.TargetPath = Wscrīpt.scrīptFullName
oShellLink.Save
Set ōUrlLink = 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 对象有关的属性。属性 说明
Arguments 快捷方式对象的参数。
Descrīption 快捷方式对象的说明。
Hotkey 快捷方式对象的热键。
IconLocation 快捷方式对象的图标位置。
TargetPath 快捷方式对象的目标路径。
WindowStyle 快捷方式对象的窗口样式。
WorkingDirectory 快捷方式对象的工作目录。
下表说明与 WshShortcut 对象有关的方法。方法 说明
Save 将快捷方式存储到指定的文件系统中。WshShortcut.Arguments
Arguments 属性提供快捷方式对象的参数。语法
WshShortcut.Arguments = strArgumentsWshShortcut.Descrīption
Descrīption 属性提供快捷方式对象的说明。语法
WshShortcut.Descrīption = strDescrīptionWshShortcut.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 = Wscrīpt.CreateObject(\"Wscrīpt.WshShell\")
strDesktop = WshShell.SpecialFolders(\"Desktop\")
Set ōMyShortcut = WshShell.CreateShortcut(strDesktop & \"\\a_key.lnk\")
OMyShortcut.TargetPath = \"%windir%\\notepad.exe\"
oMyShortCut.Hotkey = \"ALT+CTRL+F\"
oMyShortCut.Save
Wscrīpt.Echo oMyShortCut.HotKey = \"Alt+Ctrl+F\"请参阅
WshSpecialFolders 对象WshShortcut.IconLocation
IconLocation 属性提供快捷方式对象的图标位置。图标位置的格式应为 \"Path,index\"。语法
WshShortcut.IconLocation = strIconLocationWshShortcut.TargetPath
TargetPath 属性提供快捷方式对象的目标路径。语法
WshShortcut.TargetPath = strTargetPathWshShortcut.WindowStyle
WindowStyle 属性提供快捷方式对象的窗口样式。语法
WshShortcut.WindowStyle = natWindowStyleWshShortcut.WorkingDirectory
WorkingDirectory 为一个快捷方式对象提供工作目录。语法
WshShortcut.WorkingDirectory = strWorkingDirectoryWshShortcut.Save
Save 方法把快捷方式对象保存到由 FullName 属性指定的位置。语法
WshShortcut.SaveWshUrlShortcut 对象
该对象未直接给出。要获取 WshUrlShortcut 对象,可使用 WshShell.CreateShortcut 方法。ProgID N/A
文件名 WSHom.Ocx
CLSID
IID
下表说明了和 WshUrlShortcut 对象有关的属性。属性 说明
FullName URL 快捷方式对象的完整路径。
TargetPath URL 快捷方式对象的目标路径。
下表说明了和 WshUrlShortcut 对象有关的方法。方法 说明
Save 将快捷方式保存到指定的文件系统中。WshUrlShortcut.FullName
FullName 属性提供快捷方式对象的完整路径。语法
WshUrlShortcut.FullName = strFullNameWshUrlShortcut.TargetPath
TargetPath 属性提供快捷方式对象的目标路径。语法
WshUrlShortcut.TargetPath = strTargetPathWshUrlShortcut.Save
Save 方法保存一个快捷方式,该快捷方式指向 FullName 属性指定的位置。语法
WshUrlShortcut.SaveWshShell.ExpandEnvironmentStrings
ExpandEnvironmentStrings 方法在 strString 中扩展 PROCESS 环境变量并返回结果字符串。变量被 \'%\' 字符括起。环境变量不区分大小写。
语法
WshShell.ExpandEnvironmentStrings(strString) = strExpandedString示例
MsgBox \"Prompt is \" & WshShell.ExpandEnviromentStrings(\"%PROMPT%\")WshShell.Popup
Popup 方法显示一个弹出式消息框窗口,消息框中包含的消息由 strText 指定。该消息框的窗口标题由 strTitle 指定。若 strTitle 省略,则窗口标题为 Windows scrīpting Host。语法
WshShell.Popup(strText, [natSecondsToWait], [strTitle], [natType]) = intButton注释
若提供 natSecondsToWait 且其值大于零,则消息框在 natSecondsToWait 秒后关闭。natType 的含义与其在 Win32? MessageBox 函数中相同。下表显示 natType 中的值及含义。下表中的值可以组合。
按钮类型
值 说明
0 显示“确定”按钮
1 显示“确定”和“取消”按钮
2 显示“终止”、“重试”和“忽略”按钮
3 显示“是”、“否”和“取消”按钮
4 显示“是”和“否”按钮
5 显示“重试”和“取消”按钮
图标类型
值 说明
16 显示停止标记图标
32 显示问号图标
48 显示感叹号图标
64 显示信息标记图标
以上两个表并不涵盖 natType 的所有值。完整的列表请参阅 Win32 文档。返回值 intButton 指示用户所单击的按扭编号。若用户在 natSecondsToWait 秒之前不单击按扭,则 intButton 设置为 -1 。
值 说明
1 “确定”按扭
2 “取消”按扭
3 “终止”按扭
4 “重试”按扭
5 “忽略”按扭
6 “是”按扭
7 “否”按扭
示例
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")
WshShell.Popup \"Where do you want to go today?\"请参阅
Wscrīpt.Echo 方法Wscrīpt.Echo
Echo 方法在窗口(Wscrīpt.exe 中)或“命令提示符”窗口(Cscrīpt.exe 中)显示参数。参数用空格分隔。在 Cscrīpt.exe 中,该方法在显示最后一个参数之后输出一对回车/换行(CR LF)。
语法
Wscrīpt.Echo [anyArg...]示例
Wscrīpt.Echo
Wscrīpt.Echo 1, 2, 3
Wscrīpt.Echo \"Windows scrīpting Host is cool.\"WshShell.RegDelete
RegDelete 从注册表中删除名为 strName 的键或值。语法
WshShell.RegDelete strName参数
strName
如果 strName 以反斜杠 (\\) 结束,则该方法删除键而不是值。
strName 参数必须以下列之一的根键名开始:短根键名 长根键名
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIG
示例
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")WshShell.RegDelete \"HKCU\\scrīptEngine\\Value\" \' Delete value \"Value\"
WshShell.RegDelete \"HKCU\\scrīptEngine\\Key\\\" \' Delete key \"Key\"请参阅
WshShell.RegRead 方法、WshShell.RegWrite 方法
WshShell.RegRead
RegRead 方法返回名为 strName 的注册表键或值。语法
WshShell.RegRead(strName) = strValue参数
strName
如果 strName 以反斜杠 (\\) 结束,则该方法返回键,而不是值。
strName 参数必须以下列根键名开始。Short Long
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIG
注释
RegRead 方法仅支持 REG_SZ、REG_EXPAND_SZ、REG_DWORD、REG_BINARY 和 REG_MULTI_SZ 数据类型。若注册表有其他数据类型,RegRead 返回 DISP_E_TYPEMISMATCH。示例
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")WshShell.RegRead(\"HKCU\\scrīptEngine\\Val\") \' Read from value \"Val\"
WshShell.RegRead(\"HKCU\\scrīptEngine\\Key\\\") \' Read from key \"Key\"请参阅
WshShell.RegDelete 方法、WshShell.RegWrite 方法
WshShell.RegWrite
RegWrite 方法设置名为 strName 的注册表键或值。语法
WshShell.RegWrite strName, anyValue, [strType]参数
strName
若 strName 以一个反斜杠 (\\) 结束,则该方法设置键,而不是值。
strName 参数必须以下列根键名开头。Short Long
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS
HKEY_CURRENT_CONFIGanyValue
当 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 = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")WshShell.RegWrite \"HKCU\\scrīptEngine\\Value\", \"Some string value\"
WshShell.RegWrite \"HKCU\\scrīptEngine\\Key\\\", 1 \"REG_DWORD\"请参阅
WshShell.RegDelete 方法、WshShell.RegWrite方法
WshShell.Run
Run 方法创建一个新的进程,该进程以 intWindowStyle 窗口样式执行 strCommand。语法
WshShell.Run (strCommand, [intWindowStyle], [blnWaitOnReturn])参数
strCommand
在 strCommand 参数内部的环境变量被自动扩展。intWindowStyle
这是为新进程在 STARTUPINFO 结构内设置的 wShowWindow 元素的值。其意义与 ShowWindow 中的 nCmdShow 参数相同,可取以下值之一。名称 值 含义
SW_HIDE
0 隐藏窗口并激活另一窗口。
SW_MINIMIZE
6 最小化指定窗口并激活按 Z 序排序的下一个顶层窗口。
SW_RESTORE
9 激活并显示窗口。若窗口是最小化或最大化,则恢复到原来的大小和位置。在还原应用程序的最小化窗口时,应指定该标志。
SW_SHOW
5 以当前大小和位置激活并显示窗口。
SW_SHOWMAXIMIZED
3 激活窗口并以最大化显示该窗口。
SW_SHOWMINIMIZED
2 激活窗口并以最小化显示该窗口。
SW_SHOWMINNOACTIVE
7 最小化显示窗口。活动窗口保持活动。
SW_SHOWNA
8 以当前状态显示窗口。活动窗口保持活动。
SW_SHOWNOACTIVATE
4 按窗口最近的大小和位置显示。活动窗口保持活动。
SW_SHOWNORMAL
1 激活并显示一个窗口。若窗口是最小化或最大化,则恢复到其原来的大小和位置。blnWaitOnReturn
如果未指定 blnWaitOnReturn 或其值为 FALSE,则该方法立即返回到脚本继续执行而不等待进程结束。
若 blnWaitOnReturn 设为 TRUE,则 Run 方法返回由应用程序返回的任何错误代码。如果未指定 blnWaitOnReturn 或其值为 FALSE,则 Run 返回错误代码 0(zero)。示例
\' This fragment launches Notepad with the current executed scrīpt
Set WshShell = Wscrīpt.CreateObject(\"Wscrīpt.Shell\")
WshShell.Run (\"notepad \" & Wscrīpt.scrīptFullName)
WshShell.Run (\"%windir%\\notepad\" & Wscrīpt.scrīptFullName)\' This fragment returns the error code from the executed application
Return = WshShell.Run(\"notepad \" & Wscrīpt.scrīptFullName, 1, TRUE)