Im looking for a way to get the name and path of a layer file which is used in an MXD. I know once a layer file is added to a MXD, the path info is not stored, which is problematic.
Essentially the end goal is to use arcpy.mapping
module to update a bunch of layers within any MXD. using the following code
arcpy.mapping.UpdateLayer(maindataframe, updateLayer, sourceLayer, True)
We have a folder structure where all our layer files are connected to a mix of SDE and GDB's. And to find a way to make a table of all the layers with the paths would be a godsend.
The question is, is there a way to programmaticly run though each folder and sub folders (maybe using os.walk?) and get the layer name used in ArcMap (in this case, the updatelayer), and the path (sourcelayer) to get this format -> [River System,r'C:\Catchments\Area\River_Catchments.lyr']
Edit: I should point out that this is not to update the datasource, but the path of where it is found. The reason I need this is that there are old MXD's that contain old/dated symbology and need to be updated. By updating the layer with the one in the folder structure I can update multiple MXD's at once.
1 Answer 1
Assuming that you "have a folder structure where all [your] layer files are connected to a mix of SDE and GDB's", and that you are talking about *.lyr
files and not talking about layers stored in map documents (*.mxd
files), the code you need to write just needs to:
- Use arcpy.da.Walk to walk through the folder structure looking for
*.lyr
files. - For each layer file found create a layer object using arcpy.mapping.Layer().
- View the layer objects name using the
name
property and its data source using thedataSource
property. - When you write a list of the above as tuples you could also store the name of the layer file as a third item in each tuple.
-
Testing this now, seems promisingSlevy– Slevy2017年02月07日 08:02:35 +00:00Commented Feb 7, 2017 at 8:02
arcpy.mapping.ListLayers()