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?
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
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.
because the problem will be as shown in my lines 6 and 11
I needed to be quicker to get a reply in ahead you when it is a Python question. How’s retirement?
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