2

I was wondering what was the equivalent of "Save As" in ArcPy, which works perfectly fine in ArcMap. As the MXDs are currently set up with relative path moving MXD would break the links, however if MXD is saved with "Save As" option the links would be fine. I was trying to automate the procedure as there are too many files. SaveACopy (option 1) is breaking all links which is given below:

Option 1

import arcpy, os

srcdir = r"C:\temp\test1"

for basename in os.listdir(srcdir):
    if basename.endswith('.mxd'):
        mxdPathName = os.path.join(srcdir, basename)
        mxd = arcpy.mapping.MapDocument(mxdPathName)
        mxd.saveACopy(r"C:\temp\test1\test2\\" + basename)

I have tried to set the MXD to absolute path (option 2) and copy as a new copy which isn't working either (links are still broken).

Option 2

import arcpy, os

Workspace = r"C:\temp\test1"
arcpy.env.workspace = Workspace

mxdList = arcpy.ListFiles("*.mxd")

for file in mxdList:
    filePath = os.path.join(Workspace, file)
    filePathCopy = Workspace + r"test2\\" + file
    mxd = arcpy.mapping.MapDocument(filePath)
    mxd.relativePaths = False
    mxd.save()
    mxd.saveACopy(filePathCopy)
    mxdCopy = arcpy.mapping.MapDocument(filePathCopy)
    mxdCopy.save()
    mxdCopy.relativePaths = True
    mxdCopy.save()
    mxd.relativePaths = True
    mxd.save()
3
  • I do not want to set my MXDs to relative path, they already have relative path set in them! I want to move these MXDs without breaking links. Please read the question
    – Curtis
    Commented Jul 19, 2017 at 11:36
  • Have you tried to use env.workspace instead of os.listdir()?
    – ahmadhanb
    Commented Jul 19, 2017 at 11:58
  • Apologies everyone, both options work - option 2 wouldn't be necessary, apparently when I was testing the MXDs i had already broken the source MXDs - which was daft...sorry @ahmadhanb
    – Curtis
    Commented Jul 19, 2017 at 12:43

3 Answers 3

0

The following code works perfectly, and I could save the documents to a new directory using relative paths, and the feature classes inside the documents are not broken:

import arcpy, os

#workspace to search for MXDs
ws = r"D:\Path\To\Folder\\"
arcpy.env.workspace = ws

#list map documents in folder
mxdList = arcpy.ListFiles("*.mxd")

#set relative path setting for each MXD in list.
for filename in mxdList:
    #set map document to change
    filePath = os.path.join(ws, filename)
    mxd = arcpy.mapping.MapDocument(filePath)
    #set relative paths property
    mxd.relativePaths = True
    #save map document change
    mxd.saveACopy(ws + r"Test\\" + filename)
7
  • unfortunately it only has mxd.save() which would overwrite the current MXD, but i want to move the MXD without links being broken. I tried to put the new MXD path into bracket of as argument save(), but it wasn't successful
    – Curtis
    Commented Jul 19, 2017 at 10:30
  • The MXDs already have relative path set, that's why moving MXD would break things which i'm trying to avoid
    – Curtis
    Commented Jul 19, 2017 at 10:31
  • Yes that's what i tried (code given in my question), but links are getting broken when i open MXDs
    – Curtis
    Commented Jul 19, 2017 at 10:40
  • I updated my answer
    – ahmadhanb
    Commented Jul 19, 2017 at 11:36
  • maybe updating 'mxd.relativePaths = True' in 'mxd.relativePaths = False' would save it with absolute paths, trerefore preventing that relative path break?
    – Hélène
    Commented Jul 19, 2017 at 11:38
0

Maybe this helps: http://support.esri.com/en/technical-article/000011678

which is also echoed here: Programmatical Setting of 'Store Relative Pathways' for multiple MXDs ArcGIS 10.1

=> How about running the code to store the paths as absolute, then move the MXDs, then set the path as relative again?.

0

I would recommend using one of the arcpy updating and fixing data sources methods for each mxd file. Essentially, for each moved mxd file define the old directory and new directory path and re-save.

Updating and fixing data sources with arcpy.mapping

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