Labels

Wednesday, 26 September 2012

QTP - Handling Files and Folders

QTP - Handling Files and Folders

Useful Methods:

Couple of useful methods available:
  • GetFolder
  • FolderExists
  • CopyFolder
  • FileExists

Public Function fileFn(ByVal fpath, ByVal fileName)

   Dim objnewFile
   Dim filePathName
  
   Set fso= CreateObject ("Scripting.FileSystemObject")
   print "******************************"
    '' Checking whether folder exits , otherwise create a new one
   If fso.FolderExists(fpath) Then
       print "Folder"&"-"&fpath&"..Exists"
    Else
        print "Folder does not exists"
        print "creating new folder"&"--"&fpath
        fso.createFolder(fpath)
   End If

    Set objFolder = fso.GetFolder(fpath)
    Set objFiles = objFolder.Files
    Set objSubFolder = objFolder.SubFolders
    print "Number of sub folders-"& objSubFolder.count
    print "Number of files-"& objFiles.count
    '' print the subfolders
    For each subfolder in objSubFolder
        print subfolder
    Next
    ' Get file name and last modified date and time
    For each nFile in objFiles
        print nFile &"--"& nFile.Path &"--"& nFile.DateLastModified
     Next
    print "New File"&fpath&"\"&fileName

    Set fso = Nothing
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objnewFile = Nothing
    filePathName = fpath&"\"&fileName
    print filePathName

    '' check file exists , if not create and append text
    If fso.FileExists(filePathName) Then
        Set objnewFile= fso.OpenTextFile (filePathName, 8, True) ' 8 - For appending
        objnewFile.writeLine "test555"
    else
        Set objnewFile=fso.createtextfile(filePathName)
        '        Set objTxt = fso.opentextfile (fpath&"\"&fileName, Forappending, true)
        objnewFile.writeLine "test"
    End If
    ''' Copying the file
    fso.CopyFile filePathName,filePathName&"1"
   
Set fso = Nothing
Set objFolder = Nothing
Set objFiles = Nothing
Set objnewFile = Nothing
Set objTxt = Nothing

End Function

res = fileFn("c:\data","newfile66.txt")
''fileFn("c:\data4")

No comments:

Post a Comment