-
Thanks xld. It works however it only search on the main Dir and doesn't search thru sub folders, digging deeper the solution is a recursive searching using this one. I make it work but again I failed to incorporate.
Usage Example:
[vba]
Dim colFiles As New Collection
RecursiveDir colFiles, "C:\Photos", "*.jpg", True
Dim vFile As Variant
For Each vFile In colFiles
Debug.Print vFile
Next vFile
[/vba]
Function
[vba]
Public Function RecursiveDir(colFiles As Collection, _
strFolder As String, _
strFileSpec As String, _
bIncludeSubfolders As Boolean)
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add files in strFolder matching strFileSpec to colFiles
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colFiles.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Fill colFolders with list of subdirectories of strFolder
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 RecursiveDir for each subfolder in colFolders
For Each vFolderName In colFolders
Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
Next vFolderName
End If
End Function
Public Function TrailingSlash(strFolder As String) As String
If Len(strFolder) > 0 Then
If Right(strFolder, 1) = "\" Then
TrailingSlash = strFolder
Else
TrailingSlash = strFolder & "\"
End If
End If
End Function
[/vba]
This also catch my interest but I dont understand much the code.
[vba]
Sub Recurse_Dir(mypath As String, Why As String)
Dim DirList() As Variant
Dim counter As Integer
Dim MyName As String
counter = 0
ReDim DirList(1)
MyName = Dir(mypath, vbDirectory)
Do While MyName <> "" ' Start the loop.
If MyName <> "." And MyName <> ".." And InStr(1, MyName, "%") = 0 Then
If (GetAttr(mypath & MyName) And vbDirectory) = vbDirectory Then
ReDim Preserve DirList(counter)
DirList(counter) = mypath & MyName
counter = counter + 1
Else
If UCase(ActiveDocument.Name & ".rep") <> UCase(MyName) Then
If Why = "Report" Then
Call CheckTheFile(mypath, MyName)
Else
CheckUNV (mypath & MyName)
End If
End If
End If
End If
MyName = Dir ' Get next entry.
Loop
If counter = 0 Then Exit Sub
Call Recurse_Dir((DirList(counter)) & "\", Why)
Next
End Sub
[/vba]
Last edited by defcon_3; 06-04-2012 at 06:46 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules