Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a large number of project folders where each project folder can have many subfolders. These subfolders can contain mxds, shapefiles, personal gdb's and fgdb's. In many cases the user forgot to check the "Use Relative Paths" before saving the mxds, so when the folder gets copied, datasource links become broken.

I would like to write a standalone arcpy script that crawls through the directories, opens each mxd, sets the relativePaths to true, then saves the mxd. Is there a script that does this somewhere?

The relativePaths property on the MapDocument object is readonly. Is there some other way to change this?

share|improve this question
    
I am unsure of this is possible or not using that workflow. But what I do think could work is if you automatically open each .mxd, copy all data sources and information from that original .mxd into a new blank .mxd that has relative paths checked, and then re-save the new one. Just an idea as a work-around if your first way ends up not being possible. –  MLowry Jul 7 '11 at 18:14
    
I don't think it's possible. You could get a list of all documents using absolute paths and their authors and SPAM them with a stern e-mail, though. –  Sean Jul 7 '11 at 19:18
add comment

2 Answers

Not possible in 10.0, but I have had some success using ArcObjects in Python for relatively simple tasks like this where writing a .NET application would be overkill, but the necessary functionality was not exposed in the arcpy/arcgisscripting modules. Use os.walk for the crawling the directory tree part.

For the ArcObjects bit, use IMapDocument.Open() to open the MXD (without having to have it open in ArcMap), cast to IMxDocument and set RelativePaths to true, and use IMapDocument.Save() to save it.

share|improve this answer
    
Have you verified that this actually fixes the paths to relative? –  Jason Scheirer Jul 9 '11 at 18:47
    
I have not tried it myself, no, but this thread suggests it should work: forums.esri.com/Thread.asp?c=93&f=1154&t=260812 –  blah238 Jul 9 '11 at 21:04
    
Also I'm sure Kirk already knows how to do this: forums.esri.com/Thread.asp?c=93&f=992&t=64946 :) –  blah238 Jul 9 '11 at 21:08
    
@blah238 That's VBA, what I need is a standalone arcpy script. –  Kirk Kuykendall Aug 23 '11 at 14:00
1  
The question was in reference to doing this with arcpy, not .NET, and as I understand it's not possible to do with arcpy at 10.0 (see the first link in my answer). –  blah238 Jan 26 '12 at 18:19
show 3 more comments

This is possible using the following python script (with thanks to Neil Clemmons over at the ESRI forum). You will need the comtypes library and the Snippets module from Pierssen Consulting.

from Snippets import GetLibPath, InitStandalone
from comtypes.client import GetModule, CreateObject
import os

m = GetModule(GetLibPath() + 'esriCarto.olb')
InitStandalone()
file_dir = r'C:\dir'
for filename in os.listdir(file_dir):
   fullpath = os.path.join(file_dir, filename)
   p = CreateObject(m.MapDocument, interface=m.MapDocument)
   p.Open(fullpath)
   newpath = os.path.join(file_dir,'Changed_' + filename)
   p.SaveAs(newpath,True,False)
   del p
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.