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

概述

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:控制台应用程序,运行时会有一个命令行窗口,输出被发送到这个窗口

语法

转义字符

'换行符 vbCrLf

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

print "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 Function

dim a,b,c,result
result = 0

a = inputbox("请输入一个数字")
print a

b = inputbox("请输入操作符")
print b

c = inputbox("请输入另一个数组")
print c

if b = "+" then
    result = CDbl(a) + CDbl(c)
elseif b = "-" then
    result = a - c
elseif b = "*" then
    result = a * c
elseif b = "/" then
    result = a / c
end if

print 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)=20

print nums(0)
print nums(3)

在这里插入图片描述

基于元素确定容量
Public Function print(message)
    Set stdout=CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
    stdout.WriteLine message
End Function

print "=========="

dim nums(2)
nums(0) = 0
nums(1) = 1
nums(2) = 2
for i = 0 to 2
    print nums(i)
next

print "=========="

dim nums2
nums2 = array(0,1,2)
for i = 0 to 2
    print nums2(i)
next

在这里插入图片描述

循环、迭代

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

public function getArraySize(nums)
    getArraySize = UBound(nums) - LBound(nums) + 1
end function




dim nums(5)
nums(0) = 1
nums(1) = 11
nums(2) = 111
nums(3) = 1111
nums(4) = 11111
nums(5) = 111111

for i = 0 to getArraySize(nums)-1
    print i
Next

print ""
print ""

dim index
index =0
do while true
    if index = getArraySize(nums) then
        Exit do
    end if

    print "nums["&index&"]: "&nums(index)
    index = index + 1
loop

print ""
print ""


index =0
do
    print "nums["&index&"]: "&nums(index)
    index = index + 1
loop until index = getArraySize(nums)

print ""
print ""

for each numElem in nums
    print 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 Function

dim a,b,c,d,e,f,g

'转成浮点数,入参非数字直接报错
a = CDbl("123")
print a

a = CDbl("123.5")
print a

'非数字直接报错
b = CBool(2)
print b
b = CBool(0)
print b
b = CBool(0.6)
print b

print ""

'8字节的数字
c = CByte("30")
print c

d = Asc("a")
print d

e = Chr(97)
print e

f = Hex(20)
print f

g = Oct(20)
print g

在这里插入图片描述

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

dim 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 Function

print "fsfsdf"

在这里插入图片描述

getArraySize - 获取数组长度

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

public function getArraySize(nums)
    getArraySize = UBound(nums) - LBound(nums) + 1
end function


dim nums(5)


arraySize = getArraySize(nums)
print arraySize

在这里插入图片描述

execCmd - 执行CMD命令

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

public 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
'Loop


public 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.AtEndOfStream
        result = objExecObject.StdOut.ReadAll()
    Loop

    execCmd = result

end Function

dim 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 Function

public function getArraySize(nums)
    getArraySize = UBound(nums) - LBound(nums) + 1
end function

public 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.AtEndOfStream
        result = objExecObject.StdOut.ReadAll()
    Loop

    execCmd = result

end Function




public Function readFileContent(filePath)
    Set oFS = CreateObject("Scripting.FileSystemObject")


    dim result,currentRowContent
    result = ""
	
	'文件不存在,直接退出
    If Not oFS.FileExists(filePath) Then
        print "[warning]file no exit: "&filePath
        readFileContent = result
        exit Function
    End If

	'文件存在
    Set oFile = oFS.OpenTextFile(filePath, 1, 0)
    do until oFile.AtEndOfStream
        currentRowContent = oFile.ReadLine
        result = result & currentRowContent & vbLf
    loop
    readFileContent = result

end Function


dim result


result = readFileContent("D:\workspace\personal\selenium-test\src\main\java\work\linruchang\vbsFile\1.txt")
print result


result = 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 Function


dim 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 Function

setEnvKeyValue "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 Function

public function getArraySize(nums)
    getArraySize = UBound(nums) - LBound(nums) + 1
end function

public 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.AtEndOfStream
        result = objExecObject.StdOut.ReadAll()
    Loop

    execCmd = result

end Function



Set 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.AtEndOfStream
    currentRowContent = oFile.ReadLine
    fileContent = fileContent & currentRowContent & vbLf
loop
oFile.Close

print 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 Function


dim 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 Function

setEnvKeyValue "OneDrive2", "C:\Users\Administrator\OneDrive2"

print(getenvkeyvalue("OneDrive2"))

在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值