import arcpy,os for dirname, dirnames, filenames in os.walk(r"C:\Temp"): for filename in filenames: path = os.path.join(dirname, filename) basename, extension = os.path.splitext(path) if extension == ".mxd": mxd = arcpy.mapping.MapDocument(path) print "MXD: " + path brknList = arcpy.mapping.ListBrokenDataSources(mxd) for brknItem in brknList: print "\t" + brknItem.name del mxd
try: brknItem.replaceDataSource(workspace, "SDE_WORKSPACE", item) print "\t item successfully replaced" except: print "\t error occured replacing layer"
# Import arcpy module import arcpy, os dir = "path to mxd files" workspace = "path to new datasource" for file in os.listdir(dir): if file.startswith("Sector"): file = os.path.join(dir, file) print "\n\n" + file mxd = arcpy.mapping.MapDocument(file) brknList = [] brknList = arcpy.mapping.ListBrokenDataSources(mxd) if len(brknList) > 0: print "\nlist of broken data sources: " for lyr in brknList: print "\t" + lyr.datasetName print "\nreplacing layers" for brknItem in brknList: if brknItem.datasetName != "EPOISicCodeTABLE": arcpy.AddMessage("\t" + brknItem.datasetName) print "\t" + brknItem.datasetName item = brknItem.datasetName try: brknItem.replaceDataSource(workspace, "SDE_WORKSPACE", item) print "\t item successfully replaced" except: print "\t error occured replacing layer" print arcpy.GetMessages(2) try: mxd.save() except: print "\t an error occured w/ saving %s" % file print "done" del mxd
brknItem.replaceDataSource(workspace, "SDE_WORKSPACE", item) File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\_mapping.py", line 585, in replaceDataSource return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True))) ValueError: Layer: Unexpected error