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, strPath

strDrive = "c:" 'Drive where you want to create the folder

strfoldername="test" 'Name of the folder to be created

strPath= 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 strPath

Set objFolder = objFSO.CreateFolder(strPath)

If err.Number = 58 then      'VB Script Run Time Error 58 -File Already exists

   msgbox "Folder  already exist  at" & strPath


End If

msgbox "Folder  created is at " & strPath

How to delete a folder?

Take the first 6 lines from the code above and add these lines

On Error Resume Next 'Incase folder is not found


If  err.Number =76 then      'VB Script Run Time Error 76 -File Not Found

     msgbox "Folder  Not Found at " & strPath


End If

msgbox "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” then

msgbox “Folder Already Exists”

End If

If you want to keep track of further articles on UFT (QTP). I recommend you to subscribe by Email and have new UFT articles sent directly to your inbox.

Subscribe to get free updates on UFT!

LearnQTP is the most popular site on UFT (formerly QTP).

Enter your first name and email address below to instantly subscribe and download an eBook on Optimizing UFT Scripts. In future, we will make sure you get new tips & tricks on UFT delivered direct to your email box.

Please check your email and confirm free subscription!