<?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: Changing data source of .lyr files in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743528#M57466</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Did you try the alternative:&lt;/P&gt;&lt;P&gt;Layer.replaceDataSource(workspace_path, workspace_type, dataset_name, {validate})&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000004p000000" title="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000004p000000"&gt;ArcGIS Help (10.2, 10.2.1, and 10.2.2)&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 30 Jan 2015 02:12:29 GMT</pubDate>
    <dc:creator>XanderBakker</dc:creator>
    <dc:date>2015-01-30T02:12:29Z</dc:date>
    <item>
      <title>Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743524#M57462</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've got 65 layer files (both normal and group ones) that need their data sources changed. I got the following script to work for MXDs (which is good since there are several hundred):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt;HomerString = [list of mxds with paths]&lt;/SPAN&gt;
&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt;for bob in HomerString:&lt;/SPAN&gt;
&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(bob)&lt;/SPAN&gt;
&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt; mxd.findAndReplaceWorkspacePaths("WC-HOMER", "WC-HOMERB")&lt;/SPAN&gt;
&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; NewString = bob.replace(r'\\WC-HOMER', r'\\WC-HOMERB')&lt;/SPAN&gt;
&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(NewString)&lt;/SPAN&gt;
&lt;SPAN style="font-size: 12.0pt; font-family: 'Times New Roman','serif';"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I cannot figure out any variation of it that will work for .lyr files&lt;/P&gt;&lt;P&gt;(and I did look at code snippets, help documents, and Google before posting here)&lt;/P&gt;&lt;P&gt;Any ideas or solutions out there?&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Tycho&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:38:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743524#M57462</guid>
      <dc:creator>TychoGranville</dc:creator>
      <dc:date>2021-12-12T07:38:27Z</dc:date>
    </item>
    <item>
      <title>Re: Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743525#M57463</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Is something like this what you are after:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;layers = [list of layers with paths]
for layer_path in layers:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = arcpy.mapping.Layer(layer_path)
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(...)
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.saveACopy(...)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:38:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743525#M57463</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-12T07:38:30Z</dc:date>
    </item>
    <item>
      <title>Re: Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743526#M57464</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Unlike the MXD version of findandreplace, which ends in paths, plural, the LYR version of findandreplace end in path, singular.&amp;nbsp; I am not sure whether the LYR version recursively searches or not, like the MXD version.&amp;nbsp; If not, you can add some code to loop through all the layers in a layer file:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;layer_paths = [list of layers with paths]
for layer_path in layer_paths:
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer = arcpy.mapping.Layer(layer_path)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for lyr in arcpy.mapping.ListLayers(lyr):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.findAndReplaceWorkspacePath(...)
&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.saveACopy(...)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:38:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743526#M57464</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-12-12T07:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743527#M57465</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I tried variations of that for the last couple of days. I got rid of all the nifty for-in loops and lists. It's down to this -&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Modules
import arcpy
lyr = arcpy.mapping.Layer(r"\\WC-HOMER\Shared\GIS\2N15E.lyr")
lyr.findAndReplaceWorkspacePath("WC-HOMER", "WC-HOMERB")
lyr.saveACopy(r"\\WC-HOMERB\Shared\GIS\2N15E.lyr")
del lyr&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I don't comment out line 4 I get the error&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;ValueError: Layer: Unexpected error&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #303030;"&gt;I've tried using various strings for line 4, including (r"\\WC-HOMER", r"\\WC-HOMERB"). At this point I'm almost out of hair to pull...&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 07:38:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743527#M57465</guid>
      <dc:creator>TychoGranville</dc:creator>
      <dc:date>2021-12-12T07:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743528#M57466</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Did you try the alternative:&lt;/P&gt;&lt;P&gt;Layer.replaceDataSource(workspace_path, workspace_type, dataset_name, {validate})&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000004p000000" title="http://resources.arcgis.com/en/help/main/10.2/index.html#//00s30000004p000000"&gt;ArcGIS Help (10.2, 10.2.1, and 10.2.2)&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Jan 2015 02:12:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743528#M57466</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2015-01-30T02:12:29Z</dc:date>
    </item>
    <item>
      <title>Re: Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743529#M57467</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;That error with findAndReplaceWorkspacePath is common when you are trying to not just replace the workspace path but also the workspace type.&amp;nbsp; Are you switching from a personal geodatabases to a file geodatabases?&amp;nbsp; If you are switching workspace types as well, then you need to use replaceDataSource as &lt;A href="https://community.esri.com/migrated-users/3100"&gt;Xander Bakker&lt;/A&gt;‌ suggests.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, if you are going to be coming across group layers or layers with lots of different data sources, it is good to use the layer.supports method to check the layer supports a data source property.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Jan 2015 14:44:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743529#M57467</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2015-01-30T14:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Changing data source of .lyr files</title>
      <link>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743530#M57468</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for your help, guys. Hopefully this will help the next newbie that stumbles into this.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It looks like i was formatting the&lt;EM&gt; findAndReplaceWorkspacePath&lt;/EM&gt; incorrectly. It appears that just changing the server name is not enough; you have to have at least part of the old and new paths as well. As all the data and directory structure was copied onto the new server I thought all I needed was to substitute the new server name for the old one (so from Homer to HomerB in my case). As soon as I changed the string to a real path -&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;layer.findAndReplaceWorkspacePath(r"\\WC-HOMER\Shared\GIS", r"\\WC-HOMERB\Shared\GIS")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;- everything works as advertised&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Feb 2015 19:35:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/changing-data-source-of-lyr-files/m-p/743530#M57468</guid>
      <dc:creator>TychoGranville</dc:creator>
      <dc:date>2015-02-06T19:35:36Z</dc:date>
    </item>
  </channel>
</rss>

