import arcpy, os folderPath = arcpy.GetParameterAsText(0) #path to mxds Name = arcpy.GetParameterAsText(1)#Name of bad layer in mxds DataS= arcpy.GetParameterAsText(2)#path to new workspace NewS= arcpy.GetParameterAsText(3)#new dataset for filename in os.listdir(folderPath): fullpath = os.path.join(folderPath, filename) if fullpath[-4:] == ".mxd": mxd = arcpy.mapping.MapDocument(fullpath) for df in arcpy.mapping.ListDataFrames(mxd): lyrs = arcpy.mapping.ListLayers(df) for lyr in lyrs: if lyr.name == Name: lyr.replaceDataSource(DataS, "SHAPEFILE_WORKSPACE",NewS,True) mxd.save() del mxd
Solved! Go to Solution.
if fullpath[:-4] == ".mxd":
In third line of the loop try:if fullpath[:-4] == ".mxd":
import arcpy, os folderPath = r"C:\student\bgd" for filename in os.listdir(folderPath): fullpath = os.path.join(folderPath, filename) if fullpath[-4:] == ".mxd": mxd = arcpy.mapping.MapDocument(fullpath) print str(mxd) for df in arcpy.mapping.ListDataFrames(mxd): print "The dataframe is ", df.name lyrs = arcpy.mapping.ListLayers(df) for lyr in lyrs: print "Layer name: ", str(lyr) if lyr.name =="parks": lyr.replaceDataSource(r"C:\student\bgd\test_test", "SHAPEFILE_WORKSPACE","parks") mxd.save() del mxd