File system handling using Vbscript in QTP


File system handling using Vbscript in QTP is another example of AOM model that is utilized to suit our needs to write logs especially to generate the customized result files. Scripting.Filesystem object is the claas which is utilized to cater to the needs of the object creation and hence its property access.

How to Create a new text file ?
How to Check if a specific file exist ?
How to write data to a file ?
How to Read line from file ?
How to Read Entire Text file ?

How to Close an open file ?
How to Open a specified file and returns an object that can be used to read from, write to, or append to the file ?
How to Copy a file ?
How to Delete a file ?
How to Create a Folder if it does not Exists ?
How to Count Number of Lines in file ?
dim objFso
' creating the file system object
set objFso = CreateObject ("Scripting.FileSystemObject")

' Create a new txt file

' Parameters:
' FilePath - location of the file and its name

Function CreateFile (StrFilePath)

   ' variable that will hold the new file object
   dim NewFile
   ' create the new text File
   set NewFile = objFso.CreateTextFile(StrFilePath, True)
   set CreateFile = NewFile
End Function

' Check if a specific file exist

' Parameters:
' FilePath - location of the file and its name

Function CheckFileExists (StrFilePath)

   ' check if file exist
   CheckFileExists = objFso.FileExists(StrFilePath)
End Function

' Write data to file

' Parameters:
' FileRef - reference to the file
' str - data to be written to the file

Function WriteToFile (byref FileRef,str)

   ' write str to the text file
   FileRef.WriteLine(str)
End Function

' Read line from file

' Parameters:
' FileRef - reference to the file

Function ReadLineFromFile (byref FileRef)

   ' read line from text file
   ReadLineFromFile = FileRef.ReadLine
End Function

' Read Entire Text File Test

' Parameters:
' FileRef - reference to the file

Function ReadTextFileTest(StrFilePath)

   Const ForReading = 1, ForWriting = 2, ForAppending = 8
   Dim fso, f, Msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(StrFilePath, ForReading)
   ReadTextFileTest = f.Readall()
End Function

' Closes an open file.

' Parameters:
' FileRef - reference to the file

Function CloseFile (byref FileRef)

   FileRef.close
End Function

' Opens a specified file and returns an object that can be used to read from, write to, or append to the file.

' Parameters:
' FilePath - location of the file and its name
' mode options are:
' ForReading - 1
' ForWriting - 2
' ForAppending - 8

Function OpenFile (StrFilePath,mode)

   ' open the txt file and retunr the File object
   set OpenFile = objFso.OpenTextFile(StrFilePath, mode, True)
End Function

' Copy file.

' Parameters:
' FileRef - reference to the file

Sub FileCopy ( StrFilePathSource,StrFilePathDest)

   ' copy source file to destination file
   objFso.CopyFile StrFilePathSource, StrFilePathDest
End Function

' Delete a file.

' Parameters:
' FileRef - reference to the file

Sub FileDelete ( StrFilePath)
   ' copy source file to destination file
   objFso.DeleteFile ( StrFilePath)
End Function

' Create Folder if Not Exists.

Function ReportFolderStatus(fldr)

   Dim fso, msg
   Set fso = CreateObject("Scripting.FileSystemObject")
   If Not (fso.FolderExists(fldr)) Then
   Set f = fso.CreateFolder(fldr)
   End If
End Function

ReportFolderStatus("C:\Viplav")

' Count Number of Lines in txt file

Function NumberOfLines(FileName)

   i = 0
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objTextFile = objFSO.OpenTextFile(FileName, ForReading)
   Do Until objTextFile.AtEndOfStream
   Redim Preserve arrFileLines(i)
   arrFileLines(i) = objTextFile.ReadLine
   i = i +1
   Loop
   NumberOfLines = UBound(arrFileLines)
   objTextFile.Close
End Function

msgbox NumberOfLines("C:\Viplav\myfile.txt")




For gaining more insights in the automation using QTP log on to below url :
Automation Testing Using QTP

Post a Comment

أحدث أقدم