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?
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()