Macro Traversing Subdirectories

Discussion in 'SolidWorks' started by Blair Sutton, Mar 22, 2006.

  1. Blair Sutton

    Blair Sutton Guest

    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
     
    Blair Sutton, Mar 22, 2006
    #1
  2. Blair Sutton

    That70sTick Guest

    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".
     
    That70sTick, Mar 22, 2006
    #2
  3. Blair Sutton

    Jeff Guest

    Jeff, Mar 22, 2006
    #3
  4. Blair Sutton

    Mr. Who Guest

    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
     
    Mr. Who, Mar 22, 2006
    #4
  5. Blair Sutton

    Blair Sutton Guest

    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
     
    Blair Sutton, Mar 23, 2006
    #5
  6. Blair Sutton

    Mr. Who Guest

    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.
     
    Mr. Who, Mar 27, 2006
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.