1.編寫(xiě)環(huán)境
今天突發(fā)奇想下載了個(gè)gVim來(lái)寫(xiě)VBS腳本,我用的版本是7.4的
在寫(xiě)腳本前,需要在gVim的安裝根目錄下,找到文件“_vimrc”,在里面添加下面三行:
set number
set softtabstop=4
set tabstop=4
意思分別是“顯示行號(hào)”、“按退格鍵一次刪掉4個(gè)空格”和“設(shè)定Tab長(zhǎng)度為4個(gè)字符”
這個(gè)設(shè)置類(lèi)似于Linux系統(tǒng)下文件“.vimrc”的配置
1.字典:Scripting.Dictionary
VBS中的字典需要使用Scripting.Dictionary
腳本文件:a.vbs,包含字典的添加、刪除、判斷鍵是否存在、修改鍵、修改值、遍歷、統(tǒng)計(jì)鍵值對(duì)個(gè)數(shù)
Option Explicit
'建立字典
Dim Dict : Set Dict = CreateObject("Scripting.Dictionary")
'添加鍵值對(duì)
Dict.Add "Key1", "Item1"
Dict.Add "Key2", "Item2"
Dict.Add "Key3", "Item3"
'字典中鍵值對(duì)數(shù)量
WScript.Echo "字典中現(xiàn)有鍵值對(duì)數(shù)量: " Dict.Count
WScript.Echo
'檢查指定鍵是否存在
If Dict.Exists("Key1") Then
WScript.Echo "Key1 存在!"
Else
WScript.Echo "Key1 不存在!"
End If
If Dict.Exists("Keyn") Then
WScript.Echo "Keyn 存在!"
Else
WScript.Echo "Keyn 不存在!"
End If
WScript.Echo
'遍歷字典
Sub TraverseDict
Dim DictKeys, DictItems, Counter
DictKeys = Dict.Keys
DictItems = Dict.Items
For Counter = 0 To Dict.Count - 1
WScript.Echo _
"鍵: " DictKeys(Counter) _
"值: " DictItems(Counter)
Next
End Sub
TraverseDict
WScript.Echo
'在一個(gè)鍵值對(duì)中,修改鍵或修改值
Dict.Key("Key2") = "Keyx"
Dict.Item("Key1") = "Itemx"
TraverseDict
WScript.Echo
'刪除指定鍵
Dict.Remove("Key3")
TraverseDict
WScript.Echo
'刪除全部鍵
Dict.RemoveAll
WScript.Echo "字典中現(xiàn)有鍵值對(duì)數(shù)量: " Dict.Count
調(diào)用方法:通過(guò)雙擊a.bat調(diào)用,a.bat代碼如下:
cscript a.vbs
pause
運(yùn)行結(jié)果截圖:
2.動(dòng)態(tài)數(shù)組:System.Collections.ArrayList
VBS中的動(dòng)態(tài)數(shù)組需要使用System.Collections.ArrayList
腳本文件:b.vbs,包含動(dòng)態(tài)數(shù)組的添加元素、刪除元素、遍歷、統(tǒng)計(jì)元素個(gè)數(shù)、清空
Option Explicit
'建立動(dòng)態(tài)數(shù)組
Dim Arrl : Set Arrl = CreateObject("System.Collections.ArrayList")
'添加元素
Arrl.Add "Element3"
Arrl.Add "Element2"
Arrl.Add "Element1"
'查看動(dòng)態(tài)數(shù)組中的元素?cái)?shù)
WScript.Echo "動(dòng)態(tài)數(shù)組中現(xiàn)有元素?cái)?shù)量: " Arrl.Count
WScript.Echo "動(dòng)態(tài)數(shù)組容量: " Arrl.Capacity
WScript.Echo
'遍歷動(dòng)態(tài)數(shù)組
Sub TraverseArrl
Dim Counter
For Counter = 0 To Arrl.Count - 1
WScript.Echo Arrl(Counter)
Next
End Sub
TraverseArrl
WScript.Echo
'動(dòng)態(tài)數(shù)組排序
Arrl.Sort
TraverseArrl
WScript.Echo
'刪除指定元素
Arrl.Remove("Element1")
TraverseArrl
WScript.Echo
'清空全部元素
Arrl.Clear
WScript.Echo "動(dòng)態(tài)數(shù)組中現(xiàn)有元素?cái)?shù)量: " Arrl.Count
WScript.Echo "動(dòng)態(tài)數(shù)組容量: " Arrl.Capacity
調(diào)用方法:通過(guò)雙擊b.bat調(diào)用,b.bat代碼如下:
cscript b.vbs
pause
運(yùn)行結(jié)果截圖:
3.隊(duì)列:System.Collections.Queue
VBS中的隊(duì)列需要使用System.Collections.Queue
腳本文件:c.vbs,包含隊(duì)列的添加元素(入隊(duì))、刪除元素(出隊(duì))、遍歷、統(tǒng)計(jì)元素個(gè)數(shù)、清空
Option Explicit
'建立隊(duì)列
Dim Que : Set Que = CreateObject("System.Collections.Queue")
Que.EnQueue("Element1")
Que.EnQueue("Element2")
Que.EnQueue("Element3")
'查看隊(duì)列中的元素?cái)?shù)
WScript.Echo "隊(duì)列中的元素?cái)?shù): " Que.Count
WScript.Echo
'遍歷隊(duì)列
Sub TraverseQue
Dim ArrQue : ArrQue = Que.ToArray
Dim Counter
For Counter = 0 To UBound(ArrQue)
WScript.Echo ArrQue(Counter)
Next
End Sub
TraverseQue
WScript.Echo
'退出隊(duì)列
WScript.Echo Que.DeQueue
WScript.Echo "---"
TraverseQue
WScript.Echo
'清空隊(duì)列
Que.Clear
WScript.Echo "隊(duì)列中的元素?cái)?shù): " Que.Count
調(diào)用方法:通過(guò)雙擊c.bat調(diào)用,c.bat代碼如下:
cscript c.vbs
pause
運(yùn)行結(jié)果截圖:
4.堆棧:System.Collections.Stack
VBS中的堆棧需要使用System.Collections.Stack
腳本文件:d.vbs,包含堆棧的添加元素(壓棧)、刪除元素(出棧)、遍歷、統(tǒng)計(jì)元素個(gè)數(shù)、清空
Option Explicit
'建立堆棧
Dim Stk : Set Stk = CreateObject("System.Collections.Stack")
Stk.Push "Element1"
Stk.Push "Element2"
Stk.Push "Element3"
'查看堆棧中的元素?cái)?shù)
WScript.Echo "堆棧中的元素?cái)?shù): " Stk.Count
WScript.Echo
'遍歷堆棧
Sub TraverseStk
Dim ArrStk : ArrStk = Stk.ToArray
Dim Counter
For Counter = 0 To UBound(ArrStk)
WScript.Echo ArrStk(Counter)
Next
End Sub
TraverseStk
WScript.Echo
'元素出棧
WScript.Echo Stk.Pop
WScript.Echo "---"
TraverseStk
WScript.Echo
'清空堆棧
Stk.Clear
WScript.Echo "堆棧中的元素?cái)?shù): " Stk.Count
調(diào)用方法:通過(guò)雙擊d.bat調(diào)用,d.bat代碼如下:
cscript d.vbs
pause
運(yùn)行結(jié)果截圖:
可以看出,遍歷隊(duì)列和堆棧時(shí),遍歷順序時(shí)由出隊(duì)和出棧的順序決定的,而不是入隊(duì)和壓棧的順序