Code:
Public Function ListFiles2(strPath As String, Optional strFileSpec As String, _
Optional bIncludeSubfolders As Boolean)
On Error GoTo Err_Handler
Dim td As TableDef, S As String
Dim i As Long
i = 1
'Purpose: List the files in the path.
'Arguments: strPath = the path to search.
' strFileSpec = "*.*" unless you specify differently.
' bIncludeSubfolders: If True, returns results from subdirectories of strPath as well.
' lst: if you pass in a list box, items are added to it. If not, files are listed to immediate window.
' The list box must have its Row Source Type property set to Value List.
'Method: FilDir() adds items to a collection, calling itself recursively for subfolders.
Dim colDirList As New Collection
Dim varItem As Variant
Call FillDir2(colDirList, strPath, strFileSpec, bIncludeSubfolders)
'Add the files to a list box if one was passed in. Otherwise list to the Immediate Window.
'THIS FOR-EACH LOOP HAS ALL OF MY CODE IN IT. YOU DO WHATEVER YOU WANT WITH EACH FILE IT RUNS THROUGH.
'OTHER THAN THE LOOP CODE, ALL OF IT IS MINE AND NOT PART OF ALLEN BROWNE'S ORIGINAL POSTING ON HIS SITE.
For Each varItem In colDirList
Name CStr(varItem) As Replace(CStr(varItem), ", ", "__")
's = TxtReadAll1(CStr(varItem))
's = Replace(s, """", "_DOUBLE_QUOTES_")
'Call TxtWrite1(s, CStr(varItem))
'Name varItem As Replace(varItem, ",", "__")
'DoCmd.TransferText acImportDelim, , _
"_" & Mid(CStr(varItem), (InStrRev(CStr(varItem), "\") + 1), _
((InStrRev(CStr(varItem), ".") - (InStrRev(CStr(varItem), "\") + 1)))), _
CStr(varItem), 0
If Len(CStr(i / 10)) = 1 Then
MsgBox "break"
End If
's = "_" & Mid(CStr(varItem), InStrRev(CStr(varItem), "\") + 1, _
(Len(Mid(CStr("c:\dosf\assoc.txt"), _
InStrRev(CStr("c:\dosf\assoc.txt"), "\") + 1)) - 4))
'Debug.Print s & " " & CStr(i)
i = i + 1
Next
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & err.Number & ": " & err.Description
Resume Exit_Handler
End Function
Private Function FillDir2(colDirList As Collection, ByVal strFolder As String, strFileSpec As String, _
bIncludeSubfolders As Boolean)
'Build up a list of files, and then add add to this list, any additional folders
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add the files to the folder.
strFolder = TrailingSlash2(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colDirList.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Build collection of additional subfolders.
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0& Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop
'Call function recursively for each subfolder.
For Each vFolderName In colFolders
Call FillDir2(colDirList, strFolder & TrailingSlash2(vFolderName), strFileSpec, True)
Next vFolderName
End If
End Function
Public Function TrailingSlash2(varIn As Variant) As String
If Len(varIn) > 0& Then
If Right(varIn, 1&) = "\" Then
TrailingSlash2 = varIn
Else
TrailingSlash2 = varIn & "\"
End If
End If
End Function