VBScript
发行时间 | 1996 |
---|---|
| |
Windows | |
.vbs, .vbe, .wsf, .wsc (.hta, .htm, .html, .asp) | |
docs | |
Windows Script Host, Active Server Pages | |
Visual Basic | |
Windows PowerShell |
VBScript("Microsoft Visual Basic Scripting Edition")
使用 范围
[编辑]Windows操作 系 统
[编辑]VBScript
Windows
网页浏览器 (客 户端的 VBS)
[编辑]网页
网页服 务器(服 务器方面 的 VBS)
[编辑]示 范
[编辑]Hello World
[编辑]MsgBox "Hello World"
以.vbs
cscript.exe
wscript.exe
执行。
一个更复杂的例子中,MsgBox
Dim x
' These three produce the same result. However, the use of constants as in the third line
' is considered best practice.
x = MsgBox("Hello World:Text",1+64+4096,"Hello World:Title")
x = MsgBox("Hello World:Text",4161,"Hello World:Title")
x = MsgBox("Hello World:Text", vbOKCancel+vbInformation+vbSystemModal, _
"Hello World:Title")
' Presents the number corresponding to the button pressed. Different constants will produce
' different behaviours. For example, vbOKCancel specifies two buttons in the dialogue box,
' whereas vbYesNoCancel specifies three.
x = MsgBox("Hello World:Text", vbYesNoCancel+vbInformation,"Hello World:Title")
MsgBox "The result is " & x
终止任 务
[编辑]VBScript
'Terminate all processes involving the name <strProcessToKill>
Option Explicit
Dim strComputer, strProcessToKill, objWMIService, colProcess, objProcess
strComputer = "."
strProcessToKill = "notepad.exe"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer _
& "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcessToKill & "'")
For Each objProcess in colProcess
MsgBox "... terminating " & objProcess.Name
objProcess.Terminate()
Next
创建具有 唯 一的名称的十个文件
[编辑]这个实例显示
For i = 1 to 10
createFile( i )
Next
Public sub createFile(a)
Dim fso,myFile
filePath = "C:\file_name" & a & ".txt"
Set fso=CreateObject("Scripting.FileSystemObject")
Set MyFile= fso.CreateTextFile( filePath)
MyFile.WriteLine("This is a separate file")
MyFile.close
End Sub
发送按键
[编辑]SendKeys
set shl = createobject("wscript.shell")
shl.sendkeys "Hello World!"
wscript.sleep 2000
shl.sendkeys "Hello World!"
wscript.sleep 2000
shl.sendkeys "Hello World!"
wscript.sleep 2000
执行
Windows文 件 操作
[编辑]对象FileSystemObject执行
myfilename = "C:\Wikipedia - VBScript - Example - Hello World.txt"
MakeHelloWorldFile myfilename
Sub MakeHelloWorldFile (FileName)
'Create a new file in C: drive or overwrite existing file
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(FileName) Then
Answer = MsgBox ("File " & FileName & " exists ... OK to overwrite?", vbOKCancel)
'If button selected is not OK, then quit now
'vbOK is a language constant
If Answer <> vbOK Then Exit Sub
Else
'Confirm OK to create
Answer = MsgBox ("File " & FileName & " ... OK to create?", vbOKCancel)
If Answer <> vbOK Then Exit Sub
End If
'Create new file (or replace an existing file)
Set FileObject = FSO.CreateTextFile (FileName)
FileObject.WriteLine "Time ... " & Now()
FileObject.WriteLine "Hello World"
FileObject.Close()
MsgBox "File " & FileName & " ... updated."
End Sub
MakeHelloWorldFile
Excel对象操作
[编辑]Option Explicit '所有 变量必须显式声明
Dim app,workbook,sheet
Dim row,col
Set app = WScript.CreateObject("Excel.Application")
app.Visible = True
Set workbook = app.WorkBooks.Add
Set sheet = workbook.Worksheets(1)
'10x10 random value
For row = 1 To 10
For col = 1 To 10
sheet.Cells(row,col).Value = CInt(Int((100 * Rnd()) + 1))
Next
Next
Set sheet = workbook.Worksheets(2)
'10x10 random value
sheet.Range("A1:J10").Formula = "=Int(Rand() * 100 + 1)"
语言
[编辑]VBScript
由 于VBScript由 操作 系 统,而不是 由 网页浏览器 解 释,它的文 件 比 较小。易学 。在 所有 2000 / 98SE以后的 Windows版本 都 可 直接 使用 。可 以使用 其它程 序 和 可 使用 的 物件 (尤 其是Microsoft Office)。
- 现在VBS无法
作 为电子 邮件的 附 件 了 。Microsoft Outlook拒 绝接受 VBS为附件 ,收 信人 无法直接 使用 VBS附 件 。 - VBS
的 各 种编辑器不 受欢迎 。 操作 系 统没有 任 何 特 别的保 护设施 。VBS程 序 与 其它JS、EXE、BAT或 CMD程 序 一 样对待 。操作 系 统没有 监察恶意功 能 的 能力 。
和 VB的 对比
[编辑]不能 为变量定 义类型
[编辑]不能 使用 条件 编译
[编辑]而由于VBScript
安全 性
[编辑]一般很难保护VBScript
参考 资料
[编辑]- ^ (
英文 )VBScript is no longer supported in IE11 edge mode for the Internet zone. [2014-05-25]. (原始 内容 存 档于2015-04-07). - ^ Remarks on Option Explicit Statement (页面
存 档备份,存 于互联网档案 馆), MSDN Library - ^ Why force yourself to declare all variables?, In VBScript For Dummies, John Walkenbach, IDG Books Worldwide, Inc., 1996
外部 链接
[编辑]
|
|
|
|