Python script for mass find and replace of workspace path?

12621
24
01-23-2013 11:20 AM
KevinBladon
New Contributor II
Hi folks,

As our research group has run out of space on a network drive, our IT department just moved all of our folders/data to a new network drive.  However, in doing this all of the folder structures for the data locations have changed.  As a result, none of our project files (e.g., .mxd) are functioning.  As we have ~150-200 .mxd project files does anyone know if there is a piece of code which can automatically work through all of the ArcGIS related folders and find and replace the mapped network drive info or the folder structure.

I found this piece of code, but this only works for a single file.  What I would like to be able to do is a batch process to look through all folders/subfolders/files and find and replace the workspace path (Note: we are working with ArcGIS 10.1).

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
mxd.findAndReplaceWorkspacePaths(r"C:\Project\Data", r"D:\Project\Data")
mxd.saveACopy(r"D:\Project\Project.mxd")
del mxd

Thanks.
Tags (2)
24 Replies
IanMurray
Frequent Contributor

Not a problem at all.  I rarely get to do much coding work at my current job, so I hang around the forums to help people out and keep my python skills sharp. 

0 Kudos
IanMurray
Frequent Contributor

Also for future reference if you ever need to find out if picture element source image exists, you can use arcpy.Exists to check.  I wasn't sure it works on picture elements, but since JPEGs can be used as a layout element or as a dataset in ArcMap, you can.

I used the map document you uploaded and it did return False that the source image file wasn't in existence anymore.  It would be fairly easy to adjust the code we already worked on to check all picture elements in your map documents for broken links this way, as opposed to broken data links in your map layers.  You could have it write to a text file which ones had broken links to picture elements and which map documents they were in and only work on those.  I'm mentioning this since I said earlier you couldn't do this with arcpy but you actually can.

0 Kudos
JaredPilbeam1
Occasional Contributor

Back again. It actually wasn't working. Started to mess with it some more and now have it working. I think the map doc I was looking at after the initial "success" was one I changed manually a few days ago only to forget which one. 

Anyway, I had to add overwriteOutput = True since the documents were already in the folder. Then it worked. 

For what it's worth, the Element Name stayed the same (ie Jackson...) while the picture source path changed to the new folder:

 

Also, thanks for the tip on arcpy.Exists. I'll be piecing that together next. 

0 Kudos
IanMurray
Frequent Contributor

Actually that is pretty interesting since the name obviously did change when the original link was broken, but when a new valid source path was made it didn't update the name.  Obviously if you wanted to change the name property when the imageSource property was changed you could, but good to know for the future.  I do more work with the arcpy mapping module than anything else(just had a script finish producing ~3600 map documents) so something for me to keep in mind.

0 Kudos
JaredPilbeam1
Occasional Contributor

Yes.

Not working/old path:     \\Jackson\gis_dept\Data\Images\County-logo-1x1Small.JPG

Working/new path:         P:\Images\County-logo.JPG 

I thought it wouldn't be bad either? The script I posted above, and again here, seems nice. But it doesn't want to work in a case where the path is broken. So, line 3 is calling the mxd I zipped above. And, as noted, because of the broken path, the pictures aren't in the mxd. That's why I think this script isn't working. In other words, I think the pictures have to be there for the script to work.

Description: find a picture element using a wildcard and then change the picture's data source

import arcpy

mxd = arcpy.mapping.MapDocument(r'R:\AtlasMaps\ATLAS_MAPS_17\New folder\1st_District_B2.mxd')
for elm in arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "*logo*"):
    if elm.name == 'County-logo-1x1Small':
        elm.sourceImage = r'P:\Images\County-logo.JPG'
        print '{} changed'.format(elm.sourceImage)
mxd.saveACopy(r'R:\AtlasMaps\ATLAS_MAPS_17\New folder\1st_District_B3.mxd')
del mxd‍‍‍‍‍‍‍‍‍

Taken from here: ListLayoutElements—Help | ArcGIS for Desktop  

Hm, that is interesting. I'll keep that in mind. 

0 Kudos