Macro Traversing Subdirectories

I have a macro that adds custom properties. I want to add these
properties to every file in a directory including subdirectories. I
have it working on the main directory, but I'm having trouble with the
subdirectories.
Does anyone have a SW Macro example of traversing subdirectories to
perform a function that they would share?
The version of VB that appears when editing a Macro seems limited. It
doesn't seem to recognize commands like dir$ or FileSharedObjects. Are
there better commands/ways to do this?
Thanks, Blair
Reply to
Blair Sutton
Loading thread data ...
The VB version for macros is VBA. VB6 is also very similar.
VBA does have this capability. I have used it. You need to add the appropriate references to your macro.
In the VB macro editor, under "Tools --> References", check to add "Microsoft Scripting Runtime".
Reply to
That70sTick
Blair,
Have a look at this article for more information on working with subdirectories.
formatting link
Jeff
Reply to
Jeff
This bit of code should do what you need. It is a recursive function that will spit out all the file and folder names in a particular directory that you specify. You have to be sure to add Microsoft Scripting Runtime as a reference.
Sub MyProject
FolderTree ("C:\myFolder")
End Sub
Function FolderTree(FolderPath As String)
Dim fso As Object Dim OriginalFolder As Folder Dim SubFolders As Folders Dim OrginalFile As File Dim files As files Dim Folder As Folder Dim File As File
Set fso = New FileSystemObject Set OriginalFolder = fso.GetFolder(FolderPath)
Set files = OriginalFolder.files If files Is Nothing Then Exit Function
For Each File In files Debug.Print File.Name 'Do whatever you want to each file at this step in macro. Next File
Set SubFolders = OriginalFolder.SubFolders If SubFolders Is Nothing Then Exit Function
'Iterate all subfolders For Each Folder In SubFolders Debug.Print Folder.Name 'Do whatever you want to the folder at this point in macro. FolderTree Folder.Path Next Folder
End Function
Reply to
Mr. Who
Thanks for the great answers. Each of you gave great info that made this doable.
That70'sTick-Thanks for explaining the macro portion of SW and how to add tools. I missed how and when to add references.
Jeff- That MS site did a nice job of explaing the function
Mr. Who-I cut and pasted the code and was able to add my function calls where promted. Do you have a favorite site with code snippets like this?
Thanks, Blair
Reply to
Blair Sutton
I don't, but I have thought about putting something together to help the community. Especially an API primer for getting new people started. Once you understand a few simple things about the API even a novice can get things done. One of these days I will budget some time into producing something.
Reply to
Mr. Who

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.