编辑“︁
VBScript
”︁
跳转到导航
跳转到搜索
Template:Editnotice load/content
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
{{noteTA |G1=IT |G2=Windows }} {{Infobox programming language | logo = [[File:VBScript file format icon.png|48px]] | file_ext = .vbs, .vbe, .wsf, .wsc (.hta, .htm, .html, .asp) | paradigm = | year = {{Start date and age|1996}} | designer = | developer = [[微软]] | typing = | implementations = [[Windows Script Host]], [[Active Server Pages]] | dialects = | influenced_by = [[Visual Basic]] | influenced = [[Windows PowerShell]] | operating_system = [[Microsoft Windows|Windows]] | license = | website = | wikibooks = }} '''VBScript'''("''Microsoft Visual Basic Scripting Edition''")是[[微软]]以 [[Visual Basic]] 为蓝本开发的一种已废弃的{{link-en|动态脚本|Active Scripting}}语言。它允许 [[Microsoft Windows]] [[系统管理员]]生成用于管理计算机的强大工具,无需[[例外处理 (编程)|错误处理]],并具有[[子程序]]和其他高级编程结构,亦让用户能夠控制諸多计算环境。2023年,微軟宣布棄用VBScript。<ref>{{cite web |date=7 November 2023 |title=Deprecated features in the Windows client - What's new in Windows |url=https://learn.microsoft.com/en-us/windows/whats-new/deprecated-features |access-date=2024-12-05 |archive-date=2023-09-03 |archive-url=https://web.archive.org/web/20230903200546/https://learn.microsoft.com/en-us/windows/whats-new/deprecated-features |dead-url=no }}</ref>2024年5月,發布VBScript階段移除計畫,2027年後不再預設啟用,並且未來的Windows版本也會永久移除VBScript。<ref>{{Cite web |last=Shankar Chilla |first=Naveen |date=2024-05-22 |title=VBScript deprecation: Timelines and next steps |url=https://techcommunity.microsoft.com/t5/windows-it-pro-blog/vbscript-deprecation-timelines-and-next-steps/ba-p/4148301 |access-date=2024-05-28 |website=Microsoft Community Hub |language=en |archive-date=2024-10-05 |archive-url=https://web.archive.org/web/20241005145622/https://techcommunity.microsoft.com/t5/windows-it-pro-blog/vbscript-deprecation-timelines-and-next-steps/ba-p/4148301 |dead-url=no }}</ref> == 使用范围 == 由于VBScript可以通过[[Windows Script Host]]调用[[COM]],因而可以使用[[Windows]]操作系统中可以被使用的程序[[库]],比如它可以使用[[Microsoft Office]]的库,尤其是使用[[Microsoft Access]]和[[Microsoft SQL Server]]的程序库,当然它也可以使用其它程序和操作系统本身的库。在实践中VBScript一般被用在以下三个方面: === Windows操作系统 === VBScript可以被用来自动地完成重复性的Windows操作系统任务。在Windows操作系统中,VBScript可以在[[Windows Script Host]]的范围内运行。Windows操作系统可以自动辨认和执行*.VBS和*.WSF两种文件格式,此外[[Internet Explorer]]可以执行HTA和CHM文件格式。VBS和WSF文件完全是文字式的,它们-{只}-能通过少数几种对话窗口与用户通讯。HTA和CHM文件使用HTML格式,它们的程序码可以像HTML一样被编辑和检查。在WSF、HTA和CHM文件中VBScript和JavaScript的程序码可以任意混合。HTA文件实际上是加有VBS、JavaScript成分的HTML文件。CHM文件是一种{{link-en|在线帮助|Online help}},用户可以使用专门的编辑程序将HTML程序编辑为CHM。 Windows 操作系统也提供一些 VBScript 脚本来进行高级管理功能,例如管理 Windows 激活密钥的<code>slmgr.vbs</code>(Windows Server License Manager Script)。 === 网页浏览器(客户端的VBS) === 网页中的VBS可以用来控制客户端的网页浏览器(以浏览器执行VBS程序)。VBS与JavaScript在这一方面是竞争者,它们可以用来实现[[DHTML|动态HTML]],甚至可以将整个[[计算机程序|程式]]结合到[[网页]]中来。 至今为止VBS在客户方面未能占优势,因为它只获得Microsoft Internet Explorer的支持([[Mozilla Suite]]可以透过安裝一个[[Firefox擴充套件列表|套件]]来支援VBS),并且[[Internet Explorer 11|IE11]]起已不再支持VBScript<ref>{{en}}{{cite web|url=http://msdn.microsoft.com/en-us/library/ie/dn384057(v=vs.85).aspx|title=VBScript is no longer supported in IE11 edge mode for the Internet zone|accessdate=2014-05-25|archive-date=2015-04-07|archive-url=https://web.archive.org/web/20150407200900/https://msdn.microsoft.com/en-us/library/ie/dn384057(v=vs.85).aspx}}</ref>。而JavaScript则受到所有网页浏览器的支援。在Internet Explorer中VBS和JavaScript使用同样的權限,它们-{只}-能有限地使用Windows操作系统中的对象。 === 网页服务器(服务器方面的VBS) === 在网页服务器方面VBS是微软的[[Active Server Pages]]的一部分,它与[[JSP|JavaServer Pages]]和[[PHP]]是竞争对手。在这里VBS的程序码直接嵌入到[[HTML]]页内,这样的网页以ASP结尾。[[网页服务器]][[Internet信息服务]]执行ASP页内的程序部分并将其结果转化为HTML传递给网页浏览器供用户使用。这样服务器可以进行数据库闻讯并将其结果放到HTML网页中。 == 示范 == === Hello World === 最简单的例子: <syntaxhighlight lang="vb"> MsgBox "Hello World" </syntaxhighlight> 以<code>.vbs</code>文件保存。再使用<code>cscript.exe</code>或<code>wscript.exe</code>执行。 一个更复杂的例子中,示出了使用<code>MsgBox</code>作为函数(返回一个结果),并使用了三个参数,其中第二个参数使用的是常量。 <syntaxhighlight lang="vb"> 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 </syntaxhighlight> === 终止任务 === VBScript能访问[[Windows管理规范]] (WMI),就像[[Windows任务管理器]]。以下的代码执行时将会终止(“杀掉”)任何关于''notepad.exe''的进程。 <syntaxhighlight lang="vb"> '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</syntaxhighlight> 使用''Option Explicit''并不是必须的,但它被认为是VBScript的[[最佳实践]]。<ref>[http://msdn.microsoft.com/en-us/library/bw9t3484(VS.85).aspx Remarks on ''Option Explicit'' Statement] {{Wayback|url=http://msdn.microsoft.com/en-us/library/bw9t3484(VS.85).aspx |date=20160321175002 }}, MSDN Library</ref><ref>''Why force yourself to declare all variables?'', In VBScript For Dummies, John Walkenbach, IDG Books Worldwide, Inc., 1996</ref> === 创建具有唯一的名称的十个文件 === 这个实例显示如何创建文件并向它添加内容。它还演示了字符串连接。 <syntaxhighlight lang="vb"> 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 </syntaxhighlight> === 发送按键 === SendKeys方法模拟一个或多个按键到活动窗口(模拟在键盘上输入)。 在该示例中,脚本发送字符串“Hello World!”3次,每次暂停2秒(2000毫秒)。SendKeys巨集可能会在某些程序中失效,因为一些软件(如在安装时输入许可证密钥)将检查是否是真正的按键,而不是虚拟的。 <syntaxhighlight lang="vb"> 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 </syntaxhighlight> 执行期间,“Hello World!”将显示在命令提示符。 === Windows文件操作 === 对象FileSystemObject执行一些文件操作(例如测试一个文件是否存在),并且还创建一个文本文件(一个TextStream对象)。 <syntaxhighlight lang="vb"> 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 </syntaxhighlight> <code>MakeHelloWorldFile</code>将会在按下按钮后于C:\ 驱动器根目录创建(若已经存在则更新)一个小文本文件。 === Excel对象操作 === <syntaxhighlight lang="vb"> 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)" </syntaxhighlight> == 语言 == VBScript主要的优点有: * 由于VBScript由操作系统,而不是由网页浏览器解释,它的文件比较小。 * 易学。 * 在所有2000 / 98SE以后的Windows版本都可直接使用。 * 可以使用其它程序和可使用的物件(尤其是[[Microsoft Office]])。 缺点有: * 现在VBS无法作为电子邮件的附件了。Microsoft Outlook拒绝接受VBS为附件,收信人无法直接使用VBS附件。 * VBS的各种编辑器不受欢迎。 * 操作系统没有任何特别的保护设施。VBS程序与其它JS、EXE、BAT或CMD程序一样对待。操作系统没有监察恶意功能的能力。 == 和VB的对比 == === 不能为变量定义类型 === 在VB中,为变量定义类型使用“Dim 变量名 As 类型”的语句格式。 但是在VBScript中这样写是错误的,VBScript中的变量都是弱类型(即Variant变体),因此它不需要指定类型。-{只}-能使用“Dim 变量名”的格式,[[解释器]]会自动根据[[赋值]]的类型定义变量类型。 === 不能使用条件编译 === 在VB中,可以使用#If…Then、#ElseIf…Then、#Else、#End If、#Const… = …等语句定义编译时使用的语句 而由于VBScript不需要编译即可被WSH(Windows Script Host)直接解释执行,所以并不需要条件编译语句。 == 安全性 == 微软决定Outlook和[[Outlook Express]]中的HTML邮件可以使用VBScript后出现了许多利用Windows Script Host和[[ActiveX]]的功能的[[电脑病毒]]。这些病毒之所以能够传播开来也是因为一开始这些系统功能完全未受保护。虽然VBScript和JavaScript使用同样的使用操作系统的功能的安全措施,今天这些功能被看作不符合标准。 一般很难保护VBScript的程序码不被用户看到。 == 参考资料 == {{reflist}} == 外部链接 == {{commonscat|VBScript}} *[https://docs.microsoft.com/en-us/previous-versions/sx7b3k7y(v=vs.85) VBScript User's Guide] {{Wayback|url=https://docs.microsoft.com/en-us/previous-versions/sx7b3k7y(v=vs.85) |date=20220418071511 }} *[https://docs.microsoft.com/en-us/previous-versions/d1wf56tt(v=vs.85) VBScript Language Reference] {{Wayback|url=https://docs.microsoft.com/en-us/previous-versions/d1wf56tt(v=vs.85) |date=20220418071507 }} {{網頁技術與標準}} {{程序设计语言|V}} {{視窗元件}} {{Microsoft APIs}} [[Category:脚本语言]] [[Category:BASIC程序设计语言家族]] [[Category:微软编程语言]] [[Category:1996年建立的程式語言]] [[Category:Windows组件]] [[Category:Internet Explorer]]
摘要:
请注意,所有对Local Chinese Wikipedia的贡献均可能会被其他贡献者编辑、修改或删除。如果您不希望您的文字作品被随意编辑,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源(详情请见
Project:著作权
)。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)
导航菜单
个人工具
未登录
讨论
贡献
创建账号
登录
命名空间
页面
讨论
大陆简体
不转换
简体
繁體
大陆简体
香港繁體
澳門繁體
大马简体
新加坡简体
臺灣正體
查看
阅读
编辑
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息