当前位置: 首页 > news >正文

VBS学习1 - 语法、内置函数、内置对象

文章目录

    • 概述
    • 执行脚本
    • 语法
      • 转义字符
      • 文本弹框msgbx
      • 定义变量dim(普通类型)
      • 定义接收对象set
      • 字符拼接&
      • 用户自定义输入框inputbox以及输入判断ifelse
      • 数组(参数表最大索引,非数组容量)
        • 有容量无元素
        • 基于元素确定容量
      • 循环、迭代
    • 内置函数
    • 自定义函数
      • print - 控制台打印
      • getArraySize - 获取数组长度
      • execCmd - 执行CMD命令
      • readFileContent - 读取文件内容
      • getEnvKeyValue - 获取某个环境变量值
      • setEnvKeyValue - 新建或修改某个环境变量值
    • Windows常用内置对象
      • 文件对象:Scripting.FileSystemObject
      • 系统环境变量对象:USER

概述

VBS(Visual 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脚本文件
适合交互
适合批处理
执行程序
wscript:窗口应用程序,不创建控制台窗口,输出不会在命令行中显示
cscript:控制台应用程序,运行时会有一个命令行窗口,输出被发送到这个窗口

语法

转义字符

'换行符 vbCrLfPublic Function print(message)Set stdout=CreateObject("Scripting.FileSystemObject").GetStandardStream(1)stdout.WriteLine message
End Functionprint "fdsfsd"&"fsdfsdf"print "=========="print "fdsfsd"&vbCrLf&"fsdfsdf"

在这里插入图片描述

文本弹框msgbx

'类似前端的alert玩意
msgbox "弹框内容"

在这里插入图片描述

定义变量dim(普通类型)

dim name
name="lrc"msgbox name

在这里插入图片描述

定义接收对象set

Set oFS = CreateObject("Scripting.FileSystemObject")

字符拼接&

dim name,sport
name="lrc"
sport="swimming"msgbox name&"喜欢的运动是:"&sport

在这里插入图片描述

用户自定义输入框inputbox以及输入判断ifelse

格式: if then elseif then else then end if

Public Function print(message)Set stdout=CreateObject("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 a&b&c&"="&result
msgbox result

在这里插入图片描述

数组(参数表最大索引,非数组容量)

有容量无元素
Public Function print(message)Set stdout=CreateObject("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 stdout=CreateObject("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 stdout=CreateObject("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出参结果 ==  0=>false 其他数字t=>true,转不出来数字报错
CBool//将入参转成字节类型Byte出参结果
CByte//用于将表达式转换为货币型数据(Currency)
CCur//用于将有效的日期表达式转换为日期型数据
CDate//将表达式转换为单精度浮点型数据(Single) == java的Float
CSng//返回字符串首字母的ANSI字符代码(ASCII值) == 数字
Asc//将指定的ANSI字符代码转换为相应的字符 == 字符串
Chr//将指定的数字转换为十六进制值
Hex//将指定的数字转换为八进制值
Oct//字符串2在字符串1的索引位置,索引从1开始,不是从0
InStr//字符串全部变成大写
ucase
Public Function print(message)Set stdout=CreateObject("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 stdout=CreateObject("Scripting.FileSystemObject").GetStandardStream(1)stdout.WriteLine message
End Functiondim h, i
h = "fsdfds123f"
i = "123"
print InStr(h, i)

在这里插入图片描述

自定义函数

tip: 有无返回值,建议都使用 function 即可,毕竟相较于其他语言,并没有sub这玩意

函数
function:期望函数调用有返回值
sub:期望函数调用无返回值

print - 控制台打印

Public Function print(message)Set stdout=CreateObject("Scripting.FileSystemObject").GetStandardStream(1)stdout.WriteLine message
End Functionprint "fsfsdf"

在这里插入图片描述

getArraySize - 获取数组长度

Public Function print(message)Set stdout=CreateObject("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 arraySize

在这里插入图片描述

execCmd - 执行CMD命令

Public Function print(message)Set stdout=CreateObject("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.AtEndOfStream
'    strText = objExecObject.StdOut.ReadAll()
''    WScript.Echo strText
'    print "================="
'    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 pwdResult

在这里插入图片描述

readFileContent - 读取文件内容

Public Function print(message)Set stdout=CreateObject("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 result

在这里插入图片描述

在这里插入图片描述

getEnvKeyValue - 获取某个环境变量值

'打印日志
Public Function print(message)Set stdout=CreateObject("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

在这里插入图片描述

在这里插入图片描述

setEnvKeyValue - 新建或修改某个环境变量值

'修改新建某个环境变量值
public Function setEnvKeyValue(keyName, keyValue)Set oShell = CreateObject("WScript.Shell")Set oEnv = oShell.Environment("USER")oEnv(keyName) = keyValue
end FunctionsetEnvKeyValue "OneDrive2", "C:\Users\Administrator\OneDrive"

在这里插入图片描述

Windows常用内置对象

系统对象使用的文档(可直接参考微软的VBA文档):https://learn.microsoft.com/en-us/office/vba/api/overview/

文件对象:Scripting.FileSystemObject

Public Function print(message)Set stdout=CreateObject("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 stdout=CreateObject("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\OneDrive2"print(getenvkeyvalue("OneDrive2"))

在这里插入图片描述

在这里插入图片描述


http://www.mrgr.cn/news/29703.html

相关文章:

  • [mysql]mysql排序和分页
  • 计算机毕业设计 办公用品管理系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
  • 壹嘉情,中国与世界经济文化交流的新桥梁
  • C++11: 声明和定义
  • 【C++ Primer Plus习题】16.8
  • 【渗透测试】-vulnhub源码框架漏洞-Os-hackNos-1
  • SQL编程复习(24/9/18)
  • 链表经典面试题
  • LCR 026
  • JavaScript第五天(函数,this,严格模式,高阶函数,闭包,递归,正则,ES6)高级
  • Python 入门教程(4)数据类型 | 4.3、数字类型
  • 请求转发和重定向的区别
  • 掌握Python虚拟环境:隔离项目依赖,提升开发效率的必备指南
  • 【Transformer深入学习】之一:Sinusoidal位置编码的精妙
  • Ubuntu上如何使用sh文件更新CMake
  • Redis - 深入理解Redis事务
  • 微服务配置中心介绍
  • 【学习笔记】IOC容器
  • 《深度学习》—— PyTorch的神经网络模块中常用的损失函数
  • 【AI学习】AI绘画发展简史