# xModifyDataLocationsInlayerFiles.py import sys, os import arcpy outFolder = r"C:\Projects\CoreData\GeoName" newFolder = r"C:\projects\testmove\test2\GeoName" inLayerFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.lyr" for lyr in arcpy.mapping.Layer(inLayerFile): pout = lyr.isFeatureLayer if pout == True: if lyr.supports("DATASOURCE"): currWSPath = str(lyr.workspacePath) scol = str.upper(currWSPath).find('GEONAME') scol = scol + len('GEONAME') + 1 newPath = newFolder + os.sep + currWSPath[scol:] print lyr.name + " | Path: " + newPath + " | DSN: " + lyr.datasetName lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True) else: print "Layer " + lyr.name + " does not support datasource changes" else: pass
Solved! Go to Solution.
import arcpy outFolder = r"C:\Projects\CoreData\GeoName" newFolder = r"\\silver\projects\SpatialData_Core\GeoName" inLayerFile = r"C:\Projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.lyr" outLayerFile = r"\\silver\projects\SpatialData_Core\GeoName\LayerFiles\AG10\BaseInfo_updated.lyr" inMXDFile = r"C:\Projects\CoreData\GeoName\LayerFiles\AG10\BaseInfo.mxd" outMXDFile = r"\\silver\projects\SpatialData_Core\GeoName\LayerFiles\AG10\BaseInfo_updated.mxd" if arcpy.Exists(outMXDFile): arcpy.Delete_management(outMXDFile) if arcpy.Exists(outLayerFile): arcpy.Delete_management(outLayerFile) mxd = arcpy.mapping.MapDocument(inMXDFile) mxd.findAndReplaceWorkspacePaths(outFolder,newFolder, False) mxd.saveACopy(outMXDFile) del mxd mxd = arcpy.mapping.MapDocument(outMXDFile) df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "", df)[0] lyr.saveACopy(outLayerFile) if arcpy.Exists(outMXDFile): arcpy.Delete_management(outMXDFile) print "Finished creating new Layer file: " + outLayerFile
How about changing True to False in the following line?
lyr.replaceDataSource(unicode(newPath), 'FILEGDB_WORKSPACE', lyr.datasetName, True)
import arcpy outFolder = r"C:\Projects\CoreData\GeoName" newFolder = r"C:\projects\testmove\test2\GeoName" inMXDFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo.mxd" outMXDFile = r"C:\projects\testmove\test2\GeoName\LayerFiles\AG10\BaseInfo_updated.mxd" mxd = arcpy.mapping.MapDocument(inMXDFile) mxd.findAndReplaceWorkspacePaths(outFolder,newFolder, False) mxd.saveACopy(outMXDFile)
You will need to replacedatasource for the individual layer files in the group layer and then do a save of the group layer that holds the individual layers.
Try either one of these lines of code:
lyr.save() - I think you would put in inLayerFile in () but I'm not sure that will work as the ESRI documentation just shows it blank
lyr.saveACopy(inLayerFile)
The documentation for this is found at:
http://resources.arcgis.com/en/help/main/10.1/index.html#/Layer/00s300000008000000/
import arcpy fullLyr = arcpy.mapping.Layer(r"C:\Temp\FullPath.lyr") lyrs = arcpy.mapping.ListLayers(lyr1) for lyr in lyrs: if lyr.supports("DATASOURCE"): lyr.findAndReplaceWorkspacePath(r"C:\Temp", r"C:\Active\ArcPY\ScrumWorks\Data") fullLyr.saveACopy(r"C:\Temp\FullPath2.lyr") relLyr = arcpy.mapping.Layer(r"C:\Temp\RelPath.lyr") lyrs = arcpy.mapping.ListLayers(lyr2) for lyr in lyrs: if lyr.supports("DATASOURCE"): lyr.findAndReplaceWorkspacePath(r"C:\Temp", r"C:\Active\ArcPY\ScrumWorks\Data") relLyr.saveACopy(r"C:\Temp\RelPath2.lyr")