Reading Filenames to an array

jamsta

Registered User.
Local time
Today, 20:16
Joined
Oct 29, 2009
Messages
26
Hi all,

I'm looking for a way to read the names of PDF files in a specified folder, and storing them in an array. There is a similar post in this forum already... but the answer doesn't quite give me what I'm after. Is there a straight-forward way of doing this? Any advice, greatly appreciated as always.

Jamsta.
 
Have a search for "Filesearch" either forum or access help, see where that gets you.
 
Here some code to do what you're asking. It may give you a sample to work with.
Code:
Public Sub GetFileList()

Const ArrTop As Integer = 300 'allow 301 files
Dim MyArray(ArrTop) As String
Dim strdrive As String
Dim file_name As String
strdrive = "I:\Mypdfs" '[COLOR="Red"][B]< my location for some pdfs[/B][/COLOR]
Dim i As Integer ' MyArray index for fill


On Error Resume Next


file_name = dir$(strdrive & "\*.pdf")  '[COLOR="Red"][B]you said pdf files[/B][/COLOR]
i = 0
Do While (Len(file_name) > 0)

    ' See if we should skip this file.
    If Not (file_name = ".") Or (file_name = "..") Then
        MyArray(i) = file_name
        Debug.Print "Myarray(" & i & ") contains " & file_name
        i = i + 1
    End If

    ' Get the next file.
    file_name = dir$()
Loop
End Sub
 
Thanks Jdraw. A concise answer... and a very specific answer to my problem. Consider yourself a star!
 
Yes very nice, except Dir is a "poor mans" solution to what should be filesearch.

Dont get me wrong, not knocking the sample, if it works and gets you where you need to go ... Great, Just that Filesearch is a bit more....
 
I would tend to agree with namliam.... example provided:

Code:
Public Sub GetFileList()

Const ArrTop As Integer = 300 'allow 301 files
Dim strfile(ArrTop) As String
Dim strdrive As String

strdrive = "C:\pdfs" 'location for PDF's

On Error Resume Next

        With Application.FileSearch
            'indicate a new search.
            .NewSearch
            'Tell the search where to start.
            .Lookin = strdrive           'this is the base path of your search
            'inform the search to also check Sub-Folders.
            .SearchSubFolders = True
            'Inform the search we're looking for a PDF file
            .FileName = "*.pdf"
            'Execute and sort the search....

            If .Execute(SortBy:=msoSortByFileName, _
                SortOrder:=msoSortOrderAscending) > 0 Then
                
                'A few optionals thrown in..........
                If .FoundFiles.Count > 1 Then
                
                    For i = 1 To .FoundFiles.Count
                        strfile(i) = .FoundFiles(i)
                    Next i
                    
                    MsgBox "There were more than one files found.", vbOkOnly                   
                Else
                    For i = 1 To .FoundFiles.Count
                        strfile(i) = .FoundFiles(i)
                    Next i
                End If
            
            Else
                MsgBox = "There were no files found.", vbOkOnly
            End If
                
        End With
 
Yes very nice, except Dir is a "poor mans" solution to what should be filesearch.

Dont get me wrong, not knocking the sample, if it works and gets you where you need to go ... Great, Just that Filesearch is a bit more....

This is exactly the issue that I was having with my MP3 file copy database. Unfortunately I was not able to get "Application.Filesearch" to work for my MP3 program using Access 2007, so I gave-up and simply used the MS-DOS DIR command.

When I saw this thread, I thought that there might be hope and I looked into the issue again. Regretfully the news is not good if you are using Access 2007. Bodisathva reported that "Since the Application.FileSearch method is depreciated or hidden for 2007, try the File System Object's FileExists method. This could actually accomplish the same thing in just a few more lines of code if you do it recursively.". Some additional narrative from Microsoft.

Microsoft link to Filesearch object.

What is also very irritating is that when you use Access help (filesearch), you get a bunch of gobbledygook that makes it appear that "filesearch" is available. Now if you enter Access help and look under the members in the Application Object you will not see "filesearch" listed as a member. Long story short, I wasted a lot of time trying to tweak "filesearch" to make it work.

In doing this, I also ran across the Access "DIR" function. At first thought this would seem to be equivalent to the MS-DOS DIR command; but it turns out to be a "poor mans" version. Here is the Microsoft write-up on the Access DIR function.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom