for layerFile in arcpy.ListFiles("*.lyr"): # find the actual layer files in the folder arcpy.AddMessage("Found layer file: " + str(layerFile)) lyr = arcpy.mapping.Layer(folder + "\\" + layerFile) # make layer object from layer file for lyrObj in arcpy.mapping.ListLayers(lyr):# a layer file may have multiple layers within it so work through those... if lyrObj.isGroupLayer == 1: # if it is a group layer arcpy.AddMessage("This layer file contains grouped items") for item in lyrObj: arcpy.AddMessage("It contains a layer object named: " + item.name) newWS = item.workspacePath.replace(".mdb", ".gdb") newSource = item.dataSource.replace(".mdb", ".gdb") arcpy.AddMessage("dataSource: " + newWS) if arcpy.Exists(newSource): arcpy.AddMessage("I've found a replacement GDB for this so I'll update this layer.") item.replaceDataSource (newWS, "FILEGDB_WORKSPACE") else: arcpy.AddMessage(newSource + " DOES NOT exist. Will not update this layer.") lyrObj.save()
Solved! Go to Solution.
import arcpy import arcpy.mapping import os, sys, string, logging from arcpy import env # Folder- the folder where you want to search for the layer files folder = arcpy.GetParameterAsText(0) arcpy.env.workspace = folder arcpy.AddMessage("Searching the input folder for layer files: " + folder) for layerFile in arcpy.ListFiles("*.lyr"): # find the actual layer files in the folder lyr = arcpy.mapping.Layer(folder + "\\" + layerFile) # make layer object from layer file for lyrObj in arcpy.mapping.ListLayers(lyr):# a layer file may have multiple layers within it so work through those... if lyr.isGroupLayer: #arcpy.AddMessage(layerFile + " is a group layer.") if lyrObj.longName != str(lyr.name): # just update the layers within the group layer arcpy.AddMessage(layerFile + " contains a layer object named: " + lyrObj.name) newWS = lyrObj.workspacePath.replace(".mdb", ".gdb") newSource = lyrObj.dataSource.replace(".mdb", ".gdb") if arcpy.Exists(newSource): arcpy.AddMessage("Updating " + lyrObj.name + " with replacement GDB.") lyrObj.replaceDataSource (newWS, "FILEGDB_WORKSPACE") lyr.save() else: arcpy.AddMessage(newSource + " DOES NOT exist. Will not update this layer.") elif lyr.isGroupLayer == 0: newWS = lyr.workspacePath.replace(".mdb", ".gdb") newSource = lyr.dataSource.replace(".mdb", ".gdb") if arcpy.Exists(newSource): arcpy.AddMessage("Updating " + lyr.name + " with replacement GDB.") lyr.replaceDataSource (newWS, "FILEGDB_WORKSPACE") lyr.save() else: arcpy.AddMessage(newSource + " DOES NOT exist. Will not update this layer.") del arcpy, os, sys, lyr
Shouldn't you be using the SaveToLayerFile_management command?
The following is the 10.0 webhelp ref:
Save To Layer File (Data Management)
Resource Center » Professional Library » Geoprocessing » Geoprocessing tool reference » Data Management toolbox » Layers and Table Views toolset
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000070000000
Also, I think you'll need to save at the uppermost 'level' (the outer loop where you first accessed the group), although I did not test this.
Enjoy,
Wayne
for layerFile in arcpy.ListFiles("*.lyr"): # find the actual layer files in the folder arcpy.AddMessage("Found layer file: " + str(layerFile)) lyr = arcpy.mapping.Layer(folder + "\\" + layerFile) # make layer object from layer file for lyrObj in arcpy.mapping.ListLayers(lyr): if lyrObj.isGroupLayer == 1: # if it is a group layer arcpy.AddMessage("This layer file contains grouped items") for item in lyrObj: arcpy.AddMessage("It contains a layer object named: " + item.name) newWS = item.workspacePath.replace(".mdb", ".gdb") newSource = item.dataSource.replace(".mdb", ".gdb") arcpy.AddMessage("dataSource: " + newWS) if arcpy.Exists(newSource): arcpy.AddMessage("I've found a replacement GDB for this so I'll update this layer.") item.replaceDataSource (newWS, "FILEGDB_WORKSPACE") else: arcpy.AddMessage(newSource + " DOES NOT exist. Will not update this layer.") layerFile.save()
import arcpy import arcpy.mapping import os, sys, string, logging from arcpy import env # Folder- the folder where you want to search for the layer files folder = arcpy.GetParameterAsText(0) arcpy.env.workspace = folder arcpy.AddMessage("Searching the input folder for layer files: " + folder) for layerFile in arcpy.ListFiles("*.lyr"): # find the actual layer files in the folder lyr = arcpy.mapping.Layer(folder + "\\" + layerFile) # make layer object from layer file for lyrObj in arcpy.mapping.ListLayers(lyr):# a layer file may have multiple layers within it so work through those... if lyr.isGroupLayer: #arcpy.AddMessage(layerFile + " is a group layer.") if lyrObj.longName != str(lyr.name): # just update the layers within the group layer arcpy.AddMessage(layerFile + " contains a layer object named: " + lyrObj.name) newWS = lyrObj.workspacePath.replace(".mdb", ".gdb") newSource = lyrObj.dataSource.replace(".mdb", ".gdb") if arcpy.Exists(newSource): arcpy.AddMessage("Updating " + lyrObj.name + " with replacement GDB.") lyrObj.replaceDataSource (newWS, "FILEGDB_WORKSPACE") lyr.save() else: arcpy.AddMessage(newSource + " DOES NOT exist. Will not update this layer.") elif lyr.isGroupLayer == 0: newWS = lyr.workspacePath.replace(".mdb", ".gdb") newSource = lyr.dataSource.replace(".mdb", ".gdb") if arcpy.Exists(newSource): arcpy.AddMessage("Updating " + lyr.name + " with replacement GDB.") lyr.replaceDataSource (newWS, "FILEGDB_WORKSPACE") lyr.save() else: arcpy.AddMessage(newSource + " DOES NOT exist. Will not update this layer.") del arcpy, os, sys, lyr