Showing posts with label QTP_VBScripting. Show all posts
Showing posts with label QTP_VBScripting. Show all posts

Reverse a string without using string functions - VBScript

mystr = inputbox("Enter the String:")


'Option 1 - 
Set r=new regexp
 r.pattern ="\w|\W" ' "[a-z 0-9 A-Z]" is equivalent to \w
 r.global=true
 set s=r.execute(mystr)
For each i in s
 revstr = i.value & revstr
Next
MsgBox "Using regexp :" & vbnewline & revstr

'Option 2 - 
cnt = Len(mystr)
For i = 1 to cnt
  revstr = Mid(mystr, i, 1) & revstr
Next
Msgbox "Using Mid function: " & vbnewline & revstr


@Please feel free to comment which will encourage me to write more information in future@

Arrays in VBScript


An Array variable can have different data type values like int, char, string, Boolean etc
An Array variable values can be assigned or retrieved by using it's index, index can not be -ve.









An Array variable can be single/multi dimension: if multi dimension variable, can have up to 60 dimension.

Static Array variable -

'Declaration of a single dimension array variable
  Dim MyArray()
'Assigning values to an array
MyArray(0) = 1
MyArray(1) = 2.5
MyArray(2) = 's'
MyArray(3) = "VBScript"
...
(or)
MyArray = Array(1,2.5,"s","VBScript",#12/03/2013#)

Dynamic Array variable - 

  Dim MyArray()

'Declaring a dynamic array with size using REDIM keyword
  ReDim MyArray(5) / MyArray(3,6)

'Preserving the previous values of variable MyArray and expanding the size further
  ReDim Preserve MyArray(10) / MyArray(10,20)

Note: If we do not use Preserve keyword and expand the size, then previous stored data will be empty.

Accessing each array values using loops -
1) For...Next
For i=0 to UBound(MyArray)
MsgBox MyArray(i)
Next
2) For Each…Next
For Each Arr in MyArray
MsgBox Arr
Next

NOTE: how to get the UBound of a multi dimensional array
Dim Arr2(3,2)
Ubound(Arr, 1) = 3 ' The Largest Subscript of the first dimension in Arr
UBound(Arr, 2) = 2 ' The Largest Subscript of the second dimension in Arr

@Please feel free to comment which will encourage me to write more information in future@

How to avoid scripts running recursively in QTP -

1) Check if any data present in Data Table? if yes, try clearing those values (delete or clear cells by right clicking on data table cells).
2) go to file -> setting and select run on iterations only option.

How to get the creation date & time of a particular file

Set f1 = CreateObject("Scripting.FileSystemObject")
Set f2 = f1.GetFile("D:\QTP_TEST\test.docx")
S = "File was Created on: "&f2.DateCreated
Msgbox S

How to list name of subfolders of a folder

Let's assume, we have D:\PFolder where 3 subfolders present named as sun1, sun2 & sun3
We will use FileSystemObject as class name to deal with folders.

Set a = CreateObject("Scripting.FileSystemObject")
Set b = a.GetFolder("D:\QTP_TEST")
Set c = b.SubFolders
For Each d in c
e=e&d.name&vbnewline
Next
msgbox e