Selected Answer
Malkal
Revision 07 June 2023 (moved code portion from function to parent procedure)
Here's a simple macro which searched in a named folder then calls a function. That function checks subfolders in the folder and - for each subfolder- lists files and (iteratively) calls itself to find more files and subfolders within. They are printed in VB Project Explorer's Intermediate window.
Public ObjFolder As Object, objSubFile As Object
Public objFso As Object
Public objFldLoop As Object
Sub GetStructure()
' call the function to loop through folder and its subfolders
Set objFso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = objFso.GetFolder("C:\DATA\")
' loop through this folder
For Each objSubFile In ObjFolder.Files
If InStr(objSubFile.Name, ".pdf") > 0 Then
Debug.Print objSubFile.Path
End If
Next objSubFile
' call the function to loop through its subfolders (and theirs)
LoopEachFolder ObjFolder
' quit FSO
Set objFso = Nothing
End Sub
Function LoopEachFolder(fldFolder As Object)
' With the subfolders in this folder....
For Each objFldLoop In fldFolder.subFolders
' ...loop through files in this folder
For Each objSubFile In objFldLoop.Files
If InStr(objSubFile.Name, ".pdf") > 0 Then
Debug.Print objSubFile.Path
End If
Next objSubFile
' ... and run the function on each subfolder found
LoopEachFolder objFldLoop
Next objFldLoop
End Function
If you run GetStructure, it will print (in VB Project Explorer's Immediate window) the path/name for all pdfs in your folder and all levels of subfolder.
You will need to change both lines:
If InStr(objSubFile.Name, ".pdf") > 0 Then
with:
If InStr(objSubFile.Name, ".pdf") > 0 And InStr(objSubFile.Name, "_") > 0 Then
(since you want files like "*_*.pdf") and each Debug.Print line with your Move code etc..
Revision 1 08 June 2023:
File added. Be sure you check (/correct) the line:
Set ObjFolder = objFso.GetFolder("C:\DATA\")
to ensure it has a valid folder which contains files with the extension .pdf.
Revision 2 08 June 2023:
To get a case-insensitive (for *.pdf" and *.PDF files etc,), the code need to convert filenames to lowercase- see changes in bold (implemented in the second attached file, Case-insensitive search through folder and all subfolders v0_b ):
Public ObjFolder As Object, objSubFile As Object
Public objFso As Object
Public objFldLoop As Object
Sub GetStructure()
' call the function to loop through folder and its subfolders
Set objFso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = objFso.GetFolder("C:\DATA\")
' loop through this folder
For Each objSubFile In ObjFolder.Files
If InStr(LCase(objSubFile.Name), ".pdf") > 0 Then
Debug.Print objSubFile.Path
End If
Next objSubFile
' call the function to loop through its subfolders (and theirs)
LoopEachFolder ObjFolder
' quit FSO
Set objFso = Nothing
End Sub
Function LoopEachFolder(fldFolder As Object)
' With the subfolders in this folder....
For Each objFldLoop In fldFolder.subFolders
' ...loop through files in this folder
For Each objSubFile In objFldLoop.Files
If InStr(LCase(objSubFile.Name), ".pdf") > 0 Then
Debug.Print objSubFile.Path
End If
Next objSubFile
' ... and run the function on each subfolder found
LoopEachFolder objFldLoop
Next objFldLoop
End Function
Hope this helps. If so, please don't forget to mark this Answer as Selected as usual.