5

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

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 20, 2012 at 5:43
1

2 Answers 2

8

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()
answered Jan 20, 2012 at 9:38
4
  • 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? Commented Jan 23, 2012 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) Commented Jan 23, 2012 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. Commented Jan 23, 2012 at 19:15
  • If anyone is using this and there a multiple folders on D:, I would recommend using os.path.join(root, fileName) because using path meant the links didn't work Commented Nov 16, 2015 at 14:43
1

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
answered May 21, 2012 at 11:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.