Finding broken data sources in all map documents in a folder

A common scenario in many organizations involves the movement of data from one workspace to another or from one workspace type to another. When this happens, any map documents or layers that reference these data sources become broken. Finding each of these data sources can be a huge task if undertaken manually. Fortunately, you can create a geoprocessing script that will find all broken data sources in a folder or list of folders.

Getting ready

In this recipe, you will learn how to recursively search directories for map document files, find any broken data sources within these map documents, and write the names of the broken data layers to a file.

How to do it...

Follow these steps to learn how to find all broken data sources in all map documents in a folder:

  1. Open IDLE and create a new script window.
  2. Import the arcpy and os packages:
    import arcpy.mapping as mapping, os
  3. Open a file that you will use to write the broken layer names:
    f = open('BrokenDataList.txt', 'w')
  4. Pass a path to the c:ArcpyBook folder to use in the os.walk() method along with a for loop to walk the directory tree:
    for root,dirs,files in os.walk("C:ArcpyBook"):
  5. Inside the for loop, create a second for loop that loops through all the files returned and create a new filename variable. Remember to indent the for loop inside the first for loop:
    for name in files:
        filename = os.path.join(root, name)
  6. Following the last line of code that you added, test the file extension to see if it is a map document file. If so, create a new map document object instance using the path, write the map document name, get a list of broken data sources, loop through each of the broken data sources, and write to the file:
    if ".mxd" in filename:
         mxd = mapping.MapDocument(filename)
         f.write("MXD: " + filename + "
    ")
         brknList = mapping.ListBrokenDataSources(mxd)
         for brknItem in brknList:
              print "Broken data item: " + brknItem.name + " in " + filename
              f.write("	" + brknItem.name + "
    ")
  7. Add a print statement to indicate that you are done and close the file:
    print("All done")
    f.close()
  8. The entire script should appear as follows:
    How to do it...
  9. You can check your work by examining the c:ArcpyBookcodeCh3ListBrokenDataSources.py solution file.
  10. Run the script to generate the file.
  11. Open the file to see the results. Your output will vary depending upon the path you've defined. The following screenshot shows my output file:
    How to do it...

How it works...

This script uses a combination of methods from the Python os package and the arcpy.mapping package. The os.walk() method walks a directory tree and returns the path, a list of directories, and a list of files for each directory starting with a root directory that you have defined as the c:ArcpyBook directory. This root directory could have been any directory. The os.walk() method returns a three item tuple consisting of the root directory, a list of directories immediately contained within that root, as well as a list of files immediately contained within the root. We then loop through this list of files and test each one to see if it contains the .mxd string, which indicates a map document file. Files identified as map documents have their filenames written to a text file, and a new MapDocument object instance is created. The ListBrokenDataSources() method is then used with a reference to the map document to generate a list of broken data sources within the file, and these broken data sources are written to the file as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset