Micro Focus QTP (UFT) Forums
Sample code: Search thorugh a Folder - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: Sample code: Search thorugh a Folder (/Thread-Sample-code-Search-thorugh-a-Folder)



Sample code: Search thorugh a Folder - Halemani - 08-17-2012

Hi all,
I was just trying how to search through a given folder. Find all its sub-folders and files. List them as well.
Here is a sample code I came up with. Hope someone can use it.
---------------
Code:
Option Explicit
Dim folderspec
folderspec = "G:\ProjectQTP"  'Get the path of the folder you want to be searched
Print Mid(folderspec, InStrRev(folderspec, "\")+1) 'Print the folder you are searching
Call ShowFolderList(folderspec, 0) 'Call the function to search along with the Tab counter, used to print tabs after each folder. To simulate a tree structure (Easy to see)

Function ShowFolderList(folderspec, tabCnt)
   Dim fso, f, f1, f2, s, sf, Counter, Spaces
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.GetFolder(folderspec)
   Set sf = f.SubFolders
    For Each f1 in sf
        tabCnt = tabCnt + 1
          For Counter = 1 to tabCnt  'Used to print number of dashes
            Spaces = Spaces & "- " 'Prints the dashes to make it more readable
        Next
        print  Spaces & f1.name   'Print the parent folder's name
        Spaces = Empty  
        Call ShowFolderList(f1.Path, tabCnt)    'If a sub-folder is found then call this function again to traverse thorugh that sub-folder to find other folders untill the last folder is traversed.
        tabCnt = tabCnt - 1  'Reduce the count to go back to the parent folder's tabCnt
    Next
    
   Set s = f.Files
    For Counter = 1 to (tabCnt+1) 'Files should have a different space counter
        Spaces = Spaces & "- "
    Next
    For Each f2 in s
        Print Spaces & f2.name  'Print the folder's files
    Next
        
End Function
--------------------------