Results 1 to 6 of 6

Thread: need to collect files in a folder and all its subfolders

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Mayaguez, PR
    Posts
    7

    Question need to collect files in a folder and all its subfolders

    Im tryng to load filenames to a list. The problem is that I cant seem to find a way to browse subfolders within a folder. I mean, if i wanted to add all files in the windows directory, the program would also add all files contained in all of the windows folder's subdirectories, and on all their respective subdirectories.

  2. #2
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819
    What do you mean by a list? Are you just trying to create an array or do you want to display the list of directories so that you can select from them? If the latter, you can use a DirListBox which will do all the hard work for you...
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  3. #3
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    This has been answered by me several times in this forum, you can do a search to find it, I'd say: SUBFOLDER, but I'll do the work for ye ...
    Thread:
    http://www.vbforums.com/showthread.p...threadid=91531 (to get all subfolders using FSO)
    Code:
    Dim FSO As FileSystemObject
    Dim fFolder As Folder
    Dim fRoot As Folder
    
    Private Sub Form_Load()
        Set FSO = New FileSystemObject
        ShowFolders ("C:\")
    End Sub
    
    Private Sub ShowFolders(Path As String)
        Set fRoot = FSO.GetFolder(Path)
        For Each fFolder In fRoot.SubFolders
            MsgBox fFolder.Name
            ShowFolders(fFolder.Path)
        Next
    End Sub
    All you have to do is throw in file capabilities.
    Code:
    Dim FSO As FileSystemObject
    Dim fFolder As Folder
    Dim fRoot As Folder
    Dim fFile As File
    
    Private Sub Form_Load()
        Set FSO = New FileSystemObject
        ShowFolders ("C:\")
    End Sub
    
    Private Sub ShowFolders(Path As String)
        Set fRoot = FSO.GetFolder(Path)
        For Each fFolder In fRoot.SubFolders
            MsgBox fFolder.Name
            GetFiles(Path)
            ShowFolders(fFolder.Path)
        Next
    End Sub
    
    Private Sub GetFiles(Path As String)
        Dim fSub As Folder
        Set fSub = FSO.GetFolder(Path)
        For Each fFile In fSub.Files
            MsgBox fFile.Name
        Next
    End Sub
    Now all you have to do is change the MsgBox commands to List1.AddItem commands.
    You will need the microsoft scripting runtime added to your references or use the createobject function.
    -Excalibur

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Mayaguez, PR
    Posts
    7
    yeah im trying to load the names on a list so the user selects which files to process. I knew about the filesystemobject, but i coudn't figure out how to make it work with all subfolders. I'll give it a try sometime tonite . . . THANX A WHOLE LOT MAN!!!!

    =)

  5. #5
    Addicted Member
    Join Date
    Feb 2001
    Location
    NJ
    Posts
    148
    Here is code using the API.

    I've tested this code against using FSO and using the Dir function. This code is much faster than the other two methods.

    Copy it into a class module. There is a function to return all
    results in an array and one to populate a listbox. Let me know what you think.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Originally taken from Rick Meyer at [url]http://pages.about.com/vbmakai/getfiles.htm[/url]
    4. 'Code Greatly Modified by Mike Rossi
    5.  
    6. '==========================================
    7. ' These are the API declarations needed for
    8. '   the file searching operations
    9. '==========================================
    10. Const FILE_ATTRIBUTE_NORMAL = &H80
    11. Const FILE_ATTRIBUTE_HIDDEN = &H2
    12. Const FILE_ATTRIBUTE_SYSTEM = &H4
    13. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    14.  
    15. Private Type FILETIME
    16.   dwLowDateTime     As Long
    17.   dwHighDateTime    As Long
    18. End Type
    19.  
    20. Private Type WIN32_FIND_DATA
    21.   dwFileAttributes  As Long
    22.   ftCreationTime    As FILETIME
    23.   ftLastAccessTime  As FILETIME
    24.   ftLastWriteTime   As FILETIME
    25.   nFileSizeHigh     As Long
    26.   nFileSizeLow      As Long
    27.   dwReserved0       As Long
    28.   dwReserved1       As Long
    29.   cFileName         As String * 260
    30.   cAlternate        As String * 14
    31. End Type
    32.  
    33. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" ( _
    34.     ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    35.    
    36. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" ( _
    37.     ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    38.  
    39. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    40.  
    41. '==========================================
    42. ' These are the API declarations needed for
    43. '   adding the listbox horizontal scrollbar
    44. '==========================================
    45. Const LB_SETHORIZONTALEXTENT = &H194
    46.  
    47. Private Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" ( _
    48.     ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    49.  
    50. Dim maxWdth As Long
    51.  
    52. Public Sub arrayGetAllFiles(ByVal strDir As String, ByVal Extension As String, ByRef Results() As String, Optional NumFound As Long)
    53.    
    54. Dim fPath$, fName$, fPathName$
    55. Dim hfind&, nameLen%, matchLen%
    56. Dim WFD As WIN32_FIND_DATA
    57. Dim found As Boolean
    58.  
    59.   fPath = strDir
    60.   If Right(fPath, 1) <> "\" Then
    61.     fPath = fPath & "\"
    62.   End If
    63.    
    64.   matchLen = Len(Extension)
    65.   Extension = LCase$(Extension)
    66.  
    67.   'The first API call is FindFirstFile
    68.   hfind = FindFirstFile(fPath & "*", WFD)
    69.   found = (hfind > 0)
    70.  
    71.   Do While found
    72.  
    73.     fName = TrimNull(WFD.cFileName)
    74.     nameLen = Len(fName)
    75.     fPathName = fPath & fName
    76.    
    77.     If fName = "." Or fName = ".." Then
    78.  
    79.     ElseIf WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then arrayGetAllFiles fPathName, Extension, Results(), NumFound
    80.        
    81.     ElseIf matchLen > nameLen Then
    82.    
    83.     ElseIf LCase$(Right$(fName, matchLen)) = Extension Then
    84.       ReDim Preserve Results(NumFound)
    85.       Results(NumFound) = fPathName
    86.       NumFound = NumFound + 1
    87.     End If
    88.    
    89.     'Subsequent API calls to FindNextFile
    90.     found = FindNextFile(hfind, WFD)
    91.    
    92.   Loop
    93.  
    94.   'Then close the findfile operation
    95.   FindClose hfind
    96.  
    97. End Sub
    98.  
    99. Public Sub lstBoxGetCurrentDir(ByVal strDir As String, ByVal Extension As String, FormName As Form, ListBoxName As ListBox, Optional NumFound As Long, Optional ResetMaxWidth As Boolean = True)
    100.  
    101. Dim fName As String
    102. Dim fFile As String
    103.  
    104.   If ResetMaxWidth = True Then maxWdth = 0
    105.  
    106.   fName = Dir(strDir & "*." & Extension)
    107.   Do Until fName = ""
    108.     fFile = strDir & fName
    109.     ListBoxName.AddItem fFile
    110.     If FormName.TextWidth(fFile) > maxWdth Then
    111.       maxWdth = FormName.TextWidth(fFile)
    112.     End If
    113.     NumFound = NumFound + 1
    114.     fName = Dir
    115.   Loop
    116.  
    117. End Sub
    118. Public Sub lstBoxGetAllFiles(ByVal strDir As String, ByVal Extension As String, FormName As Form, ListBoxName As ListBox, Optional NumFound As Long, Optional ResetMaxWidth As Boolean = True)
    119.    
    120. Dim fPath$, fName$, fPathName$
    121. Dim hfind&, nameLen%, matchLen%
    122. Dim WFD As WIN32_FIND_DATA
    123. Dim found As Boolean
    124.  
    125.   If ResetMaxWidth = True Then maxWdth = 0
    126.  
    127.   fPath = strDir
    128.   If Right(fPath, 1) <> "\" Then
    129.     fPath = fPath & "\"
    130.   End If
    131.    
    132.   matchLen = Len(Extension)
    133.   Extension = LCase$(Extension)
    134.  
    135.   'The first API call is FindFirstFile
    136.   hfind = FindFirstFile(fPath & "*", WFD)
    137.   found = (hfind > 0)
    138.  
    139.   Do While found
    140.  
    141.     fName = TrimNull(WFD.cFileName)
    142.     nameLen = Len(fName)
    143.     fPathName = fPath & fName
    144.    
    145.     If fName = "." Or fName = ".." Then
    146.  
    147.     ElseIf WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then lstBoxGetAllFiles fPathName, Extension, FormName, ListBoxName, NumFound, False
    148.        
    149.     ElseIf matchLen > nameLen Then
    150.    
    151.     ElseIf LCase$(Right$(fName, matchLen)) = Extension Then
    152.       ListBoxName.AddItem fPathName
    153.       NumFound = NumFound + 1
    154.       If FormName.TextWidth(fPathName) > maxWdth Then
    155.         maxWdth = FormName.TextWidth(fPathName)
    156.       End If
    157.     End If
    158.    
    159.     'Subsequent API calls to FindNextFile
    160.     found = FindNextFile(hfind, WFD)
    161.    
    162.   Loop
    163.  
    164.   'Then close the findfile operation
    165.   FindClose hfind
    166.  
    167. End Sub
    168.  
    169. Private Function TrimNull(ByVal Item As String) As String
    170.    
    171. Dim pos As Integer
    172.    
    173.   pos = InStr(Item, Chr$(0))
    174.   If pos Then Item = Left$(Item, pos - 1)
    175.    
    176.   TrimNull = Item
    177.  
    178. End Function
    179.  
    180. Private Sub Class_Initialize()
    181.  
    182.   maxWdth = 0
    183.  
    184. End Sub
    185.  
    186. Public Sub SetHorizontalBar(FormName As Form, ListBoxName As ListBox)
    187.  
    188.   maxWdth = maxWdth + FormName.TextWidth("  ")
    189.   maxWdth = maxWdth / Screen.TwipsPerPixelX
    190.   'The API call to add the horizontal scrollbar
    191.   SendMessageByNum ListBoxName.hwnd, LB_SETHORIZONTALEXTENT, maxWdth, 0
    192.  
    193. End Sub



    ADO, SQL, Access, HTML, ASP, XML
    Visual Basic 6.0 SP5 Enterprise Edition
    VB.Net


  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Mayaguez, PR
    Posts
    7

    Smile thanx

    well im really not very familiar with the API, so to me all that code looks rather complex. I'll give it a try though, and i'll try to figure it out. thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width