Sunday 22 September 2013

How to get list of all files and folders starting from a specific path - Visual Basic 6.0

option explicit
private fs as Object     ' declared for filesystem object
private flds as Object    ' declared for main folder object
private fld as Object    ' declared for folder object
private fl as Object      ' declared for file object

private sub ListFolderAndFiles(pathname as String)
   create file system object
   set fs=CreateObject("Scripting.FileSystemObject")

   'exit if fails to create file system object
   if fs is nothing then exit sub

  ' exit is folder does not exists in the pathname
  if not fs.isFolderExists(pathname) then
      msgbox "Ivalid path or folder name"
      exit sub
  end if
 
   ' Set the folder object from the specified path
   set flds=fs.GetFolder(pathName)
  
   ' exit if failed to create folder object
   if not flds is nothing then exit sub

  ' enumerate through all folders in the parent folder
   for each fld in flds.Folder
     ' enumerate through all files in the folder
       for each fl in fld.Files
           ' add filename along with it's containing folder name  in the listbox
           call list1.AddItem(fld.Name & "-" & fl.Name)
       next fl
   next fld
 
   'release object from memory
   set flds=nothing
   set fl=nothing
   set fs=nothing
End Sub

Private Sub Form_Load()
     ' Calling procedure
     call ListFolderAndFiles("c:\MyLibrary")End Sub

No comments: