Store relative path for map documents based on ArcGIS Desktop Version

864
3
12-01-2017 11:48 AM
Brian_McLeer
Occasional Contributor II

From a prior posting here, I was looking for a solution on how to turn the relative path on in batch. The solution generated will grab any MXD in a root folder and enable the relative paths to be turned on in any sub folder using this code:

import os
from glob import glob
import arcpy

PATH = r'C:\Users\mclbr\Desktop\GIS_Maintenance'
arcpy.env.workspace = PATH

result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.mxd'))]
for map in result:  # map will be complete path to map file
    print "Processing: {}".format(map) # keep track of what's happening
    mxd = arcpy.mapping.MapDocument(map)
    #set relative paths property
    mxd.relativePaths = True
    #save map document change
    mxd.save()

One issue that I have come across is that we have users on 10.2.2 and users on 10.3.1. The code above works on 10.3.1, but when run it updates all 10.2.2 MXDs to 10.3.1. The other issue is that when run on a 10.2.2 machine, the script will fail once it reaches an MXD that was last saved in 10.3.1.

I am taking a shot in the dark, but would anyone know of a way to create code that will only select versions of ArcGIS Desktop for version 10.2.x, then one for 10.3.x? 

If that is possible, I would run the routine on a 10.3.1 machine and insert the code to only update MXDs that are a 10.3.1 document, and do the same for the 10.2.2 machine and documents. 

Brian
0 Kudos
3 Replies
MitchHolley1
MVP Regular Contributor

Stack Exchange has a similar question with an answer.  Check it out: Using ArcPy to determine ArcMap document version? 

RandyBurton
MVP Alum

And a second version on stack exchange: Checking .mxd version using ArcPy?

Also of interest: How To: Save map documents in ArcGIS 10.x to previous version of ArcGIS in batch

A try/except code block could work for the 10.2 version trying to open a 10.3 file.

RandyBurton
MVP Alum

As a follow-up to my previous...

Run a version on 10.2 with a try/except block and write the exceptions to a file.

import os
from glob import glob
import arcpy

f = open('map_list.txt', 'w')

PATH = r'C:\Start\Here'
arcpy.env.workspace = PATH

result = [y for x in os.walk(PATH) for y in glob(os.path.join(x[0], '*.mxd'))]
for map in result: # map will be complete path to map file
    try:
        mxd = arcpy.mapping.MapDocument(map)
        #set relative paths property
        mxd.relativePaths = True
        #save map document change
        mxd.save()
    except:
        # assume exceptions are version 10.3
        f.write(map + '\n')

f.close()

With 10.3 process the maps from the file, which are assumed to be 10.3.

import arcpy

with open('map_list.txt') as f:
    maps = f.readlines()
f.close()

# remove whitespace characters like `\n` at the end of each line
maps = [x.strip() for x in maps]

for map in maps:
    try:
        mxd = arcpy.mapping.MapDocument(map)
        #set relative paths property
        mxd.relativePaths = True
        #save map document change
        mxd.save() # current version (10.3)
        mxd.saveACopy('modified map name',"10.1") # for older version
    except:
        print "Failed processing {}".format(map)

By using mxd.saveACopy you could create a copy in version 10.1 (which is used by 10.2) by modifying the map document name.

I did some experimenting with the code suggested at stack exchange, but it sometimes failed at getting the version number.  It seems there is no consistent way to do this.  Unfortunately, there is no map document property for file version.