List Folders And Files


This article is written by Pon Saravanan  on 03-Jul-07 Last modified on :02-Jun-10





List files and folders including subfolders using VB 6.0

While programming file, folders and IO related applications, scanning a directory is an important task which may need frequently too. Sometimes we may need to list files in the directory as well the subfolder also. In .Net this is very easy, you need to specify a parameter to include the subfolders. It does all internally and you will get access to all the folders from a single function itself.

How to List Folder and List Files

This Function loops thro the folder and scan for all objects if it is a file it List files and if it is a directory it goes in to the directory and scans further down. Hence entire directory structure can be scanned and able to List files and List Folders including subfolders.

This is a very basic and simple function and can be customized to cater for suitable requirements.

• If only folder name needs to be printed then comment the line in file listing.
• If only file name needs to be printed then do the above in folder listing

This is a recursive function. Modifications to this function should be taken with care using proper breaking of recursion.

Source Code

'***************************************************************
'*  Purpose :  List Files and List Folders including sub folder
'*
'*  Inputs  :   strPath(String)           Path to folder
'*
'*  Returns :   list of items in the folder
'*
'****************************************************************
Public Sub ListFolders(strFolder As String)
    Dim fold As Scripting.Folder
    Dim foldSub As Scripting.Folder
    Dim fil As File
    Dim fso As New FileSystemObject
    Dim strOutput As String
   
    On Error GoTo ErrTrap
    Set fold = fso.GetFolder(strFolder)
    For Each fil In fold.Files
        'to get file name
        Debug.Print fil.Path
        DoEvents
    Next
    For Each foldSub In fold.SubFolders
        'to get folder name
        Debug.Print foldSub.Path
        DoEvents
        ListFolders foldSub.Path
    Next
ErrTrap:
    If Err Then
        Err.Raise Err.Number, _
        , "Error form Fucntions.ListFolders " _
        & Err.Description
    End If
End Sub

Note: Add reference to Microsoft Scripting runtime for using Scripting.Folder




- Next »






Comments
  • GUEST
    Nice Code 7/2/2009 2:09:49 AM

  • GUEST
    thank you very much...it works for me. 12/30/2010 1:39:06 PM

  • GUEST
    thank you thank you thank you !!!

    i have been looking for something this simple for days.. you wont believe the different examples I've tried, some with multiple forms and modules, some which crash, and some which are soo complicated I cant even begin to see how they work, let alone manage to strip them down to what i needed.
    you're a lifesaver
    6/26/2011 5:45:34 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    



Spam Bot Trap



   



Select Theme
White
Blue
Brown
Gray