东营建设信息网老网站,企业如何注册网站,卖域名的网站哪些好,网站建设需要收集资料吗文章目录 概述执行脚本语法转义字符文本弹框msgbx定义变量dim#xff08;普通类型#xff09;定义接收对象set字符拼接用户自定义输入框inputbox以及输入判断ifelse数组#xff08;参数表最大索引#xff0c;非数组容量#xff09;有容量无元素基于元素确定容量 循环… 文章目录 概述执行脚本语法转义字符文本弹框msgbx定义变量dim普通类型定义接收对象set字符拼接用户自定义输入框inputbox以及输入判断ifelse数组参数表最大索引非数组容量有容量无元素基于元素确定容量 循环、迭代 内置函数自定义函数print - 控制台打印getArraySize - 获取数组长度execCmd - 执行CMD命令readFileContent - 读取文件内容getEnvKeyValue - 获取某个环境变量值setEnvKeyValue - 新建或修改某个环境变量值 Windows常用内置对象文件对象Scripting.FileSystemObject系统环境变量对象USER 概述 VBSVisual Basic Script Editor 基于Visual Basic开发的脚本语言文件 注释仅有单行注释符号无多行 英文单引号’ 判断 值是否等于无需像其他语言一样输入双等于号直接单等于号即可不等于则使用进行替代 系统对象使用的文档可直接参考微软的VBA文档https://learn.microsoft.com/en-us/office/vba/api/overview/ 语法细节 1. 关键字大小写不敏感函数名大小写敏感。 定义Function函数时如果没有设置返回值则内部自动转成sub而调用sub 执行脚本
//方式1
直接双击脚本//方式2 CMD命令窗口
wscript //e:vbscript vbs脚本文件//方式3 CMS命令行窗口
C:\Windows\System32\cscript.exe vbs脚本文件#mermaid-svg-OEEaVaqjgxQ6uUVW {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .error-icon{fill:#552222;}#mermaid-svg-OEEaVaqjgxQ6uUVW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-OEEaVaqjgxQ6uUVW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .marker.cross{stroke:#333333;}#mermaid-svg-OEEaVaqjgxQ6uUVW svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-OEEaVaqjgxQ6uUVW .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .cluster-label text{fill:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .cluster-label span{color:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .label text,#mermaid-svg-OEEaVaqjgxQ6uUVW span{fill:#333;color:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .node rect,#mermaid-svg-OEEaVaqjgxQ6uUVW .node circle,#mermaid-svg-OEEaVaqjgxQ6uUVW .node ellipse,#mermaid-svg-OEEaVaqjgxQ6uUVW .node polygon,#mermaid-svg-OEEaVaqjgxQ6uUVW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-OEEaVaqjgxQ6uUVW .node .label{text-align:center;}#mermaid-svg-OEEaVaqjgxQ6uUVW .node.clickable{cursor:pointer;}#mermaid-svg-OEEaVaqjgxQ6uUVW .arrowheadPath{fill:#333333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-OEEaVaqjgxQ6uUVW .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-OEEaVaqjgxQ6uUVW .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-OEEaVaqjgxQ6uUVW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-OEEaVaqjgxQ6uUVW .cluster text{fill:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW .cluster span{color:#333;}#mermaid-svg-OEEaVaqjgxQ6uUVW div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-OEEaVaqjgxQ6uUVW :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 适合交互 适合批处理 执行程序 wscript窗口应用程序不创建控制台窗口输出不会在命令行中显示 cscript控制台应用程序运行时会有一个命令行窗口输出被发送到这个窗口 语法
转义字符
换行符 vbCrLfPublic Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionprint fdsfsdfsdfsdfprint print fdsfsdvbCrLffsdfsdf文本弹框msgbx
类似前端的alert玩意
msgbox 弹框内容定义变量dim普通类型
dim name
namelrcmsgbox name定义接收对象set
Set oFS CreateObject(Scripting.FileSystemObject)字符拼接
dim name,sport
namelrc
sportswimmingmsgbox name喜欢的运动是sport用户自定义输入框inputbox以及输入判断ifelse 格式 if then elseif then else then end if Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functiondim a,b,c,result
result 0a inputbox(请输入一个数字)
print ab inputbox(请输入操作符)
print bc inputbox(请输入另一个数组)
print cif b thenresult CDbl(a) CDbl(c)
elseif b - thenresult a - c
elseif b * thenresult a * c
elseif b / thenresult a / c
end ifprint abcresult
msgbox result 数组参数表最大索引非数组容量
有容量无元素
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Function定义容量为5个元素的数组里面的括号表示数组的最大索引
dim nums(4)nums(0)10
nums(3)20print nums(0)
print nums(3)基于元素确定容量
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionprint dim nums(2)
nums(0) 0
nums(1) 1
nums(2) 2
for i 0 to 2print nums(i)
nextprint dim nums2
nums2 array(0,1,2)
for i 0 to 2print nums2(i)
next循环、迭代
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionpublic function getArraySize(nums)getArraySize UBound(nums) - LBound(nums) 1
end functiondim nums(5)
nums(0) 1
nums(1) 11
nums(2) 111
nums(3) 1111
nums(4) 11111
nums(5) 111111for i 0 to getArraySize(nums)-1print i
Nextprint
print dim index
index 0
do while trueif index getArraySize(nums) thenExit doend ifprint nums[index]: nums(index)index index 1
loopprint
print index 0
doprint nums[index]: nums(index)index index 1
loop until index getArraySize(nums)print
print for each numElem in numsprint numElem
next内置函数
//将入参转成小数类型Double出参结果
CDbl//将入参转成整数类型Integer出参结果
CInt//将入参转成长整数类型Long出参结果
CLNG//将入参转成长布尔类型Bool出参结果 0false 其他数字ttrue转不出来数字报错
CBool//将入参转成字节类型Byte出参结果
CByte//用于将表达式转换为货币型数据(Currency)
CCur//用于将有效的日期表达式转换为日期型数据
CDate//将表达式转换为单精度浮点型数据(Single) java的Float
CSng//返回字符串首字母的ANSI字符代码(ASCII值) 数字
Asc//将指定的ANSI字符代码转换为相应的字符 字符串
Chr//将指定的数字转换为十六进制值
Hex//将指定的数字转换为八进制值
Oct//字符串2在字符串1的索引位置索引从1开始不是从0
InStr//字符串全部变成大写
ucasePublic Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functiondim a,b,c,d,e,f,g转成浮点数入参非数字直接报错
a CDbl(123)
print aa CDbl(123.5)
print a非数字直接报错
b CBool(2)
print b
b CBool(0)
print b
b CBool(0.6)
print bprint 8字节的数字
c CByte(30)
print cd Asc(a)
print de Chr(97)
print ef Hex(20)
print fg Oct(20)
print g Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functiondim h, i
h fsdfds123f
i 123
print InStr(h, i)自定义函数 tip 有无返回值建议都使用 function 即可毕竟相较于其他语言并没有sub这玩意 #mermaid-svg-Ew8peaRnHAvN3OgY {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY .error-icon{fill:#552222;}#mermaid-svg-Ew8peaRnHAvN3OgY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Ew8peaRnHAvN3OgY .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Ew8peaRnHAvN3OgY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Ew8peaRnHAvN3OgY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Ew8peaRnHAvN3OgY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Ew8peaRnHAvN3OgY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Ew8peaRnHAvN3OgY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Ew8peaRnHAvN3OgY .marker.cross{stroke:#333333;}#mermaid-svg-Ew8peaRnHAvN3OgY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Ew8peaRnHAvN3OgY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY .cluster-label text{fill:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY .cluster-label span{color:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY .label text,#mermaid-svg-Ew8peaRnHAvN3OgY span{fill:#333;color:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY .node rect,#mermaid-svg-Ew8peaRnHAvN3OgY .node circle,#mermaid-svg-Ew8peaRnHAvN3OgY .node ellipse,#mermaid-svg-Ew8peaRnHAvN3OgY .node polygon,#mermaid-svg-Ew8peaRnHAvN3OgY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Ew8peaRnHAvN3OgY .node .label{text-align:center;}#mermaid-svg-Ew8peaRnHAvN3OgY .node.clickable{cursor:pointer;}#mermaid-svg-Ew8peaRnHAvN3OgY .arrowheadPath{fill:#333333;}#mermaid-svg-Ew8peaRnHAvN3OgY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Ew8peaRnHAvN3OgY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Ew8peaRnHAvN3OgY .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Ew8peaRnHAvN3OgY .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Ew8peaRnHAvN3OgY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Ew8peaRnHAvN3OgY .cluster text{fill:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY .cluster span{color:#333;}#mermaid-svg-Ew8peaRnHAvN3OgY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Ew8peaRnHAvN3OgY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 函数 function期望函数调用有返回值 sub期望函数调用无返回值 print - 控制台打印
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionprint fsfsdf getArraySize - 获取数组长度
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionpublic function getArraySize(nums)getArraySize UBound(nums) - LBound(nums) 1
end functiondim nums(5)arraySize getArraySize(nums)
print arraySizeexecCmd - 执行CMD命令
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionpublic function getArraySize(nums)getArraySize UBound(nums) - LBound(nums) 1
end function 创建WScript.Shell对象
Set objShell CreateObject(WScript.Shell)执行cmd命令这里以ipconfig /all为例
Set objExecObject objShell.Exec(cmd /c ipconfig /all)读取命令执行的结果
Do While Not objExecObject.StdOut.AtEndOfStreamstrText objExecObject.StdOut.ReadAll()WScript.Echo strTextprint print print strText
Looppublic Function execCmd(cmd)dim result创建WScript.Shell对象Set objShell CreateObject(WScript.Shell) 执行cmd命令这里以ipconfig /all为例Set objExecObject objShell.Exec(cmd /c cmd) 读取命令执行的结果Do While Not objExecObject.StdOut.AtEndOfStreamresult objExecObject.StdOut.ReadAll()LoopexecCmd resultend Functiondim pwdResult,execComandStr
execComandStr pwd
pwdResult execCmd(execComandStr)print pwdResultreadFileContent - 读取文件内容
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionpublic function getArraySize(nums)getArraySize UBound(nums) - LBound(nums) 1
end functionpublic Function execCmd(cmd)dim result 创建WScript.Shell对象Set objShell CreateObject(WScript.Shell) 执行cmd命令这里以ipconfig /all为例Set objExecObject objShell.Exec(cmd /c cmd) 读取命令执行的结果Do While Not objExecObject.StdOut.AtEndOfStreamresult objExecObject.StdOut.ReadAll()LoopexecCmd resultend Functionpublic Function readFileContent(filePath)Set oFS CreateObject(Scripting.FileSystemObject)dim result,currentRowContentresult 文件不存在直接退出If Not oFS.FileExists(filePath) Thenprint [warning]file no exit: filePathreadFileContent resultexit FunctionEnd If文件存在Set oFile oFS.OpenTextFile(filePath, 1, 0)do until oFile.AtEndOfStreamcurrentRowContent oFile.ReadLineresult result currentRowContent vbLfloopreadFileContent resultend Functiondim resultresult readFileContent(D:\workspace\personal\selenium-test\src\main\java\work\linruchang\vbsFile\1.txt)
print resultresult readFileContent(D:\workspace\personal\selenium-test\src\main\java\work\linruchang\vbsFile\2.txt)
print resultgetEnvKeyValue - 获取某个环境变量值
打印日志
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Function获取某个环境变量值
public Function getEnvKeyValue(keyName)Set oShell CreateObject(WScript.Shell)Set oEnv oShell.Environment(USER)getEnvKeyValue oEnv(keyName)
end Functiondim result
result getEnvKeyValue(OneDrive)
print resultsetEnvKeyValue - 新建或修改某个环境变量值
修改新建某个环境变量值
public Function setEnvKeyValue(keyName, keyValue)Set oShell CreateObject(WScript.Shell)Set oEnv oShell.Environment(USER)oEnv(keyName) keyValue
end FunctionsetEnvKeyValue OneDrive2, C:\Users\Administrator\OneDriveWindows常用内置对象 系统对象使用的文档可直接参考微软的VBA文档https://learn.microsoft.com/en-us/office/vba/api/overview/ 文件对象Scripting.FileSystemObject
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Functionpublic function getArraySize(nums)getArraySize UBound(nums) - LBound(nums) 1
end functionpublic Function execCmd(cmd)dim result 创建WScript.Shell对象Set objShell CreateObject(WScript.Shell) 执行cmd命令这里以ipconfig /all为例Set objExecObject objShell.Exec(cmd /c cmd) 读取命令执行的结果Do While Not objExecObject.StdOut.AtEndOfStreamresult objExecObject.StdOut.ReadAll()LoopexecCmd resultend FunctionSet oShell CreateObject(WScript.Shell)Set oFS CreateObject(Scripting.FileSystemObject)读取文本文件并打印出来到控制台
dim file,fileContent,currentRowContent
file D:\workspace\personal\selenium-test\src\main\java\work\linruchang\vbsFile\1.txt
Set oFile oFS.OpenTextFile(file, 1, 0)
do until oFile.AtEndOfStreamcurrentRowContent oFile.ReadLinefileContent fileContent currentRowContent vbLf
loop
oFile.Closeprint fileContent 系统环境变量对象USER 打印日志
Public Function print(message)Set stdoutCreateObject(Scripting.FileSystemObject).GetStandardStream(1)stdout.WriteLine message
End Function获取某个环境变量值
public Function getEnvKeyValue(keyName)Set oShell CreateObject(WScript.Shell)Set oEnv oShell.Environment(USER)getEnvKeyValue oEnv(keyName)
end Functiondim result
result getEnvKeyValue(OneDrive)
print result修改新建某个环境变量值
public Function setEnvKeyValue(keyName, keyValue)Set oShell CreateObject(WScript.Shell)Set oEnv oShell.Environment(USER)oEnv(keyName) keyValue
end FunctionsetEnvKeyValue OneDrive2, C:\Users\Administrator\OneDrive2print(getenvkeyvalue(OneDrive2))