How do I loop a folder of mxds

14739
43
05-10-2016 08:23 AM
DevinUnderwood2
Occasional Contributor

I want to loop many mxds within a folder to retrieve layer,connection,etc. information using python.

I just need the syntax for looping through the mxds.

0 Kudos
43 Replies
David_van_Riel
New Contributor III

Hi Devin,

I have created a Python script to give info on all mxds in a specified folder.  The output is a separate text file for each mxd.  I based it off of this page:https://community.esri.com/thread/19655.  I found that not all available document/layer/table... info was being reported on so I used this page http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000008000000 to get as much info as I could (although I am having trouble getting data driven page info).  There is still info I would like to get out (like feature symbology), but this is the best I could do/find.

0 Kudos
DevinUnderwood2
Occasional Contributor

Thank you, I will take a look and parse only what I need. Which is the most difficult part, there are examples and suggestions that I wish only had just what I needed.

0 Kudos
DevinUnderwood2
Occasional Contributor

The following code prints out the data I want for a specified mxd. I want to iterate every mxd in a specified folderpath and return the layers. Any idea how to do this ?

import arcpy,os

folderPath = (xxxxxx)

mxd = arcpy.mapping.MapDocument (xxxx)

layers = arcpy.mapping.ListLayers (mxd)

for layer in layers: 

  if layer.supports("dataSource"): 

        print layer.dataSource

del mxd

0 Kudos
David_van_Riel
New Contributor III

Hi Devin,

I don't know if this is the best way to do this (probably not), and it doesn't do subfolders (Gerard's code seems to do this) but this is what I do (it works 😞

##Set the mxd folder path.

MxdFolderPath = r"C:\GIS"

    ##Loop through each file in the folder.

    for FileName in os.listdir(MxdFolderPath):

        FileFullPath = os.path.join(MxdFolderPath, FileName)

        ##If the file exists then...

        if os.path.isfile(FileFullPath):

            ##Initialize the file extensions to look for list.

            FileExtensionsToReportList = [".mxd",".Mxd",".MXD"]

            ##For each file extension to report on...

            for FileExtensionToReport in FileExtensionsToReportList:

                ##If the file ends with the file extension to report on then...

                if FileName.endswith(FileExtensionToReport):

0 Kudos
David_van_Riel
New Contributor III

Sorry, was in a rush to leave at the end of the day and didn't properly read your post.

First you need to get a listing of all the data frames and go through each of them:

DataFrameList = arcpy.mapping.ListDataFrames(MxdFile)

for DataFrame in DataFrameList:

There is a bunch of info you can get on the data frame.  Then in each data frame loop get a listing of the layers in each data frame and go through each of them:

LayerList = arcpy.mapping.ListLayers(MxdFile, "", DataFrame)

for Layer in LayerList:

You can also get a listing of tables:

TableList = arcpy.mapping.ListTableViews(MxdFile, "", DataFrame)

for Table in TableList:

I have attached the script I created.

0 Kudos
GerardHavik
New Contributor III

Just finished a project to extract as much relevant layer information as possible from MXD's in a folder with folders.

The choice to go only one niveau is intentional. This makes backup folders 'invisible'.

Hope this helps, suggestions for  improvements are welcome.

0 Kudos
DevinUnderwood2
Occasional Contributor

go only one niveau  ?

0 Kudos
GerardHavik
New Contributor III

David,

Try the following:

import arcpy,os

def printlayers(mxdfile):
  mxd = arcpy.mapping.MapDocument(mxdfile)
  layers = arcpy.mapping.ListLayers(mxd)
  for layer in layers: 
    if layer.supports("dataSource"):
      print layer.dataSource
  del mxd

folderPath = (xxxxxx)
for root,dirs,files in os.walk(folderpath):
  for file in files:
    if file.endswith('.mxd'):
      printlayers(os.path.join(root,file))

For using os.walk study: OS.walk in Python

part of the fun in coding is figuring out how to use something for your own project.

Gerard Havik

0 Kudos
David_van_Riel
New Contributor III

Hi Gerard,

I am not having trouble going through each mxd in a folder.  I am having trouble reporting the colour, linestyle type, thickness, point symbol, etc... that a symbol is using.  There doesn't seem to be any way of doing that.  I want to report all the information so that someone could, if needed, recreate the mxd from scratch and get the same result - or if I want to know how the mxd was at certain times and report on it and compare with other times in a table format, without having to back up the mxd and opening it up to see all the settings, symbology, etc... .

0 Kudos
GerardHavik
New Contributor III

Hi David,

That is an entire different question. I recommend searching for anwers to that question, if found none or not satisfactory ask it as a new question. Let's not contaminate this discussion.

Greetings, Gerard.

0 Kudos