Is there any routine I can use to read the data sources out of mxd´s without waiting 30 seconds to open each one?

I need to create lists of all data sources as we are migrating data and altering our file system.

I have around 1000 mxds which need to be checked.

  • 2
    you could use arcpy to list the data sources into a csv which you could later interrogate in excel. – TsvGis Jun 12 '15 at 6:15
  • 1
    To do a GIS inventory I suggest arcpy.da.Walk in conjunction with the above. – PolyGeo Jun 12 '15 at 6:37
  • arcpy can look right into the mxd layer composition? – Robert Buckley Jun 12 '15 at 6:42
  • 1
    The functions Layer() and ListLayers() should provide some guidance. This link has some examples of ListLayers() being used. – TsvGis Jun 12 '15 at 6:57

So this should solve your question. This fundtion will list all the data sources found in any MXD file within a folder and it's subfolders. It'll save the CSV in the folder you specify to begin the search from.

Change the name of the CSV on line 22 if you need to.

The CSV is written with Path, FileName, FileType

import os
import csv
import arcpy

def ListMXDSources(path,extension):
  list_dir = []
  CountList = []
  MapList = []
  list_dir = os.listdir(path)
  count = 0
  for paths, dirctory, files in os.walk(path):
    for file in files:
        if file.endswith(extension): # eg: '.mxd'
          MapList.append(os.path.join(paths, file))
          print MapList
          for m in MapList:
                count += 1
                mxd = arcpy.mapping.MapDocument(m)
                ## --------- For each map list layers
                for lyr in arcpy.mapping.ListLayers(mxd):
                    with open("ListOfDataSources.csv", 'wb') as csvfile:
                        csvwriter = csv.writer(csvfile)
                        for dirpath, dirnames, filenames in arcpy.da.Walk(MapList):
                            for filename in filenames:
                                desc = arcpy.Describe(os.path.join(dirpath, filename))
                                csvwriter.writerow([desc.catalogPath, desc.name, desc.dataType])

#-------------Use below to call the function----------------#
ListMXDSources(path=r"ROOT FOLDER LOCATION",extension=".mxd")
|improve this answer|||||
  • spk578, I have tried to use script file for set of shapefiles in a mxd i have and while it creates the csv file it comes up empty, do you have any ideas to why? – popboarder Dec 15 '15 at 19:15
  • @popboarder thanks for the comment I see this is happening! I think you should add a condition utilizing the output from lyr.dataSource – spk578 Dec 16 '15 at 10:46

In my search to find an answer to the same question, I came across this post first. However, after attempting to run the above supplied script, I was unable to create any output in the csv that was generated. Searching again, I came across this (duplicate question/post?) post on Stack that did run successfully, and in addition to listing data sources, looped through all dataframes (if more than one), and listed layer names:

Stack Exchange - list all datasources for mxd's in a Directory

Would like to add, if you are a GIS Manager/Analyst that is undergoing a data migration and subsequent edits need to be made to REST Services in ArcGIS Server that are coming to SDE datasources, IMHO the best way to check the data sources of these REST services is not through the ArcGIS Server REST API or REST Admin, but rather by running this script on mxd's that are placed on your ArcGIS Server at the time of publishing (e.g. [YOUR ARCGIS DIRECTORY DRIVE]\arcgisserver\directories\arcgissystem\arcgisinput )

|improve this answer|||||

for the code in spk578 Comment

Loop for .mxd while you are in one loop.

Used your code for something else.

Here is how it perfectly worked:

if file.endswith(extension): # eg: '.mxd
   mxd = arcpy.mapping.MapDocument(os.path.join(paths, file))
   ## --------- For each map list layers
   for lyr in arcpy.mapping.ListLayers(mxd):

Use these directly without maplist for each file,

|improve this answer|||||

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.