Python script for mass find and replace of workspace path?

9081
24
01-23-2013 11:20 AM
Highlighted
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
Highlighted
Occasional Contributor III
This might help you,

import arcpy, os

path = os.getcwd() # or path = r"C:\Documents"

for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension == '.mxd':
            mxd = arcpy.mapping.MapDocument(fullPath)

            mxd.findAndReplaceWorkspacePaths(r"oldfilepath1", r"newfilepath1")
            mxd.findAndReplaceWorkspacePaths(r"oldfilepath2", r"newfilepath2")
                     
            mxd.save()

del mxd




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.
Reply
0 Kudos
Highlighted
MVP Regular Contributor
Tweaked a bit to walk through subfolders.

import os,fnmatch,arcpy
def find(pattern, path=os.getcwd()):
    for path, dirs, files in os.walk(path):
        for filename in files:
     if fnmatch.fnmatch(filename, pattern):yield os.path.abspath(os.path.join(path, filename))
  
for f in find('*.mxd', 'C:/Data'):  
    mxd = arcpy.mapping.MapDocument(f)
    mxd.findAndReplaceWorkspacePaths(r"oldfilepath1", r"newfilepath1")
    mxd.findAndReplaceWorkspacePaths(r"oldfilepath2", r"newfilepath2")
  
    mxd.save()
Reply
0 Kudos
Highlighted
Occasional Contributor
This script should work to list folders in a workspace then list .mxd documents in those folders.  Then you can update the sources using arcpy.mapping.
[ATTACH=CONFIG]21457[/ATTACH]
Reply
0 Kudos
Highlighted
New Contributor

Hey Kevin.

I was just curious if either of these answers worked for you. I am about to be in the same boat, with somewhere near a thousand MXD's. I'm going to have to put something together over the next couple of days.

Thanks Kevin.

-Josh

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

This addin/toolbar (for ArcCatalog) is still in my final developement.....but I'm getting close to getting my addin that will do several things, including listing all your broken links, and then give the option to update.  I'll attache the documentation, which is all over the place as far as instructions, and the addin, which has been tested in 10.3.x (but should work in 10.2).  Instruction for loading (from an email I sent to test staff earlier this morning)

Save the ChkandFixLinks.esriaddin file to anywhere handy.  Double click, it will ask if you want to install…click Install Add-in.

  pic1.jpg

Then open or reopen ArcCatalog.  If you don’t see the toolbar, or not sure, go to Customize (on top menu bar)

Click on that and you will see Toolbars>   …hover to the right and make sure the toolbar is checked.  If you don’t see it, click it off/on until you see where it is.

pic2.jpg

Click on the third icon over (for the one that will give a unique list of all broken links in a folder)

pic3.png

In the dialog box, use the folder icon and then navigate to the folder where all you mxd’s are located (it will go thru (walk) all the subfolders underneath too) and leave the other two as they are

Then click ok, and it will run.  When completed, it will have created a couple files…they are the same, just one is and Excel file, and the other is comma delimited….I suggest opening the Excel (.xls) file to review.  They will be called BrokenSrc with the date/time it was run.

Sorry this is pretty messy....when it's completed with the additional scripts, I will post (probably to ArcScripts 2.0).  The 4th icon over will do a bulk change from drive->drive or drive->UNC    look at the Samples folder (if you unzip the esriaddin ("after installing) and there is a samples folder for format.  After the bulk updates, it will run a new unique list.  The next script which is almost complete, will allow for changing longer paths, shapes or coverages to FGDB, etc.    Feedback is welcome and appreciated (especially if it doesn't work at all for you).  BTW - the first three tools don't change anything, just create lists/reports so you can see what you need to fix....i.e. other than createing .txt, .xls, and .csv files (all with unique name_date_time for file name, they can not do any damage (but you must have write access to the folder you are analyzing. (although setting the path might work (haven't tested).

I hope these tools help.

Reply
0 Kudos
Highlighted
Occasional Contributor

@Rebecca

This is a great addin. I just tried it and It works fine for listing broken source data. But, my problem is, I need to list broken picture elements. That's something it didn't do? 

I've been trying to use this code (below) from Esri. But, it's not working due to the fact, I think, that the mxd it's referencing is missing the pictures (our file directory was recently changed, so the picture path is broken). It runs with no errror and 1st_District_B3.mxd is saved in the folder, but no changes are made.

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‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Below's a printscn of the document with the broken picture element source. It's giving you the path where the JPEG once lived. It's now in the P:\Images folder.

Are you aware of a way to list and repair broken source picture elements?

Thanks in advance.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

I haven't tried to test for graphic elements, but I can take a look at this later (probably not until later this week).  I know I have a few more types of broken links I wanted to test for, but have moved off of that project for the time being, and since it does about 98%. 

I may have a newer version and better description of my addin on this page, fwiw.

/blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair 

Can you zip you mxd and attach it?  all items will be broken of course for me, but it will give me something to test.  Thanks.

Reply
0 Kudos
Highlighted
Occasional Contributor

http://www.willcogis.org/website2014/gis/Data/1st_District_B2.zip

Here's a link to the mxd. Thanks for taking a look.

I checked your page and downloaded the newer version you had there. I ran it again and pictures (.JPG in my case) are still not included in the list of broken sources. You have 'raster.jpg' included in the valid extension list, but this is apparently different than a picture element (.JPG).

Reply
0 Kudos
Highlighted
Frequent Contributor

Hi Jared,

Can you open you map and check to make sure your picture element is still named?

Alternatively, instead of checking against the name property of the picture element, you could always check against the current imageSource property is "" \\Jackson\gis_dept\Data\Images\County-logo-1x1Small.JPG" and if so then change to r'P:\Images\County-logo.JPG'. 

Unfortunately there is no method in arcpy for checking if Picture Elements to see if the link is no longer valid.  There is probably some python module somewhere that could do this and could be used in conjunction with arcpy to check and update picture element paths

Reply
0 Kudos