Changing Paths in an MXD to UNC

If you need to publish an MXD to ArcGIS server, any file-based data sources probably need to reference a UNC location (unless you are copying data to the server). Here is the shell of a script that will do this. You will need to change the name of the MXD at the beginning and end of the script.

[raw]

import arcpy
arcpy.env.overwriteOutput = True
mxdName = r"c:\This\is\not\a_real.mxd"
print mxdName
                         
mxd = arcpy.mapping.MapDocument(mxdName)
for df in arcpy.mapping.ListDataFrames(mxd):
    print "Data Frame: " + df.name
    for ln in arcpy.mapping.ListLayers(mxd, "", df):
        if ln.supports("DATASOURCE"):
            sDSName = ln.datasetName
            sSource = ln.dataSource
            sSource = sSource.replace('X:',r'\\mygisserver\vol1\GIS')
            sSource = sSource.replace('x:',r'\\mygisserver\vol1\GIS')
            sSource = sSource.replace('R:',r'\\mygisserver\vol12\BASE')
            sSource = sSource.replace('r:',r'\\mygisserver\vol12\BASE')
           
            sWS = sSource.replace(sDSName,'')
            print sDSName
            print sSource
            print sWS
           
            if ln.isFeatureLayer:
                ln.replaceDataSource(sWS,"FILEGDB_WORKSPACE",sDSName)
        else:
            print "  - " + ln.name
mxd.saveACopy(r"c:\This\is\not\a_real_BaseUNC.mxd")     
del mxd

[/raw]

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Related Posts