Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search sub folders 1

Status
Not open for further replies.

jackeroo75

Technical User
Aug 26, 2007
34
US
This code works but I want to also check all sub folders in C and get the owmer information from the file.

Sub Test()
Dim Sh As Object
Dim ShFolder As Object
Dim ShFile As Object

Set Sh = CreateObject("Shell.Application")
Set ShFolder = Sh.Namespace("C:\") 'CHANGE FOLDER PATH
For Each ShFile In ShFolder.Items
Debug.Print ShFile.Path, ShFile.Name, ShFolder.GetDetailsOf(ShFile, 10) ‘this gets owner
Next
End Sub
 
FileSystemObject to the rescue.
Here is an example with FileSystemObject to recursively traverse all subdirectories:

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Shame to be directing people away from tek-tips when we have serviceable examples of recursive folder traversal right here, eg thread707-1787843
 
FInal code!

Option Explicit
Sub Test()
Dim Sh As Object
Dim ShFolder As Object
Dim ShFile As Object
Dim strPath As Variant
Dim Folder As Scripting.Folder

strPath = "C:\Users\JackMcDonald\Downloads"
Set Sh = CreateObject("Shell.Application")
Set ShFolder = Sh.Namespace(strPath) 'CHANGE FOLDER PATH


Set FSO = New Scripting.FileSystemObject

Set Folder = FSO.GetFolder(strPath)

FolderRoutine Folder
End Sub


Sub FolderRoutine(Folder As Scripting.Folder)

Dim File As Scripting.File
Dim SubFolder As Scripting.Folder
Dim ShSub As Object
Dim ShSubFolder As Object
Dim ShSubFile As Object
Dim strPath As Variant

For Each File In Folder.Files
' Do your File stuff here
Next
strPath = Folder
Set ShSub = CreateObject("Shell.Application")
Set ShSubFolder = ShSub.Namespace(strPath) 'CHANGE FOLDER PATH


For Each ShSubFile In ShSubFolder.Items
Debug.Print ShSubFile.Path, ShSubFile.Name, ShSubFile.Type, ShSubFolder.GetDetailsOf(ShSubFile, 10)
Next ShSubFile

For Each SubFolder In Folder.SubFolders
FolderRoutine SubFolder ' recursively process the subfolder
Next

End Sub

UPDATE: this code works but if I run into at least 2k of files then excel hour glasses stays on and the program crashes. Anyone knows why?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top