Hello friends,
In this post we will discuss about working on Folders in UFT using DotNetFactory.
Introduction
.Net framework has an inbuilt class for handling all of the folder operations. We will utilize the same name space and classes to perform various operations on folders
Creating Folders
For creating folders use the following code
'@description This function will create folders, this function will accept two parameters. First is Folder path and second is the folder name Public Function CreateFolder(Byval FolderPath) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(FolderPath) Then Msgbox "Folder Already Exists" CreateFolder = false else ODirectory.CreateDirectory FolderPath CreateFolder = true End If End Function
We will first create the object of directory class and then use the function CreateDirectory for creating the folder.
Deleting Folders
For deleting folders use the below code
'@description This function will delete the folders. This function will take two arguments. First is the path of the folder to be deleted and second is, Should we delete child folders. Public Function DeleteFolder(ByVal FolderPath, ByVal Recursive) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(FolderPath) Then ODirectory.Delete FolderPath, Recursive DeleteFolder = true else Msgbox "Folder Does not exist" DeleteFolder = false End If End Function
We will first create the object of directory class and then use the function Delete for deleting the folder.
Moving Folders
For moving folders use the below code
'@description This function will move folders. This function will accept two parameters, source and destination folder paths Public Function MoveFolder(ByVal SourceFolder, ByVal TargetFolder) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(SourceFolder) Then If ODirectory.Exists(TargetFolder) Then Msgbox "Target Directory Already Exists" MoveFolder = false Else ODirectory.Move SourceFolder, TargetFolder MoveFolder = true End If else Msgbox "Source folder does not exist" MoveFolder = false End If End Function
We will first create the object of directory class and then use the function Move for moving the folders.
Finding child folders or sub directories
For finding sub directories use the below code
'@description This function will return the list of child folders within a specified folder Public Function GetChildFolderList(ByVal ParentFolderPath) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(ParentFolderPath) Then Set GetChildFolderList = ODirectory.GetDirectories(ParentFolderPath) '############ Access the folder names like this 'Set FoldersList = GetChildFolderList(StrPath) 'For LoopCounter = 0 To Cint(FoldersList.Length) - 1 Step 1 ' Msgbox FoldersList.GetValue(LoopCounter) ' If Err.Number <> 0 Then ' On Error Goto 0 ' Exit For ' End If 'Next else Msgbox "Folder does not Exist" GetChildFolderList = false End If End Function
We will first create the object of directory class and then use the function GetDirectories for listing the child folders. You will have to loop through the return object to get the folder names.
Finding the parent folder path
For finding the parent folder path use the below code
'@description This function will get the parent directory path Public Function GetParentDirectoryPath(FolderPath) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(FolderPath) Then GetParentDirectoryPath = Cstr(ODirectory.GetParent(FolderPath).FullName) Else GetParentDirectoryPath = "Folder Does not exist" End IF End Function
We will first create the object of directory class and then use the function GetParent for finding the parent folder path.
Consolidated code
CreateFolder "C:\temp\testingcode1" CreateFolder "C:\temp\testingcode1\test1" CreateFolder "C:\temp\testingcode1\test2" CreateFolder "C:\temp\testingcode2" CreateFolder "C:\temp\testingcode2\testing1" CreateFolder "C:\temp\testingcode2\testing2" MoveFolder "C:\temp\testingcode2", "C:\temp\testingcode1\MovedFolder" Set FoldersList = GetChildFolderList("C:\temp\testingcode1") For LoopCounter = 0 To Cint(FoldersList.Length) - 1 Step 1 Msgbox FoldersList.GetValue(LoopCounter) If Err.Number <> 0 Then On Error Goto 0 Exit For End If Next Msgbox GetParentDirectoryPath("C:\temp\testingcode1\MovedFolder") DeleteFolder "C:\temp\testingcode1\test1", false DeleteFolder "C:\temp\testingcode1", true '@description This function will create folders, this function will accept two parameters. First is Folder path and second is the folder name Public Function CreateFolder(Byval FolderPath) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(FolderPath) Then Msgbox "Folder Already Exists" CreateFolder = false else ODirectory.CreateDirectory FolderPath CreateFolder = true End If End Function '@description This function will move folders. This function will accept two parameters, source and destination folder paths Public Function MoveFolder(ByVal SourceFolder, ByVal TargetFolder) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(SourceFolder) Then If ODirectory.Exists(TargetFolder) Then Msgbox "Target Directory Already Exists" MoveFolder = false Else ODirectory.Move SourceFolder, TargetFolder MoveFolder = true End If else Msgbox "Source folder does not exist" MoveFolder = false End If End Function '@description This function will delete the folders. This function will take two arguments. First is the path of the folder to be deleted and second is, Should we delete child folders. Public Function DeleteFolder(ByVal FolderPath, ByVal Recursive) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(FolderPath) Then ODirectory.Delete FolderPath, Recursive DeleteFolder = true else Msgbox "Parent Folder Does not exist" DeleteFolder = false End If End Function '@description This function will return the list of child folders within a specified folder Public Function GetChildFolderList(ByVal ParentFolderPath) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(ParentFolderPath) Then Set GetChildFolderList = ODirectory.GetDirectories(ParentFolderPath) '############ Access the folder names like this 'Set FoldersList = GetChildFolderList(StrPath) 'For LoopCounter = 0 To Cint(FoldersList.Length) - 1 Step 1 ' Msgbox FoldersList.GetValue(LoopCounter) ' If Err.Number <> 0 Then ' On Error Goto 0 ' Exit For ' End If 'Next else Msgbox "Folder does not Exist" GetChildFolderList = false End If End Function '@description This function will get the parent directory path Public Function GetParentDirectoryPath(FolderPath) Set ODirectory = DotNetFactory.CreateInstance("System.IO.Directory","System") If ODirectory.Exists(FolderPath) Then GetParentDirectoryPath = Cstr(ODirectory.GetParent(FolderPath).FullName) Else GetParentDirectoryPath = "Folder Does not exist" End IF End Function
Suggestion
Create a class for handling the folder operations withing the framework you are using.
Let me know, if you need any help.
Thanks,
Sumeet Singh Kushwah