[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! :)
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
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).
Re: Check if file extension exists in folder using wildcard and retrieve file name?
Quote:
Originally Posted by
swambast
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
Re: Check if file extension exists in folder using wildcard and retrieve file name?
Quote:
Originally Posted by
swambast
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
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! :wave:
Re: Check if file extension exists in folder using wildcard and retrieve file name?
Quote:
Originally Posted by
swambast
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! :wave:
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.
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!