Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sample code: Search thorugh a Folder
#1
Not Solved
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
--------------------------
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  CyberArk CorePAS REST API to search for accounts pramod 0 837 06-29-2023, 03:11 PM
Last Post: pramod
  Executing via create "RES"xx folder? nishitd 0 1,245 03-07-2018, 01:09 AM
Last Post: nishitd
  code for validation to create a sub folder smiley 0 1,541 10-19-2016, 10:08 PM
Last Post: smiley
  UFT 12.53 trial version not recognizing its sample flight application login/password NehaRB 2 3,907 10-07-2016, 06:49 PM
Last Post: pradeep537
  [UFT] Close opened folder robertosalemi 1 3,336 02-10-2016, 07:06 PM
Last Post: robertosalemi

Forum Jump:


Users browsing this thread: 2 Guest(s)