I have more than 300 layer files located in many sub-Folders and sub-sub-Folders. All Sub Folders are located in one large directory.I read Use python to add layers to TOC and I try, with arcpy, to detect all the layers and add them to mxd. Is it possible?
closed as off-topic by PolyGeo♦ Jun 3 '18 at 10:27
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If you work with ArcGIS 10.1 SP1 or above, you can use the arcpy.da.Walk() function:
import arcpy, os workspace = r"C:\directory" mxd = arcpy.mapping.MapDocument(r"C:\Map.mxd") df = arcpy.mapping.ListDataFrames(mxd, "*") walk = arcpy.da.Walk(workspace, datatype="Layer") for dirpath, dirnames, filenames in walk: for filename in filenames: arcpy.mapping.AddLayer(df, arcpy.mapping.Layer(os.path.join(dirpath, filename))) mxd.save()
This is possible with python. if this is part of your workflow in python you should do some python coding.
If your ultimate goal is to add the layers to the map (mxd) you can achieve this goal by:
1- go to the root folder of your layer files in the explorer
2- search for this term: *.lyr
3- use cntrl+A to select all lyr files.
4- drag them to the map of the ArcMap
As I mentioned, if this workflow is part of an automation in python you should tell and I will give some python code snippet.