I am having some trouble creating a model that will iterate through folders and subfolders. I want to be able to have the top level folder as a parameter and then have the code go through each of the sub folders and complete an action.

I have working code for the action I'm looking to complete and I am able to create an iteration to go through one folder, but I want to be able to go through 18 folders and run my script on every .mxd within each folder.

Here is the model that runs within one folder. enter image description here

I want this to run through the 18 folders that I have.

Any ideas? I can run this either as a model or a script.

  • 2
    If you're comfortable with Python, you can use arcpy.da.Walk (10.1+) or os.walk.
    – phloem
    Jan 27, 2015 at 18:58
  • probably better off doing this in a script using @phloem suggestion. Jan 27, 2015 at 19:02

1 Answer 1


Here's how to find all the mxds in a directory tree using the os module:

import os

for root, dirs, files in os.walk('C:/junk'):
    for file in files:
        if os.path.splitext(file)[1] == '.mxd':
            print 'It is an MXD!'
  • In using this then, It works the same as any normal script when it find the .mxd? So if i have 2 mxds with the same table, and want to perform the same task on both, I would simply use the functions within the loop?
    – Martin
    Jan 27, 2015 at 21:19
  • I don't understand your question. Can you rephrase?
    – phloem
    Jan 27, 2015 at 21:23
  • In this example, you did a simple print, but could arcpy.SearchCursor(for example) be used to find amount of X attributes within a certain field possible be used in there as well?
    – Martin
    Jan 27, 2015 at 22:47
  • 1
    For each file found in the directory, a new path is placed in the 'file' variable. If it happens to be an mxd, it passes into the deepest loop (with the print statement). From there, access the mxd by referring to the path found in the 'file' variable. So, yes, you can do whatever you would normally do to an mxd file, by referencing the 'file' variable. I think you may be confusing walking through a file directory with using a cursor to loop through data.
    – phloem
    Jan 27, 2015 at 22:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.