<?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 adding 3 lyr layer in all mxd in one folder in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/adding-3-lyr-layer-in-all-mxd-in-one-folder/m-p/1193099#M65340</link>
    <description>&lt;P&gt;hello I have been helped to create a script to add 3 .lyr layers in many mxd in one folder containing several subfolder with .mxd&lt;/P&gt;&lt;P&gt;The following script is not working,&lt;/P&gt;&lt;P&gt;How I am supposed to run it, python on arcmap 10.8 or python consol on window 10?&lt;/P&gt;&lt;P&gt;Many thanks for any input .&lt;/P&gt;&lt;P&gt;Paulo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import arcpy
import arcpy.mapping as m
import os

root_folder = r"path\to\root\folder"

add_lyr_1 = r"path\to\file1.lyr"
add_lyr_2 = r"path\to\file2.lyr"
add_lyr_3 = r"path\to\file3.lyr"

## create list of lyr files
add_lyrs = [add_lyr_1, add_lyr_2, add_lyr_3]

mxd_paths = []

## add all mxd paths to a list
for root, dirs, files in os.walk (root_folder):
    for f in files:
        if f.endswith(".mxd"):
            mxd_paths.append("{0}\\{1}".format(root, f))

## print to test
#print(mxd_paths)

for mxd_doc in mxd_paths:
    mxd_name = mxd_doc.rsplit("\\")[-1]
    arcpy.AddMessage(mxd_name)

    ## access the mxd
    mxd = m.MapDocument(mxd_doc)

    ## only access the desired dataframe
    ## you might want to use a dataframe name / wildcard to
    ## add layers to correct dataframe if you have multiple
    df = m.ListDataFrames(mxd)[0]

    ## add each layer file, one on top of the other.
    for lyr_file in add_lyrs:
        add_lyr = m.Layer(lyr_file)
        m.AddLayer(df, add_lyr ,"TOP")

    mxd.save()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jul 2022 19:22:39 GMT</pubDate>
    <dc:creator>Paul-EmmanuelLeroux</dc:creator>
    <dc:date>2022-07-18T19:22:39Z</dc:date>
    <item>
      <title>adding 3 lyr layer in all mxd in one folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-3-lyr-layer-in-all-mxd-in-one-folder/m-p/1193099#M65340</link>
      <description>&lt;P&gt;hello I have been helped to create a script to add 3 .lyr layers in many mxd in one folder containing several subfolder with .mxd&lt;/P&gt;&lt;P&gt;The following script is not working,&lt;/P&gt;&lt;P&gt;How I am supposed to run it, python on arcmap 10.8 or python consol on window 10?&lt;/P&gt;&lt;P&gt;Many thanks for any input .&lt;/P&gt;&lt;P&gt;Paulo&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import arcpy
import arcpy.mapping as m
import os

root_folder = r"path\to\root\folder"

add_lyr_1 = r"path\to\file1.lyr"
add_lyr_2 = r"path\to\file2.lyr"
add_lyr_3 = r"path\to\file3.lyr"

## create list of lyr files
add_lyrs = [add_lyr_1, add_lyr_2, add_lyr_3]

mxd_paths = []

## add all mxd paths to a list
for root, dirs, files in os.walk (root_folder):
    for f in files:
        if f.endswith(".mxd"):
            mxd_paths.append("{0}\\{1}".format(root, f))

## print to test
#print(mxd_paths)

for mxd_doc in mxd_paths:
    mxd_name = mxd_doc.rsplit("\\")[-1]
    arcpy.AddMessage(mxd_name)

    ## access the mxd
    mxd = m.MapDocument(mxd_doc)

    ## only access the desired dataframe
    ## you might want to use a dataframe name / wildcard to
    ## add layers to correct dataframe if you have multiple
    df = m.ListDataFrames(mxd)[0]

    ## add each layer file, one on top of the other.
    for lyr_file in add_lyrs:
        add_lyr = m.Layer(lyr_file)
        m.AddLayer(df, add_lyr ,"TOP")

    mxd.save()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jul 2022 19:22:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-3-lyr-layer-in-all-mxd-in-one-folder/m-p/1193099#M65340</guid>
      <dc:creator>Paul-EmmanuelLeroux</dc:creator>
      <dc:date>2022-07-18T19:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: adding 3 lyr layer in all mxd in one folder</title>
      <link>https://community.esri.com/t5/python-questions/adding-3-lyr-layer-in-all-mxd-in-one-folder/m-p/1203467#M65343</link>
      <description>&lt;P&gt;Can you provide more detail on how it is failing - for instance the error messages?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Aug 2022 19:49:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-3-lyr-layer-in-all-mxd-in-one-folder/m-p/1203467#M65343</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2022-08-16T19:49:35Z</dc:date>
    </item>
  </channel>
</rss>

