Select to view content in your preferred language

Reset the paths script

708
5
07-16-2019 02:58 PM
SteveKovach
Occasional Contributor

I'm trying to get a script to reset the paths of data sources from an archive path to a current path. However, the script is only changing one of the paths and not all of the paths. Here is my code:

import arcpy, os
arcpy.AddMessage("\n\n" + "Re-source MXDs was developed by Carl Beyerhelm, modified by Steve Kovach" + "\n")
mxdFolder = arcpy.GetParameterAsText(0)  ## The project MXD folder
oldFolder    = arcpy.GetParameterAsText(1)  ## The archived data folder path
newFolder    = arcpy.GetParameterAsText(2)  ## The current data folder path
arcpy.env.workspace = mxdFolder
mxdList = arcpy.ListFiles("*.mxd")  ## Get a list of the MXDs
for mxd in mxdList:  ## For each MXD
    arcpy.AddMessage("\n" + "Re-sourcing " + mxd + "...")
    mapDoc = arcpy.mapping.MapDocument(os.path.join(mxdFolder, mxd))  ## Get the mapDoc object
    mapDoc.findAndReplaceWorkspacePaths(r"oldFolder", r"newFolder", True)  ## Replace the obsolete path with the new path
    mapDoc.save()  ## Save the updated MXD
    del mapDoc  ## Release the mapDoc object
arcpy.AddMessage("\n\n" + "OK, done!" + "\n\n")

Any ideas where I'm going wrong?

Tags (1)
0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

you have to raw encode the actual value of the variable, not the variable name, so I would make sure that what is assigned to the variable name is indeed correct, as in the examples below

oldFolder = "c:\a path\not\right"

print(oldFolder)
c: path
ight

print(r"oldfolder")
oldfolder

fixed = r"c:\a path\not\right"

print(fixed)
c:\a path\not\right
LanceCole
MVP Regular Contributor

Steven, 

I formatted your code to be a little easier to read.

import arcpy, os

arcpy.AddMessage("\n\n" + "Re-source MXDs was developed by Carl Beyerhelm, modified by Steve Kovach" + "\n")

mxdFolder = arcpy.GetParameterAsText(0)  ## The project MXD folder
oldFolder = arcpy.GetParameterAsText(1)  ## The archived data folder path
newFolder = arcpy.GetParameterAsText(2)  ## The current data folder path

arcpy.env.workspace = mxdFolder
mxdList = arcpy.ListFiles("*.mxd")  ## Get a list of the MXDs

for mxd in mxdList:  ## For each MXD
    arcpy.AddMessage("\n" + "Re-sourcing " + mxd + "...")
    mapDoc = arcpy.mapping.MapDocument(os.path.join(mxdFolder, mxd))  ## Get the mapDoc object
    mapDoc.findAndReplaceWorkspacePaths(oldFolder, newFolder, True)  ## Replace the obsolete path with the new path
    mapDoc.save()  ## Save the updated MXD
    del mapDoc  ## Release the mapDoc object

arcpy.AddMessage("\n\n" + "OK, done!" + "\n\n")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You need to use the variable names oldFolder and newFolder alone in line 15 above not as raw strings.

DanPatterson_Retired
MVP Emeritus

because the problem will be as shown in my lines 6 and 11

0 Kudos
LanceCole
MVP Regular Contributor

I needed to be quicker to get a reply in ahead you when it is a Python question.  How’s retirement?

0 Kudos
DanPatterson_Retired
MVP Emeritus

So far so good... working a new numpy-based data structure for geometry.  Inputs can be derived from arcpy and/or geojson objects. Certainly speeds up geometry processing compared to cursors