VBS 基础

上一篇 / 下一篇  2012-09-13 21:06:09 / 个人分类:VBS

1.QTP使用的是5.6版本的VBScript。
2.VBS 数据类型 “Variant”

Dim anVariant
anVariant = 123
MsgBox anVariant
anVariant = anVariant+100
MsgBox anVariant
anVariant = Ucase(anVariant) & "ABC"
MsgBox anVariant
UCase(string):Returns a string that has been converted to uppercase.
在这个例子中,没有作用
3.VBS 运算符
算术运算符,比较运算符,连接运算符
Dim Str
Str = "hello"
Str = Str & "World!"
Msgbox Str
4. VBScript. 的条件语句
1.If...Then...Else
2.Select Case var
Case "0"
Msgbox "0"
Case "1"
Msgbox "1"
Case "2"
Msgbox "2"
End Select
or 可以选择 Insert | Conditional Statement

5. VBScript. 循环语句
Do...Loop: 条件为真是循环
While... Wend: 条件为真时 TRUE 循环
For...Next: 指定循环次数
For each ... Next: 对于集中的每项或数组中的每个元素,重复执行一组语句

Dim counter, num
counter = 0
num = 9
Do Until num = 10
num = num -1
counter = counter +1
If num<8 Then Exit  Do
Loop
Msgbox "循环了" & counter & "次"
Exit Do

6. VBScript. 的 Sub 与 Function
Sub 不能返回值
Function 可以返回值

Function AddMethod(num)
AddMethod = num + 100
End Function
MsgBox addMethod(100)

Function 调用只需输入过程名及参数即可, 对Sub 的调用可使用Call语句, 也可以直接调用
Function AddMethod(num)
  AddMethod = num + 100
  MsgBox addMethod
End Function

'MsgBox AddMethod(100)AddMethod 200
Call AddMethod (200)

7.VBScript. 数组
Dim B()
Dim num
num = InputBox("输入数组大小:")
ReDim B(num)
B(0)=11
B(1)=12
Msgbox UBound(B)'获取数组的范围

要使用动态数组,必须随后使用ReDim确定维数和每一维的大小。使用Preserve关键字在重新调整大小时保留数组的内容。重新调整动态数组大小的次数是没有任何限制的,尽管将数组的大小调小时,将会丢失被删除元素的数据。

8. 常用函数
Split 函数
Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x" ,-1, 1)
Msg = MyArray(0) & "" & MyArray(1)
Msg = Msg & "" & MyArray(2)
Msgbox Msg
Split(expression[,delimiter[,count[,compare]]])
count
Optional. Number of substrings to be returned; -1 indicates that all substrings are returned.

compare
Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. 0 binary comparison  1 textual comparison
 

Mid 函数
从字符串中返回指定长度的字符
Dim MyVar
MyVar = Mid("VB Script. is fun!",4,6) '返回 Script

Returns the position of the first occurrence of one string within another.

InStr([start,]string1,string2[,compare])

Function getStrBetween(Str,StartStr,EndStr)
StartPos = InStr(Str,StartStr) + Len(StartStr)
EndPos =InStr(Str,EndStr)
BetLen = EndPos - StartPos
BetStr = Mid(Str,StartPos,BetLen)
getStrBetween= BetStr
End Function 



Str = "1234567890"
StartStr = "34"
EndStr = "0"

BetweenStr = getStrBetween(Str,StartStr,EndStr)

Msgbox BetweenStr


Trim 函数
取出空格 LTrim() RTrim() Trim()

CStr



VBS 常用对象
1.WSCript.Shell
可以执行Windows应用程序,Dos命令,打开文件

Dim oShell
Set Shell = CreateObject ("WSCript.shell")
oShell.run "calc"
Set Shell = Nothing

2.SendKeys 来模拟键盘按键
Set Shell = CreateObject(“WSCript.shell”)
oShell.run "calc"
oShell.sendKeys "%h"    

3.Scripting.Dictionary

可以用作数组
Key_Value

Set Dic = CreateObject("Scripting.Dictionary")
'添加Key value
For Iterator = 1 To 3 Step 1
Dic.Add Cstr(Iterator),Iterator & "_Value"
Next

'循环读取
For Iterator = 1 To Dic.Count Step 1
Msgbox Dic.Item(Cstr(Iterator))
Next


DicArray = Dic.Items
'For I = 0 To Dic.Count
For I = 0 To UBound(DicArray)
 Msgbox DicArray(I)
Next

If Dic.Exists("2") Then
Msgbox Dic.Item("2")
Dic.remove("2")
End If
Msgbox Dic.Count

Dic.RemoveAll()
Msgbox Dic.Count


Object that stores data key, item pairs.

Remarks

ADictionaryobject is the equivalent of a PERL associative array. Items can be any form. of data, and are stored in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually a integer or a string, but can be anything except an array.

The following code illustrates how to create aDictionaryobject:

[JScript]var y = new ActiveXObject("Scripting.Dictionary");
y.add ("a", "test");
if (y.Exists("a"))
   document.write("true");
...
[VBScript]Dim d   ' Create a variable.
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"   ' Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
...

Methods

Add Method (Dictionary)|Exists Method|Items Method|Keys Method|Remove Method|RemoveAll Method


object.Items( )  'Returnsan array containing all the itemsin a Dictionary object.


FileSystemObject
提供了从VBS or QTP访问文件的能力
CreateObject 来创建FSO 对象
OpenTextFile---打开文本本件
ReadLine---读入一行
CreateTextFile---创建文本文件
WriteLine---写入一行
---------------------------------------------------------------
1.Header Information
The header information should be a standard part of your scriptfor two reasons: It makes the script. easier to read and maintain, and it controls the way the script. runs.
Option Explicit
The Option Explicit statement tells the script. that each variable used in the script. is going to be listed before it is actually used.

On Error Resume Next
tells the computer that when something is messed up (causing an error), you want the computer to just skip that line and try the next line in the script

2.Reference Information
assign values to the variables you named in the Header information section of the script
  • Minimizes typing, and therefore ensures accuracy. You have to type long strings only once.
  • Makes the script. easier to read. If a variable is used several times in the script, the variable is “referenced” to the actual item only once.
  • Makes it easier to change the script. later. For example, the sample script. you’ve been examining pulls out computer names. By changing the registry key and nothing else, you can make the script. pull out any other information in the registry.
3.Worker Information
4.Output Information


1.For Each…Next
walk through a collection of objects and do something with an individual object from the collection

Option Explicit
On Error Resume Next
Dim colDrives 'the collection that comes from WMI
Dim drive 'an individual drive in the collection
Const DriveType = 3 'Local drives. From the SDK
set colDrives =_
GetObject("winmgmts:").ExecQuery("select size,freespace " &_
"from Win32_LogicalDisk where DriveType =" & DriveType)
For Each drive in colDrives 'walks through the collection
WScript.Echo "Drive: " & drive.DeviceID
WScript.Echo "Size: " & drive.size
WScript.Echo "Freespace: " & drive.freespace
Next

Defining Constants
Const DriveType = 3

p28

相关阅读:

TAG: vbs VBS

 

评分:0

我来说两句

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 12090
  • 日志数: 13
  • 建立时间: 2012-09-11
  • 更新时间: 2012-09-26

RSS订阅

Open Toolbar