VBScript学习笔记(二)

0x01 循环结构逐一递增并打印

循环打印1到10

demo1.vbs

dim count
for count=1 to 10
msgbox count
next

demo2.vbs

dim count
count=1
do while count<11
msgbox count
count=count+1
loop

补充:demo3.vbs

dim num:num=0
for i=0 to 9
    num=num+1
    if num=5 then
        exit for
    end if
next
msgbox num

这里的num:num=0表示给num赋值为0,然后exit for代表的是退出for循环。

0x02 循环嵌套

demo4.vbs

dim i,j,k
for i=1 to 9
for j=1 to 9
k=i*j
msgbox k
next
next

这里表示的是乘法口诀的效果。

注:需要提前结束的,可以使用任务管理器。

0x03 数组

一维数组

demo5.vbs

dim a(9)
for i=0 to 9
a(i)="i am "&i
msgbox a(i)
next

其中的a(9)表示声明数组a里有10个位置(通俗一点),另外就是&符号表示连接符。

同时定义多个不同的一维数组

demo6.vbs

dim name(2),high(2),mark(2)
dim i
for i=0 to 2
name(i)=inputbox("Please enter the name of the "&i+1&" student")
high(i)=inputbox("Please enter the height of the "&i+1&" student")
mark(i)=inputbox("Please enter the grade of the "&i+1&" student")
next

msgbox name(0)
msgbox high(0)
msgbox mark(0)

二维数组

demo7.vbs

dim msg(1,1)
dim i,j
for i=0 to 1
    for j=0 to 1
    dim opt
    select case j
    case 0
        opt="name"
    case 1
        opt="age"
    end select
    msg(i,j)=inputbox("Please enter the "&opt&" of the "&i+1&" student")
    next
next

for i=0 to 1
    for j=0 to 1
    msgbox msg(i,j)
    next
next

0x04 函数

demo8.vbs

function hello()
    msgbox "hello world"
end function

call hello()

demo9.vbs

function hello(num1,num2)
    num=num1+num2
    msgbox num
end function

call hello(12,18)

认识子程序 demo10.vbs

sub msg()
    msgbox "hello world"
end sub

call msg()

0x05 内置函数

date()、time()、now()、month()、year()等等时间相关的函数。

abs()绝对值、int()取整、sin()、cos()等数学相关的函数。

lcase()将大写转换为小写,len()计算长度、replace()替换、ucase()将小写转换为大写。

isarray()检测是否为数组,是就会为true,反之为false。

lbound()检测数组最小下标,ubound()检测数组最大下标。

cint()将字符串改变为数值,clng()转换为长整型,hex()转换为16进制数,oct()转换为8进制数,formatnumber()格式化数值,formatpercent()将分数转换为百分数。

0x06 运行可执行文件

打开记事本

set ws=wscript.createobject("wscript.shell")
ws.run "notepad.exe"
posted @ 2020-01-27 18:03  Paddling  阅读(218)  评论(0编辑  收藏  举报