Results 1 to 8 of 8

Thread: [RESOLVED] Check if file extension exists in folder using wildcard and retrieve file name?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    106

    Resolved [RESOLVED] Check if file extension exists in folder using wildcard and retrieve file name?

    Hi There,

    I use a quick boolean test to see if certain file extensions exist in a certain directory (i.e., are there any *.txt files in this directory yields either True or False).
    But what would be some suggestions on how to actually retrieve all the actual *.txt file names in this folder if the answer is True? Note, I'm not using Scripting Runtime references here so FSOs would not be ideal, but could be considered if that really turns out to be the best solution.
    Am I limited to a recursive search in that folder or is there an easier way I might be overlooking? Anyone have a good function for this by chance?
    Thanks for your time and any additional insight!
    Last edited by swambast; Aug 2nd, 2013 at 12:08 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Check if file extension exists in folder using wildcard and retrieve file name?

    No need for the FSO you can do this with the basic Dir$() function or you can use the API or you can use the FileListBox control.

    In the case of Dir$()
    Code:
    Msgbox Dir$("C:\MyFolder\*.txt")
    would display the first matching file in that folder that ends in txt or return an empty string if not found

    in the case of the filelistbox
    Code:
    File1.Pattern="*.txt"
    File1.Path="c:\Myfolder"
    would populate the list will all .txt files int he given folder

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    106

    Re: Check if file extension exists in folder using wildcard and retrieve file name?

    DataMiser, thank you kindly for your reply. Correct, I can get the filename from Dir$ but what's the easiest way to get the entire file path (sorry, I'm a little rusty with Dir).

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Check if file extension exists in folder using wildcard and retrieve file name?

    Quote Originally Posted by swambast View Post
    Anyone have a good function for this by chance?
    Code:
    Option Explicit    'In a BAS module
    
    Private Sub Main()
        Dim vFile As Variant
    
        For Each vFile In GetFileNames("C:\Some\Folder\*.txt")
            Debug.Print """" & vFile & """"
        Next
    End Sub
    
    Private Function GetFileNames(ByRef sFileSpec As String) As Collection
        Const FILE_ATTRIBS = vbArchive Or vbHidden Or vbReadOnly Or vbSystem
        Dim sFile As String, sPath As String
    
        Set GetFileNames = New Collection
        sPath = Left$(sFileSpec, InStrRev(sFileSpec, "\"))
        sFile = Dir(sFileSpec, FILE_ATTRIBS)
    
        Do While LenB(sFile)
            GetFileNames.Add sPath & sFile
            sFile = Dir
        Loop
    End Function
    Last edited by Bonnie West; Aug 2nd, 2013 at 02:20 PM. Reason: Eliminated If LenB()...
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Check if file extension exists in folder using wildcard and retrieve file name?

    Quote Originally Posted by swambast View Post
    DataMiser, thank you kindly for your reply. Correct, I can get the filename from Dir$ but what's the easiest way to get the entire file path (sorry, I'm a little rusty with Dir).
    It seems like in your first post, you already know the path to the files....."...exist in a certain directory.."
    The FileListBox, as suggested above, is real simple to use.
    Put a dirlistbox and a filelistbox on your form, set the filelistbox Pattern property to *.txt.
    add this code:
    Code:
    Private Sub Dir1_Change()
    File1.Path = Dir1.Path
    End Sub
    
    Private Sub Form_Load()
    File1.Path = App.Path
    Dir1.Path = App.Path
    End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    106

    Smile Re: Check if file extension exists in folder using wildcard and retrieve file name?

    All, there are some fantastic suggestions provided here that can work. This is such a great community indeed, always willing to help each other out. I want to sincerely thank those that took their time out and helped answer my question, I really appreciate it!

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Check if file extension exists in folder using wildcard and retrieve file name?

    Quote Originally Posted by swambast View Post
    All, there are some fantastic suggestions provided here that can work. This is such a great community indeed, always willing to help each other out. I want to sincerely thank those that took their time out and helped answer my question, I really appreciate it!
    And a way you can contribute immensely, is after you decide on a resolution, post that resolution (or post the one you decided to use). This aids folks who search the forum for similar issues. If you give them YOUR answer, they just might use the same process.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    106

    Re: [RESOLVED] Check if file extension exists in folder using wildcard and retrieve f

    Well here's the beauty of this post - I used ALL the solutions. The original one by DM served as my quick fix, then longer-term I could leverage the function. Your solution SamOscarBrown was the most creative and had me thinking the solution in a new way. So, in fact I added a FileList control also and started exploring how I could leverage it better, which actually led to an even more convenient experience with added functionality! THANKS AGAIN!

Tags for this Thread

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