<?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 a multi Layer files to mxd using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421811#M33129</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I cant find the &amp;gt;&amp;gt; sign so the code will marked...&lt;/P&gt;&lt;P&gt;i run this code and nothing happend. the first code is working&lt;/P&gt;&lt;P&gt;what to do now?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&amp;nbsp; &lt;/P&gt;&lt;P&gt;import os&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;# suppose you want to add it to the current MXD (open MXD)&amp;nbsp; &lt;/P&gt;&lt;P&gt;mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")&amp;nbsp; &lt;/P&gt;&lt;P&gt;dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;# base folder&amp;nbsp; &lt;/P&gt;&lt;P&gt;workspace = r"C:\Project\layers"&amp;nbsp; &lt;/P&gt;&lt;P&gt;walk = arcpy.da.Walk(workspace, datatype="Layer")&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;for dirpath, dirnames, filenames in walk:&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in filenames:&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layerfile = os.path.join(dirpath, filename)&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addlayer = arcpy.mapping.Layer(layerfile)&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;arcpy.RefreshTOC()&amp;nbsp; &lt;/P&gt;&lt;P&gt;arcpy.RefreshActiveView()&amp;nbsp; &lt;/P&gt;&lt;P&gt;del addlayer, mxd &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 20 Jan 2015 08:45:33 GMT</pubDate>
    <dc:creator>Yaron_YosefCohen</dc:creator>
    <dc:date>2015-01-20T08:45:33Z</dc:date>
    <item>
      <title>Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421807#M33125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;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 &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://gis.stackexchange.com/questions/35693/use-python-to-add-layers-to-toc?rq=1" rel="nofollow" target="_blank"&gt;http://gis.stackexchange.com/questions/35693/use-python-to-add-layers-to-toc?rq=1&lt;/A&gt;&lt;/P&gt;&lt;P&gt;and I try, with arcpy, to detect all the layers and add them to mxd. &lt;/P&gt;&lt;P&gt;Is it possible? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Jan 2015 09:57:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421807#M33125</guid>
      <dc:creator>Yaron_YosefCohen</dc:creator>
      <dc:date>2015-01-19T09:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421808#M33126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I believe it is possible.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Search the layer using .lyr extension in the directory and then use Add layer to add the searched lyr in MXD.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Add layer &lt;/P&gt;&lt;P&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000025000000" rel="nofollow" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000025000000&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy, os&lt;/P&gt;&lt;P&gt;folderPath = r"C:\Project"&lt;/P&gt;&lt;P&gt;for filename in os.listdir(folderPath):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; fullpath = os.path.join(folderPath, filename)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isfile(fullpath):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basename, extension = os.path.splitext(fullpath)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if extension.lower() == ".mxd":&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Use Add layer here&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()&lt;/P&gt;&lt;P&gt;del mxd&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the&amp;nbsp; above code .mxd files are searched. Using&amp;nbsp; the same code you can search your .lyr and add then in the desired mxd.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am not python expert. Hope this information is helpful. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Jan 2015 11:36:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421808#M33126</guid>
      <dc:creator>KishorGhatage</dc:creator>
      <dc:date>2015-01-19T11:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421809#M33127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;First of all you should really use syntax highlighting: &lt;A href="https://community.esri.com/migration-blogpost/1070" target="_blank"&gt;Posting Code blocks in the new GeoNet&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To recursively loop through folder and sub folder and detect the layerfiles (.lyr) you can use the arcpy.da.Walk functionality (asuming you have a recent version of ArcGIS).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os

workspace = r"D:\Xander\GeoNet"

walk = arcpy.da.Walk(workspace, datatype="Layer")
for dirpath, dirnames, filenames in walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in filenames:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (os.path.join(dirpath, filename))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This prints each .lyr file it finds:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;D:\Xander\GeoNet\StackedLabels\aLayer.lyr&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;D:\Xander\GeoNet\StackedLabels\subfolder\subsubfolder\subsubsubfolder\AnotherLayer.lyr&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In case you want to add all the layer found to an existing MXD you should open the MXD before the loop and add put the add layerfile logic inside the loop. Adding a lot of layer to an MXD will probably make it very slow. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your code might look like this (in case you run this inside a session of ArcMap and want to add the layers to the current session of ArcMap, hence the keyword "CURRENT"):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os

# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# base folder
workspace = r"D:\Xander\GeoNet"
walk = arcpy.da.Walk(workspace, datatype="Layer")

for dirpath, dirnames, filenames in walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in 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, "BOTTOM")

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del addlayer, mxd&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In case you want to run it as a standalone script and/or add the layers to a different existing MXD, you should replace the keyword "CURRENT" on line 5 by a reference (string containing path and filename) to the MXD.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:03:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421809#M33127</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T19:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421810#M33128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Xander. It's very helpful.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Jan 2015 12:41:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421810#M33128</guid>
      <dc:creator>KishorGhatage</dc:creator>
      <dc:date>2015-01-19T12:41:54Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421811#M33129</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I cant find the &amp;gt;&amp;gt; sign so the code will marked...&lt;/P&gt;&lt;P&gt;i run this code and nothing happend. the first code is working&lt;/P&gt;&lt;P&gt;what to do now?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&amp;nbsp; &lt;/P&gt;&lt;P&gt;import os&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;# suppose you want to add it to the current MXD (open MXD)&amp;nbsp; &lt;/P&gt;&lt;P&gt;mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")&amp;nbsp; &lt;/P&gt;&lt;P&gt;dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;# base folder&amp;nbsp; &lt;/P&gt;&lt;P&gt;workspace = r"C:\Project\layers"&amp;nbsp; &lt;/P&gt;&lt;P&gt;walk = arcpy.da.Walk(workspace, datatype="Layer")&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;for dirpath, dirnames, filenames in walk:&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in filenames:&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layerfile = os.path.join(dirpath, filename)&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addlayer = arcpy.mapping.Layer(layerfile)&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")&amp;nbsp; &lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;arcpy.RefreshTOC()&amp;nbsp; &lt;/P&gt;&lt;P&gt;arcpy.RefreshActiveView()&amp;nbsp; &lt;/P&gt;&lt;P&gt;del addlayer, mxd &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jan 2015 08:45:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421811#M33129</guid>
      <dc:creator>Yaron_YosefCohen</dc:creator>
      <dc:date>2015-01-20T08:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421812#M33130</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="font-size: 14.4444446563721px; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;Just added save statement&lt;/P&gt;&lt;P style="font-size: 14.4444446563721px; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
import os

# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# base folder
workspace = r"C:\Project\layers"
walk = arcpy.da.Walk(workspace, datatype="Layer")

for dirpath, dirnames, filenames in walk:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in 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, "BOTTOM")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
del addlayer, mxd&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:03:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421812#M33130</guid>
      <dc:creator>KishorGhatage</dc:creator>
      <dc:date>2021-12-11T19:03:27Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi Layer files to mxd using python</title>
      <link>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421813#M33131</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;the code added just 2 lyr.file in the workspace. it didn't added the lyr in the sub folders&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 20 Jan 2015 12:32:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-a-multi-layer-files-to-mxd-using-python/m-p/421813#M33131</guid>
      <dc:creator>Yaron_YosefCohen</dc:creator>
      <dc:date>2015-01-20T12:32:54Z</dc:date>
    </item>
  </channel>
</rss>

