AnsweredAssumed Answered

Moved Data - Using Python to replace links in many MXD's, folders.

Question asked by RED_ZMAN on Mar 5, 2019
Latest reply on Mar 11, 2019 by RED_ZMAN

I'm struggling with Python, as usual, as it isn't something I use very often. Using information I've found in ESRI's documentation and here, I'm trying to solve a problem.

 

My office and several others have been given a task to standardize our GIS drives, both folders listed and names of these.  I've written up a crosswalk and don't want my end users to have to manually fix all of the maps they want to use.

 

Using the following...

 

import arcpy
mxd = arcpy.mapping.MapDocument(r"V:\Project_Data\Testing\1_SFO_Base_MapSaveOver.mxd")
mxd.findAndReplaceWorkspacePaths(r"v:\Data", r"v:\Reference\Data")
mxd.findAndReplaceWorkspacePaths(r"v:\Imagery", r"v:\Raster\Imagery")
mxd.findAndReplaceWorkspacePaths(r"v:\images", r"v:\Raster\Imagery")
mxd.findAndReplaceWorkspacePaths(r"v:\images\Contours", r"v:\Raster\Imagery")
mxd.findAndReplaceWorkspacePaths(r"v:\Project_Data", r"v:\Projects")
mxd.findAndReplaceWorkspacePaths(r"v:\UAS", r"v:\Projects\UAS")
mxd.save()
del mxd

 

I've been able to stick the location of one map in and have it come out completely working (we copied the data for now to test) and remapped correctly. This is great for single maps, but I've got somewhere in the realm of 2,000 map files I need to correct. I'd like to be able to point this at a folder and have it do everything inside of it, and the subfolders. I don't want to attempt to fix all 2K maps at once, but doing a few dozen at once for our most used projects backwards would be great.

 

I've tried to cobble something together but don't really understand the syntax and am getting an error (I've probably done something completely wrong). 

#Importing Modules
import arcpy
from arcpy import env
#Put file path for the folder containing mxds, where I have my sample file path  
env.workspace = r"V:\Project_Data\Testing\AllMapsAtOnce"  
  
#Looping through all filepaths in our workspace, make a map document object out of them, then replace their workspace path  
for file in arcpy.ListFiles("*.mxd"):  
  mxd = arcpy.mapping.MapDocument(file)  
mxd.findAndReplaceWorkspacePaths(r"v:\Data", r"v:\Reference\Data")
mxd.findAndReplaceWorkspacePaths(r"v:\Imagery", r"v:\Raster\Imagery")
mxd.findAndReplaceWorkspacePaths(r"v:\images", r"v:\Raster\Imagery")
mxd.findAndReplaceWorkspacePaths(r"v:\images\Contours", r"v:\Raster\Imagery")
mxd.findAndReplaceWorkspacePaths(r"v:\Project_Data", r"v:\Projects")
mxd.findAndReplaceWorkspacePaths(r"v:\UAS", r"v:\Projects\UAS")
mxd.save()
    
del file; del mxd

I've cribbed this together using script portions I've found across here and GIS Stackexchange.

 

The error I get is...

 

Traceback (most recent call last):
  File "V:/Project_Data/Testing/Scripts/AllMXDsOneFolderMainFixesSaveOver.py", line 10, in <module>
    mxd = arcpy.mapping.MapDocument(file)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\arcobjects\mixins.py", line 651, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.

 

Does anyone out there know how I can get this finished and working correctly?  I've actually got some more changes I need to add in for other moved folders but I can get those added if I get something working.

 

Thank you ahead of time.

Outcomes