Protected by Copyscape
Powered By Blogger

Friday, January 17, 2020

How to get the names of the sub-folders and sub-files with in a folder?

We can achieve this through recursion(a function can call itself) as we don't know the depth of the main folder means we don't know how many sub-folders and files exists in a single folder but the below code will help us to resolve this.

Dim objTxt
Set objTxt=CreateObject("Scripting.FileSystemObject")

Sub showSubFolder(Folder)
  For Each subfolder in Folder.SubFolders
  MsgBox "The path of the child folder is :"& subFolder.Path
  Set objFolder=objTxt.GetFolder(subfolder.Path)
   Set colFiles=objFolder.Files
   For Each objfiles in colFiles
     MsgBox objfiles.Name
   Next
   showSubFolder subfolder
 Next
End Sub


Call showSubFolders(objTxt.GetFolder("c:/abc"))


set objTxt=Nothing

No comments: