Just adding to this PAQ.
I'm not sure if VBA supports this also, but VB's Dir() function supports the use of wildcards (ie: c:\*.txt). It'll make things much simpler.
Form1:
-----------------
Option Explicit
Private Sub Command1_Click()
Dim files() As String
If (FindFiles("c:\windows\sys
MsgBox "Files found: " & UBound(files) & vbCrLf & vbCrLf & _
"1st file: " & files(0) & vbCrLf & _
"2nd file: " & files(1)
End If
End Sub
Private Function FindFiles(ByVal path As String, ByVal ext As String, ByRef files() As String) As Boolean
Dim ffile As String
ffile = Dir$(path & "*." & ext)
Do
If (ffile <> vbNullString) Then
If (FindFiles = False) Then
ReDim files(0) As String
FindFiles = True
Else
ReDim Preserve files(UBound(files) + 1) As String
End If
files(UBound(files)) = ffile
ffile = Dir
Else
Exit Do
End If
Loop Until (ffile = vbNullString)
End Function
Main Topics
Browse All Topics





by: iHadiPosted on 2006-05-24 at 23:42:24ID: 16758316
Hi tlwaller
Replace your GetAllFilesInDir function with the following modified one:
Function GetAllFilesInDir(ByVal strDirPath As String) As Variant
' Loop through the directory specified in strDirPath and save each
' file name in an array, then return that array to the calling
' procedure.
' Return False if strDirPath is not a valid directory.
Dim strTempName As String
Dim varFiles() As Variant
Dim lngFileCount As Long
On Error GoTo GetAllFiles_Err
' Make sure that strDirPath ends with a "\" character.
If Right$(strDirPath, 1) <> "\" Then
strDirPath = strDirPath & "\"
End If
' Make sure strDirPath is a directory.
If GetAttr(strDirPath) = vbDirectory Then
strTempName = Dir(strDirPath, vbDirectory)
Do Until Len(strTempName) = 0
' Exclude ".", "..".
If (strTempName <> ".") And (strTempName <> "..") Then
' Make sure we do not have a sub-directory name.
If (GetAttr(strDirPath & strTempName) _
And vbDirectory) <> vbDirectory Then
' Increase the size of the array
' to accommodate the found filename
' and add the filename to the array.
If Right(strTempName,4) = ".dwg" then
ReDim Preserve varFiles(lngFileCount)
varFiles(lngFileCount) = strTempName
lngFileCount = lngFileCount + 1
End If
End If
End If
' Use the Dir function to find the next filename.
strTempName = Dir()
Loop
' Return the array of found files.
GetAllFilesInDir = varFiles
End If
GetAllFiles_End:
Exit Function
GetAllFiles_Err:
GetAllFilesInDir = False
Resume GetAllFiles_End
End Function