概述
VBS 学习笔记
- 常用语法
- 强制显式声明所有变量
- 变量声明
- 变量赋值
- 分支
- 遍历
- 字符串
- 数组
- 一些代码片段的收集
- 运行BAT命令,取得结果进行处理
- 输出HTA并运行
- 网上找来的,生成exe。
- 参考资料
常用语法
- 不要分号结尾(较长时间没用VBS突然回来时很容易忘记这个)
- 注释用单引号
强制显式声明所有变量
Option Explicit
Dim a,b
Public c, d
Private e
变量声明
https://www.runoob.com/vbscript/vbscript-variables.html
- 必须以字母开头
- 不能包含点号
.
- 不能超过 255 个字符
- vbs 不允许声明的同时给变量赋值
- vbs变量有ms家族的传统:不区分大小写
dim arr '声明变量
arr = split("123,456", ",") '切字符串
msgbox arr(0) '数组引用
变量赋值
- 普通类型直接赋值。
- 对象赋值前面要加 set
dim str'声明变量
str= "哈喽"
msgbox str
Set WshShell = CreateObject("WScript.Shell")
WshShell.Exec "calc" '打开计算器
分支
IF
注意
vbs判断相等用的是一个等号 =
Dim input
input = InputBox("请随意输入一个数字", "猜数字", 0)
If input = "" Then
msgbox "你啥也没输入啊", 64, "结果"
ElseIf input = 9527 Then
msgbox "恭喜猜对了 9527 就是你的终生代号", 64, "结果"
Else
msgbox "真可惜,木猜中!", 64, "结果"
End If
Select Case
Dim result
result = MsgBox("弹窗消息", 4, "弹窗标题")
Set WshShell = createobject("wscript.shell")
Select Case result
Case 1
WshShell.popup "vbOK - OK 按钮被点击", 1, "操作反馈", 64
Case 2
WshShell.popup "vbCancel - Cancel 按钮被点击", 1, "操作反馈", 64
Case 3
WshShell.popup "vbAbort - Abort 按钮被点击", 1, "操作反馈", 64
Case 4
WshShell.popup "vbRetry - Retry 按钮被点击", 1, "操作反馈", 64
Case 5
WshShell.popup "vbIgnore - Ignore 按钮被点击", 1, "操作反馈", 64
Case 6
WshShell.popup "vbYes - Yes 按钮被点击", 1, "操作反馈", 64
Case else
WshShell.popup "vbNo - No 按钮被点击", 1, "操作反馈", 64
End Select
遍历
dim arr
arr = split(" aaa , bbb , ccc ", ",")
for each str in arr
msgbox(str)
next
dim str, i
str = "老虎"
i = 0
Do While i < 10
str = str & "_尾巴"
i = i+1
loop
msgbox str
字符串
https://www.runoob.com/vbscript/vbscript-ref-functions.html#string
回车符(Chr(13))、换行符(Chr(10))可以&
组合使用
' 拼接用 &
msgbox "用 & 符连接 " & Chr(13) & "用 Chr(13) 回车"
数组
https://www.runoob.com/vbscript/vbscript-ref-functions.html#array
Dim arr
arr = Array(1,3,5,7)
MsgBox arr(2)
set WshShell = createobject("wscript.shell")
for each x in arr
WshShell.popup x, 1, "标题", 64 '1秒自动消失
next
一些代码片段的收集
运行BAT命令,取得结果进行处理
Exec
Set WshShell = CreateObject("WScript.Shell")
Set objExec = WshShell.Exec ("ping www.baidu.com")
Do While Not objExec.StdOut.AtEndOfStream
mystr = objExec.StdOut.ReadAll()
loop
msgbox mystr
'好像用了 ReadAll 系统自然就会等待执行完毕。所以不用do loop 也 OK
Set WshShell = CreateObject("WScript.Shell")
Set objWshScriptExec = WshShell.Exec("ping www.baidu.com")
Set objStdOut = objWshScriptExec.StdOut
mystr = objStdOut.ReadAll
WScript.Echo mystr
输出HTA并运行
set Wshell=CreateObject("Wscript.Shell")
Wshell.run "%comspec% /c echo Hello HTA World! > %temp%目标文件.hta &pause ",0,0
set oexec = Wshell.Exec( "mshta.exe %temp%目标文件.hta")
WScript.Quit (0)
'直接用mshta.exe 运行HTA
set Wshell=CreateObject("Wscript.Shell")
'生成一个页面,带VBS脚本。调用了一个msgbox。确定后用:window.close自动关闭HTA窗口(爱出错的地方是引号,要注意)
set oexec = Wshell.Exec("mshta vbscript:""<html><title>myzam</title><body>hello world</body><script language=""""vbscript"""">msgbox """"hello world"""":window.close</script></html>""")
WScript.Quit (0)
网上找来的,生成exe。
把自己的EXE转0101再输出就好了
'调用气泡提示
Sub PopTips(tipsContent,tipsTitle)
Dim content, title
content = tipsContent
title = tipsTitle
'If InStr(content,"|") > 0 Then
'content = Split(tipsContent, "|")
'content = content(0)
'title = content(1)
'End If
Set W = CreateObject("WScript.Shell")
W.Run "cmd.exe /c E:PlannerworkflowProgressBalloonTip.exe " & content &" "& title, 0
Set w = Nothing
End Sub
'生成气泡提示exe
Sub ExportBalloonTip()
Dim Data, StrToByte
Data = "4D5A00000000000000000000504500004C010200534C58210000000000000000E0000F010B01000000000000000600000000000054010000001000000C00000000004000001000000002000004000000000000000400000000000000004000000002000000000000020000000000100000100000000010000010000000000000100000000000000000000000983200008400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000001000000000000000000000000000000000000000000000E00000C0000000000000000000100000003000001903000000020000000000000000000000000000E00000C08725DC324000619455A4B680FF1373F933C9FF13731633C0FF13731FB68041B010FF1312C073FA753AAAEBE0FF530802F683D901750EFF5304EB24ACD1E8742D13C9EB189148C1E008ACFF53043B43F8730A80FC05730683F87F77024141958BC5B600568BF72BF0F3A45EEB9F5EAD97AD50FF5310958B074078F37503FF630C5055FF5314ABEBEE33C941FF1313C9FF1372F8C302D275058A164612D2C34B45524E454C33322E646C6C00004D3815403259096724454CFF027F65C711B3270CF49B4887C17F6874C4703A712FEE691D2E62610E6475C3636F6DF373011C6C79782C2042D79A53AF0B4D2E02C20325730E0842616C2E6F206E54697020E8653A7874B75B14576C955D0E086DE76F75C728637329331D49906EA7266444F9433853853C01C71805E012FCE801C114FF15241001A3E4911FB9700633C0890DE81BC844EC981425A3F0DD1354F405117813227CC448C41406A2F82B30C390038B44241D0856579D7C800C33C983F8030A893D8014252035017727FF248560115834EB6822B4081B02B5E216910C03080A8C44018B2A3518942268E00A51FFD657A3150C0F866A0220335F5EE61432A134091C2428507C9B55845EC3F083FE01FA278ECA40D285F67E23008A0C3A80F9307C17C00A397F120FBE1FC98D04C7423BD6604441D07C68E499BB9410534301C383EC18384224185355B250BB28B80B9352BD9864BE084C500E14AA66003A3BC50F8E52A3B31F4E04CC3D202F51296890AD0880130CFFD7451C1283C40C011202C6057F170C5AA18414560852278484146C0381C3087EE28B7F461F8D4C241050CD5146F554F6600A52E84D14311B8BD861F41F2019047E5552100ACE6A1854EAFE8BB50026FD037E2F3312EDEB2B085620455C0A0558A459D6F12C5D110C8ED6C16818E8DCFD8E7C5553ED258B8A1E086AFA910450275D6A5B3D2CB85701EBE54BF678A9A8113AA691111422DC8F010E2C1510EBCB538ED72D7011DC73746272166E412E054578C3EE50F16F63FD73C73B1D47FD7C43716D5C61E81D4C693E3857074B45524E3F6033322E646CE309994144546BD46F706757406465771D145F7358721A7466D29E1AE25E142CFA686065125F4E6F7412696679843D506F5DB354D8417267337657514842C463D001EABAF6000000F03200000000000000000000F2010000F032000000000000000000000000000000000000000000000010400000304000C814400000000000E032400080000000007D0000C0324000E8014000DC014000DE014000C0114000FA32000008330000000000004C6F61644C69627261727941000047657450726F634164647265737300"
Set xmldoc = CreateObject("Microsoft.XMLDOM")
xmldoc.loadXML ""
Set pic = xmldoc.createElement("pic")
pic.dataType = "bin.hex"
pic.nodeTypedValue = Data
StrToByte = pic.nodeTypedValue
Set pic = Nothing
Set xmldoc = Nothing
Set Tips = CreateObject("ADODB.Stream")
Tips.Type = 1
Tips.Open
Tips.Write StrToByte
Tips.SaveToFile "E:PlannerworkflowProgressBalloonTip.exe", 2
Tips.Close
Set Tips = Nothing
End Sub
on error resume Next
'---------VBS错误处理----------
'-----------代码部分-----------
msgbox ""& 1/0
'-----------代码部分-----------
If err.Number <> 0 Then
errNum = Err.Number
errDesc = Err.Description
WScript.Echo errNum & " - " & errDesc
End If
参考资料
微软文档
- MSDN - Scripting
- 微软 VBScript 语言参考
- WshShell Object 瘟宿主对象
- MS Windows commands
- popup 倒计时自动关闭
菜鸟教程
- 菜鸟教程实例代码
- msgbox 消息弹框
Demon
Demon’s Blog » 程序设计 » VBS中Run和Exec的区别
最后
以上就是受伤金针菇为你收集整理的VBS 学习笔记常用语法一些代码片段的收集参考资料的全部内容,希望文章能够帮你解决VBS 学习笔记常用语法一些代码片段的收集参考资料所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复