Results 1 to 3 of 3
  1. #1
    maxbre is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    38

    Unhappy list files recursively and store results in array

    I’m using with my great utility the vba code provided by allen browne web page to loop through the files in a folder and displaying results in a list box (http://allenbrowne.com/ser-59.html) or alternatively writing results into a table (http://allenbrowne.com/ser-59alt.html).

    now, my problem is that I would like to store results of file listing in an array (supposing it is the correct object for accomplishing my objectives) in order to later use it for further operations (a cycle for each file name); in short, I need to catch the reslts of the file listing and store them in an array (or a variable?)



    is it that possible or sensible?

    I'm lost now, sorry...

    any help or hint to go forward in the right direction is more than welcome

    max

  2. #2
    Rawb is offline Expert
    Windows XP Access 2000
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    Well, mine doesn't do it recursively, but it will give you a list of files and let you manage them however you want:

    Code:
    Public Function GetFiles(colDirList As Collection, ByVal FilePath As String, FileName As String) As Boolean
      Dim strTemp As String
      Dim colFolders As New Collection
    
      FilePath = TrailingSlash(FilePath)
      strTemp = Dir(FilePath & FileName)
    
      Do While strTemp <> vbNullString
        colDirList.Add FilePath & strTemp
        strTemp = Dir
      Loop
    End Function
    Public Function TrailingSlash(FilePath As Variant) As String
      If Len(FilePath) > 0& Then
        If Right(FilePath, 1&) = "\" Then
          TrailingSlash = FilePath
        Else
          TrailingSlash = FilePath & "\"
        End If
      End If
    End Function
    The Function GetFiles() returns a Collection (basically an array, just a little fancier) that includes all files in a Folder. The Function TrailingSlash() just makes sure there's a backslash at the end of your Folder name so that it can properly append the filename.

    Example (simple) usage:
    Code:
      ' Lists all files in the Folder C:\WINDOWS\Temp that have a .TMP extension and lists them (one at a time) in a Message Box.
      Dim colDirList As New Collection
    
      Dim strFilename as String
      Dim strFolder as String
    
      Dim varItem As Variant
    
      strFolder = "C:\WINDOWS\Temp" ' Doesn't matter if you include the trailing slash or not!
      strFilename = "*.tmp" ' Can use Wildcards!
    
      Call GetFiles(colDirList, strFolder, strFile) ' populates our Collection
      
      For Each varItem In colDirList
        ' varItem includes the full Folder & Filename all as a single string
        MsgBox varItem
      Next varItem

  3. #3
    maxbre is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Oct 2010
    Posts
    38
    yeeesssssssss, it works like a treat!

    it's perfect for my needs

    thank you so much

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Full app - find/store/open files in a table of any type
    By pkstormy in forum Sample Databases
    Replies: 2
    Last Post: 02-03-2012, 01:19 PM
  2. Populate Combo Box with list of files
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 02-22-2011, 09:28 AM
  3. Replies: 1
    Last Post: 02-21-2011, 09:55 PM
  4. Load SQL results into an array
    By Tyork in forum Programming
    Replies: 8
    Last Post: 01-24-2011, 01:58 PM
  5. How To View List of Files in Box
    By treyprice in forum Forms
    Replies: 3
    Last Post: 01-14-2010, 03:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums