Extending upon one of previous posts on QTP and File handling, I would quickly tell you how to handle folders with QTP.
For all the examples shown below I will assume that the file to be created/deleted is at C: drive.
How to create a folder with QTP?
Dim strDrive, strfoldername,objFSO, objFolder, strPathstrDrive = "c:" 'Drive where you want to create the folderstrfoldername="test" 'Name of the folder to be createdstrPath= strDrive&strfoldername' Create FileSystemObject. We have already seen this in the earlier post.Set objFSO = CreateObject("Scripting.FileSystemObject")On Error Resume Next ' Incase folder already exist' Create a Folder, using strPathSet objFolder = objFSO.CreateFolder(strPath)If err.Number = 58 then 'VB Script Run Time Error 58 -File Already existsmsgbox "Folder already exist at" & strPathexitTestEnd Ifmsgbox "Folder created is at " & strPath
How to delete a folder?
Take the first 6 lines from the code above and add these linesOn Error Resume Next 'Incase folder is not foundobjFSO.DeleteFolder(strPath)If err.Number =76 then 'VB Script Run Time Error 76 -File Not Foundmsgbox "Folder Not Found at " & strPathexitTestEnd Ifmsgbox "Folder is deleted from " & strPath
How to find out if a folder exists on a drive?
Instead of using the err object as shown above, you can simply use .FolderExists()If objFSO.FolderExists(strpath)= “True” thenmsgbox “Folder Already Exists”End If
Ankur JainFounder at LearnQTPLoves blogging and automation.
Latest posts by Ankur Jain (see all)
- Free Webinar: The Latest In Mobile Test Automation & Extension to QTP - April 20, 2013
- MD5 Checksum: Verify Integrity of UFT(QTP) Download - December 28, 2012
- UFT(QTP) 11.5 is available for download - December 21, 2012