Hye everyone,
I have create one Python file, the task is to add layer from the latest or current folder following date of folder. The sample folder is below;
This is my sample code on Python;
import arcpy
import os
# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument(r"C:\Users\User\Documents\MTSB_JPS\MXD\test.mxd")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
# base folder
workspace = r"C:\Users\User\Documents\MTSB_JPS\Flood_Forecasting_Outputs\TRN"
walk = arcpy.da.Walk(workspace, topdown=False, datatype="FeatureClass")
for dirpath, dirnames, filenames in walk:
for filename in filenames:
layerfile = os.path.join(dirpath, filename)
addlayer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")
mxd.save()
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del addlayer, mxd
The problem is, this code can't add the layer from the latest or current folder. It should add the layer from "20171117_0600_AF" but when I run this code, it add the layer from "20170701_2000_AF" folder.
Please help.