Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to search sub folders

How to search sub folders

How to search sub folders

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
End Sub

RE: How to search sub folders

FileSystemObject to the rescue.
Here is an example with FileSystemObject to recursively traverse all subdirectories:


"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.

RE: How to search sub folders

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
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

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?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close