<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Add layer from current folder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533171#M41768</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;On a quick look, to me it looks like the for loops are going to try to add all layers.&amp;nbsp; Maybe you can grab date part of the name and do a compare until you find the latest?&amp;nbsp; Or it if it will always be the last item on the list, get a count and try to grab that.&amp;nbsp; Just a few things to try.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 21 Nov 2017 15:26:32 GMT</pubDate>
    <dc:creator>RebeccaStrauch__GISP</dc:creator>
    <dc:date>2017-11-21T15:26:32Z</dc:date>
    <item>
      <title>Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533167#M41764</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;STRONG&gt;Hye everyone,&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Sample of folder" class="image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/384485_folder.png" style="height: auto;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;This is my sample code on Python;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;arcpy
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;import &lt;/SPAN&gt;os

&lt;SPAN style="color: #808080;"&gt;# suppose you want to add it to the current MXD (open MXD)
&lt;/SPAN&gt;mxd = arcpy.mapping.MapDocument(&lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"C:\Users\User\Documents\MTSB_JPS\MXD\test.mxd"&lt;/SPAN&gt;)
dataFrame = arcpy.mapping.ListDataFrames(mxd, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"*"&lt;/SPAN&gt;)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]

&lt;SPAN style="color: #808080;"&gt;# base folder
&lt;/SPAN&gt;workspace = &lt;SPAN style="color: #008000; font-weight: bold;"&gt;r"C:\Users\User\Documents\MTSB_JPS\Flood_Forecasting_Outputs\TRN"
&lt;/SPAN&gt;walk = arcpy.da.Walk(workspace, &lt;SPAN style="color: #660099;"&gt;topdown&lt;/SPAN&gt;=&lt;SPAN style="color: #000080;"&gt;False&lt;/SPAN&gt;, &lt;SPAN style="color: #660099;"&gt;datatype&lt;/SPAN&gt;=&lt;SPAN style="color: #008000; font-weight: bold;"&gt;"FeatureClass"&lt;/SPAN&gt;)

&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;dirpath, dirnames, filenames &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;filename &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;filenames:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layerfile = os.path.join(dirpath, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addlayer = arcpy.mapping.Layer(layerfile)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(dataFrame, addlayer, &lt;SPAN style="color: #008000; font-weight: bold;"&gt;"BOTTOM"&lt;/SPAN&gt;)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
&lt;SPAN style="color: #000080; font-weight: bold;"&gt;del &lt;/SPAN&gt;addlayer, mxd&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Please help.&lt;/STRONG&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 23:10:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533167#M41764</guid>
      <dc:creator>Mohd_NurhafiziSaidin</dc:creator>
      <dc:date>2021-12-11T23:10:56Z</dc:date>
    </item>
    <item>
      <title>Re: Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533168#M41765</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;is topdown=False correct?&lt;/P&gt;&lt;P&gt;have you put in some print statements to see what is being returned at each step in the loops?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Nov 2017 03:52:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533168#M41765</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-11-21T03:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533169#M41766</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hye Dan,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yups..topdown is correct. The result of print statements is below;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="print output of loop" class="image-1 jive-image j-img-original" src="/legacyfs/online/384487_printout pycharm result.png" style="width: 620px; height: 348px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="" class="image-2 jive-image j-img-original" src="/legacyfs/online/384489_printout pycharm result_02.png" style="width: 620px; height: 253px;" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How I want add the shapefile from the last folder, thats mean the bottom of printout. Because in this root folder, the folder with date will be update with the new folder following by date. My task is to pick up the latest folder (CURRENT date) that contain the shapefile.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Nov 2017 07:11:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533169#M41766</guid>
      <dc:creator>Mohd_NurhafiziSaidin</dc:creator>
      <dc:date>2017-11-21T07:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533170#M41767</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I guess I am not seeing the desired outcome list versus the list in the way they are being added.&amp;nbsp; If it is a matter of the order in which they are added, it could a sorting issue&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Nov 2017 10:57:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533170#M41767</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2017-11-21T10:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533171#M41768</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;On a quick look, to me it looks like the for loops are going to try to add all layers.&amp;nbsp; Maybe you can grab date part of the name and do a compare until you find the latest?&amp;nbsp; Or it if it will always be the last item on the list, get a count and try to grab that.&amp;nbsp; Just a few things to try.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Nov 2017 15:26:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533171#M41768</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2017-11-21T15:26:32Z</dc:date>
    </item>
    <item>
      <title>Re: Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533172#M41769</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Dan&amp;nbsp;&lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/check.png" /&gt;&lt;/P&gt;&lt;P&gt;Yups..it's a sorting issue. On progress to solve the debugs.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Dec 2017 07:23:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533172#M41769</guid>
      <dc:creator>Mohd_NurhafiziSaidin</dc:creator>
      <dc:date>2017-12-12T07:23:58Z</dc:date>
    </item>
    <item>
      <title>Re: Add layer from current folder</title>
      <link>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533173#M41770</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Rebecca.&lt;/P&gt;&lt;P&gt;For now, I try to using combination of loop and sorting. Let's see how it goes and if it doesn't, we'll try something else.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Dec 2017 07:29:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-layer-from-current-folder/m-p/533173#M41770</guid>
      <dc:creator>Mohd_NurhafiziSaidin</dc:creator>
      <dc:date>2017-12-12T07:29:56Z</dc:date>
    </item>
  </channel>
</rss>

