Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2007
    Location
    Global Village
    Posts
    185

    Question Unanswered: Search & Find Files

    Hi Folks
    I need to know how programmatically in VBA to search in a directory for list of specific files (for example *.msg) and put it in an array.
    I tried Dir() function but it gets only the first file as far as I know
    I know with Findfilefirst and findfilenext in API it is possible but I'm not familiar with API.
    If anybody has any suggestion I appreciate to know
    Thanks

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    You want to take a look at the FileSystemObject
    Code:
    Set fso = CreateObject("Scripting.FileSystemObject")
    Or the Application.FileSearch method

    The below code is straight out of the help files
    Code:
    With Application.FileSearch
        If .Execute() > 0 Then
            MsgBox "There were " & .FoundFiles.Count & _
                " file(s) found."
            For i = 1 To .FoundFiles.Count
                MsgBox .FoundFiles(i)
            Next i
        Else
            MsgBox "There were no files found."
        End If
    End With
    Personally I've never used the FileSearch, always the FSO (which is, IMHO, a zillion times better)
    George
    Home | Blog

  3. #3
    Join Date
    Dec 2004
    Location
    Madison, WI
    Posts
    3,926
    I agree with Georgev - the FSO is the best.
    Expert Database Programming
    MSAccess since 1.0, SQL Server since 6.5, Visual Basic (5.0, 6.0)

  4. #4
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    I agree with PK ^^
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  5. #5
    Join Date
    Sep 2007
    Location
    Global Village
    Posts
    185
    Thanks to everybody, I agree with you, I didn't think about FSO, but I found something about Dir() function which is interesting and I will put it here for those who read this threat and don't know about it.
    After execution of dir() with path and attribute "i.e. dir( "some path" & "file name with wild cards",attribute)" if you execute Dir() without path and attribute then it give you next file match with your search.
    By repeating this you can get all files one by one.
    See the code:
    PHP Code:
    Public Sub searchFile()

    Debug.Print Dir("Search Path" "FileName*" ".fileExtention")
    Do 
    Until Dir() = ""
    Debug.Print Dir()
    Loop
    End Sub 

Posting Permissions

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