0

I would like to identify within a particular folder the .lyr files that have broken links. Some of these files are within multiple folders but all fall within one folder. My programming knowledge is fairly novice so I may use confusing or incorrect terminology.

I have been following the Q&A below, and have got the script to successfully identify .mxd's in a test folder only containing one .mxd file containing a broken .lyr file. When trying the .lyr script given in the same thread on a test folder containing a broken .lyr file both IDLE and Arc Desktop (10.2) crash.

Use arcpy.mapping to list broken data layers?

successful broken .lyr in mxd script:

import arcpy, os
path = r"M_XXX"
for root, dirs, files in os.walk(path):
    for fileName in files:
        basename, extension = os.path.splitext(fileName)
        if extension == ".mxd":
            fullPath = os.path.join(root, fileName)
            mxd = arcpy.mapping.MapDocument(fullPath)
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                print "MXD: " + fullPath
                print "\t" + brknItem.name

crash .lyr script:

import arcpy, os
path = r"M_XXX"
for root, dirs, files in os.walk(path):
    for fileName in files:
        basename, extension = os.path.splitext(fileName)
        if extension == ".lyr":
            fullPath = os.path.join(root, fileName)
            mxd = arcpy.mapping.MapDocument(fullPath)
            brknList = arcpy.mapping.ListBrokenDataSources(mxd)
            for brknItem in brknList:
                print "LYR: " + fullPath
                print "\t" + brknItem.name
1
  • 1
    Welcome to GIS StackExchange! When you have a chance, please take the tour. Please also edit your post to include any error details. Another way you can troubleshoot strange errors is to add print statements to determine the last line that successfully printed. Commented Nov 5, 2018 at 16:39

1 Answer 1

1

Use a describe object to identify a layer's feature class. Then use Exists to determine if the feature class exists. If it doesn't the link will be broken.

import arcpy, os
path = r"M_XXX"
for root, dirs, files in os.walk(path):
    for fileName in files:
        basename, extension = os.path.splitext(fileName)
        if extension == ".lyr":
            fil = os.path.join (root, fileName)
            desc = arcpy.Describe (fil)
            fc = desc.featureClass.catalogPath
            if not arcpy.Exists (fc):
                print "broken:"
                print fil
1
  • returns the following: Traceback (most recent call last): File "M:", line 9, in <module> fc = desc.featureClass.catalogPath AttributeError: DescribeData: Method featureClass does not exist Commented Nov 6, 2018 at 10:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.