<?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: Python -?- FindAndReplaceWorkspacePaths for directory and all subdirectories? in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/python-findandreplaceworkspacepaths-for-directory/m-p/41228#M1499</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Jeff,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I use the os.walk function to do any recursive directory "thingies" ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Here a stub-code sample you might find useful:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# ListMXDs.py
# 
# Usage: ListMXDs &amp;lt;Directory to search&amp;gt;
# ---------------------------------------------------------------------------
import sys, os
#path to search - here hardcoded
theDirectory=r'C:\Archive'
#if parsed as argument
#theDirectory = sys.argv[1]
#File-Pattern to search
thePattern = "mxd"
#cycle through the directory and find all Files of type ...
for root, folders, AllFiles in os.walk(theDirectory):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "********** \nWorking on folder "+root+"\n**********"
&amp;nbsp;&amp;nbsp;&amp;nbsp; # now find mxds and print their names&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for oneFile in AllFiles:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #only do something if it is an mxd
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if oneFile.endswith(thePattern):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print oneFile
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Guenter&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Dec 2021 21:35:57 GMT</pubDate>
    <dc:creator>GünterDörffel</dc:creator>
    <dc:date>2021-12-10T21:35:57Z</dc:date>
    <item>
      <title>Python -?- FindAndReplaceWorkspacePaths for directory and all subdirectories?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-findandreplaceworkspacepaths-for-directory/m-p/41227#M1498</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm looking to update a bunch of file references in MXDs througout a directory and all included subdirectories. This is a project I've been delaying until the ArcPy module was available, but now that it's here I'm having an issue getting it to work with those pesky subdirectories.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In the ArcGIS 10.0 help file, there is &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Updating_and_fixing_data_sources_with_arcpy_mapping/00s30000004p000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;this topic &lt;/A&gt;&lt;SPAN&gt;regarding the Python FindAndReplaceWorkspacePath function. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm looking to tweak this code (example 2 from that referenced link)...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, os
folderPath = r"C:\Project"
for filename in os.listdir(folderPath):
&amp;nbsp;&amp;nbsp;&amp;nbsp; fullpath = os.path.join(folderPath, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.isfile(fullpath):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basename, extension = os.path.splitext(fullpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if extension.lower() == ".mxd":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(fullpath)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"\\ComputerName\Project\Data")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
del mxd&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This code only seems to work for C:\Project and does not touch any MXDs in subdirectories.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;How can I tweak this to include subdirectories in the process?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:35:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-findandreplaceworkspacepaths-for-directory/m-p/41227#M1498</guid>
      <dc:creator>JeffPickles</dc:creator>
      <dc:date>2021-12-10T21:35:55Z</dc:date>
    </item>
    <item>
      <title>Re: Python -?- FindAndReplaceWorkspacePaths for directory and all subdirectories?</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-findandreplaceworkspacepaths-for-directory/m-p/41228#M1499</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Jeff,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I use the os.walk function to do any recursive directory "thingies" ...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Here a stub-code sample you might find useful:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# ListMXDs.py
# 
# Usage: ListMXDs &amp;lt;Directory to search&amp;gt;
# ---------------------------------------------------------------------------
import sys, os
#path to search - here hardcoded
theDirectory=r'C:\Archive'
#if parsed as argument
#theDirectory = sys.argv[1]
#File-Pattern to search
thePattern = "mxd"
#cycle through the directory and find all Files of type ...
for root, folders, AllFiles in os.walk(theDirectory):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "********** \nWorking on folder "+root+"\n**********"
&amp;nbsp;&amp;nbsp;&amp;nbsp; # now find mxds and print their names&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for oneFile in AllFiles:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #only do something if it is an mxd
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if oneFile.endswith(thePattern):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print oneFile
&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Guenter&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 21:35:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-findandreplaceworkspacepaths-for-directory/m-p/41228#M1499</guid>
      <dc:creator>GünterDörffel</dc:creator>
      <dc:date>2021-12-10T21:35:57Z</dc:date>
    </item>
  </channel>
</rss>

