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
    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. – smiller Nov 5 '18 at 16:39
0

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
  • 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 – FRMMO Nov 6 '18 at 10:17

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.