# 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).

-
Pythonwin can be found here sourceforge.net/projects/pywin32 –  Samuel Jan 20 '12 at 9:24

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()

-
I think the problem is I have python 2.6 installed on my computer. I've installed 2.7 with python win however I arcmap has 2.6. Any ideas how to change it? I tried copying the arcgis folder into python 2.7 but it doesn't work still. Also the above code works in the python window of arcmap but while it lists the broken links of mxds it crashes my computer. Is there a way to get it to print to a notepad document? –  sni Jan 23 '12 at 3:33
I think you have to use 2.6 for ArcGIS. Try to install the version of pythonwin for Python 2.6 (maybe first uninstall version 2.7) –  Samuel Jan 23 '12 at 7:39
If it's telling you "no module named arcpy" or something similar, make sure you run arcgis's copy of python from (assuming default locations) C:\Python26\ArcGIS10.0\python.exe with an argument of the script you want to run (so C:\Python26\ArcGIS10.0\python.exe "path\to\your\script"). That should work. You can run Python 2.7 next to ArcGIS' copy of 2.6 just fine, but you must properly start the correct interpreter to load an arcpy script. –  nicksan Jan 23 '12 at 19:15

This one is a slight variation, worked for me for Layer and MXD's

import arcpy, os
path = r"N:\Deliverables Mirror\WEBSITE_DATASET"
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

import arcpy, os
path = r"N:\Deliverables Mirror\WEBSITE_DATASET"
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

-