Monday, 8 June 2015

arcgis 10.0 - Use arcpy.mapping to list broken data layers?


I want to list all the broken links in all mxds in a server drive. But whenever I try I get an error come up. Can someone use a specific example, say the drive is D drive, how would I put it into ESRIs script here:


import arcpy, os
path = r"C:\Project"

for fileName in os.listdir(path):
fullPath = os.path.join(path, fileName)
if os.path.isfile(fullPath):
basename, extension = os.path.splitext(fullPath)
if extension == ".mxd":
mxd = arcpy.mapping.MapDocument(fullPath)
print "MXD: " + fileName
brknList = arcpy.mapping.ListBrokenDataSources(mxd)
for brknItem in brknList:
print "\t" + brknItem.name

del mxd

Also I'm not used to not having pythonwin, does anyone know where to d/l it? I googled it but it kept telling me to install it from the ArcMap cd (I'm just using a work computer).



Answer



Below updated version of the ESRI sample should work. It will recursively search all directories below D: for .mxd files. print their names and if any datalayers are broken print the names of the broken datalayers.


import arcpy, os
path = r"D:"
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(path, fileName)
mxd = arcpy.mapping.MapDocument(fullPath)
print "MXD: " + fileName
brknList = arcpy.mapping.ListBrokenDataSources(mxd)
for brknItem in brknList:
print "\t" + brknItem.name

To write to a file instead of printing to the console:


import arcpy, os

path = r"D:"
f = open('somefile.txt', 'r')
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(path, fileName)
mxd = arcpy.mapping.MapDocument(fullPath)
f.write("MXD: " + fileName + "\n")
brknList = arcpy.mapping.ListBrokenDataSources(mxd)

for brknItem in brknList:
f.write("\t" + brknItem.name + "\n")
f.close()

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...