[GIS] Checking .mxd version using ArcPy

arcgis-10.3arcpymxd

I am looking for a way to check the mxd version of a bunch of mxds. I found a script here at ArcPy method to determine ArcMap document version

I am using ArcGIS 10.3.1 on my desktop.

It goes through the directories and for every mxd it finds it grabs a bunch of details, one of which is the version. My only problem is that sometimes it can not find the version of ArcGIS; it seems to be about 50/50. At first I thought if it didn't return that correct value that meant it was below 10.0. But running my script on 200 mxds it returned the value 9.3 a few times, which means that the ones where it does not return any value could be any version and not just something below 10.0.

The current code taken from the other question:

def getMXDVersion(mxdFile):
    matchPattern = re.compile("9.2|9.3|10.0|10.1|10.2|10.3")
    with open(mxdFile, 'rb') as mxd:
        fileContents = mxd.read().decode('latin1')[1000:4500]
        removedChars = [x for x in fileContents if x not in [u'\xff',u'\x00',u'\x01',u'\t']]
        joinedChars = ''.join(removedChars)
        regexMatch = re.findall(matchPattern, joinedChars)
        #print mxdFile
        #print joinedChars
        #print regexMatch
        if len(regexMatch) > 1:
            version = regexMatch[len(regexMatch) - 1]
            return version
        elif len(regexMatch) == 1:
            version = regexMatch[0]
            return version
        else:
            return 'version could not be determined for ' + mxdFile

Is there any other way to find the map document's version number?

Best Answer

What you are after may not be determinable, it requires that the document info was saved/embedded and as you've found that is not often embedded into the document. I cannot find any reference to when this data is saved/skipped. For what it's worth this is another method:

import os, sys, arcpy
from comtypes.client import GetModule, CreateObject
BasePath = sys.argv[1]
# read http://gis.stackexchange.com/questions/80/how-do-i-access-arcobjects-from-python
# read it CAREFULLY and follow the links, there's more information needed on the links

m = GetModule(r'C:\Your path\Desktop10.1\com\esriCarto.olb') # change as needed
import comtypes.gen.esriCarto as esriCarto
mapObj = CreateObject(esriCarto.MapDocument ,interface=esriCarto.IMapDocument)

for xPath, xDirs, xFiles in os.walk(BasePath):
    for ThisFile in xFiles:
        fName, fExt = os.path.splitext(ThisFile)
        if fExt.upper() == ".MXD":
            mapObj.Open(os.path.join(xPath,ThisFile))
            verInf = mapObj.GetVersionInfo()
            if verInf[0]:
                arcpy.AddMessage("Version info missing : %s" % ThisFile)
            else:
                arcpy.AddMessage("%s is version %d.%d" %(ThisFile,verInf[1],verInf[2]))

This uses COM types and the IMapDocument.GetVersionInfo interface in python; as I have discovered only about half of my map documents have this metadata attached to them. This however does expose another interface IMapDocument.DocumentVersion which, when it returns a value of 0 means the MapDocument can be opened and 1 or 2 if it can't.. at least you can decide if the MXD can be opened with the current version of ArcMap (value=0) or is a future version (value=1) or the MXD is likely to be broken (value=2).

Related Question