Arc Py - Find Broken Data Sources For All Folders

2409
10
09-02-2010 11:19 AM
AngeloLelekis
New Contributor
I just downloaded the arcpy.mapping sample script tools and was testing the Find Broken Data Sources Tool.  It works great, however I was wondering if anyone knows how to search subfolders, or all folders on a drive for broken data sources? 

We have many subfolders on our server and it would be a lot more efficient if I can run one report for the whole server instead of going several folders deep to the project level and then having to run countless reports.  Any help would be greatly appreciated!
0 Kudos
10 Replies
ChrisFox3
Occasional Contributor III
Try this:

import arcpy,os

for dirname, dirnames, filenames in os.walk(r"C:\Temp"): 
    for filename in filenames: 
        path = os.path.join(dirname, filename)
        basename, extension = os.path.splitext(path)
        if extension == ".mxd":
            mxd = arcpy.mapping.MapDocument(path)
            print "MXD: " + path
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                print "\t" + brknItem.name
            del mxd

-Chris
0 Kudos
AngeloLelekis
New Contributor
Thanks Chris!  Sorry for the delay in responding.  This is exactly what I needed.
0 Kudos
SebastianPawlak
New Contributor II
I would like to use this code, but it only works through the first iteration. Can someone find my error?

My error handlers catch errors only in this code block:

                    try:
                        brknItem.replaceDataSource(workspace, "SDE_WORKSPACE", item)
                        print "\t item successfully replaced"
                    except:
                        print "\t error occured replacing layer" 

                       

Code is pasted below:

# Import arcpy module
import arcpy, os

dir = "path to mxd files"
workspace = "path to new datasource"


for file in os.listdir(dir):

    if file.startswith("Sector"):
        file = os.path.join(dir, file)
        print "\n\n" + file
        mxd = arcpy.mapping.MapDocument(file)
        brknList = []
        brknList = arcpy.mapping.ListBrokenDataSources(mxd)

        if len(brknList) > 0:
            print "\nlist of broken data sources: "
            for lyr in brknList:
                print "\t" + lyr.datasetName

            print "\nreplacing layers"
            for brknItem in brknList:
                if brknItem.datasetName != "EPOISicCodeTABLE":
                    arcpy.AddMessage("\t" + brknItem.datasetName)
                    print "\t" + brknItem.datasetName

                    item = brknItem.datasetName

                    try:
                        brknItem.replaceDataSource(workspace, "SDE_WORKSPACE", item)
                        print "\t item successfully replaced"
                    except:
                        print "\t error occured replacing layer"
                        print arcpy.GetMessages(2)

        try:
            mxd.save()
        except:
            print "\t an error occured w/ saving %s" % file

        print "done"

        del mxd
0 Kudos
ChrisFox3
Occasional Contributor III
What is the exact text in the exception that is raised?
0 Kudos
SebastianPawlak
New Contributor II
The error I am getting is:


brknItem.replaceDataSource(workspace, "SDE_WORKSPACE", item)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\_mapping.py", line 585, in replaceDataSource
    return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))
ValueError: Layer: Unexpected error
0 Kudos
ChrisFox3
Occasional Contributor III
Based on your code and the exception I don't think there is a coding error that is causing this problem. My recommendation would be to try to isolate the MXD and the layer that this exception is being raised for and try to simplify the code to just run once on that MXD and layer. If you can reproduce the error, look into the layer and see if there is anything that might cause your code to fail.

For example does the brknItem.datasetName exist in the new workspace you are trying to update the dataset to or is the dataset type appropriate for the layer you are trying to update.
0 Kudos
SebastianPawlak
New Contributor II
Hi Chris,

I've crafted this script to skip MXDs that have no broken links. Since the script only works through the first iteration I am forced to run the script multiple times until all my MXDs have their data sources fixed. Each time I run this script, the previously fixed MXD is skipped.

So, the issues seems to be with the "replaceDataSources" line only. It's as though this function is locked in the first iteration.
0 Kudos
ChrisFox3
Occasional Contributor III
Interesting, it may be a bug. I tested with some of my own data and your code and could not reproduce the issue, everything updated as expected. What workspace type are you going from for the layers you are updating? What version of SDE and DBMS are you going to?
0 Kudos
SebastianPawlak
New Contributor II
I'm going from a .gdb to an SDE.

- I'm running ArcInfo 10 SP3 on my client; and
- ArcSDE 9.3 (not sure of the service pack) on the server.
0 Kudos