9

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?

  • 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
6

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
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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