Monday, April 26, 2010

IN Built VB Script Functions

As far as VB Scripting for QTP is concerned we will look at the four types of In Built Functions of VB scripting since we use them very often in QTP while writing scripts. The four inbuilt functions are

1. Data & Time Function
2. String Function
3. Conversion function
4. Array Function


Date & time Function

CDate: Converts a valid date and time expression to the variant of subtype Date
Date: Returns the current system date
DateAdd: Returns a date to which a specified time interval has been added
DateDiff : Returns the number of intervals between two dates

Example of DateDiff is

Msgbox datediff("d", #mm/dd/yyyy#, Now)

Here d refers Day. This will print the difference in no of days.

String Function

With a String function we can scan the entire string and also get a count of the no of characters in the string. If needed we can use the Left and Right method to get a particular no of characters either from the left or right side of the string. You can also replace the string with a new value.

Str = “This is Friday”
Str = Right (str, 20)
Str = Left (str, 8)

The Left and Right Methods scan the string from Left/Right and prints the specified no of characters.

Str = “Hello World”
Arr = split(str, "o")
Msgbox arr(0)
Msgbox arr(1)
Msgbox arr(2)


In the first message box it will print 'Hell', In the Second Message box it will print 'W', in the third message box it will print 'rld'.

If you want to check whether a text file is there in a Folder then you need to use the split method to get the text file. After getting the folder using Get Folder and pointing to the files in the folder we need to write these lines of code in the For Next Loop.

For each f in fil
str = f.name
arr = split(str, ".")
If arr(1) = "txt" then
var = var&Vbnewline&f.name
End If
Next

Please look at the complete script for printing all the text files in a folder at

How to display all the Text Files in a Folder

No comments:

Post a Comment