AnsweredAssumed Answered

Verify mxd.findAndReplaceWorkspacePaths()

Question asked by jpilbeam on Jun 14, 2018
Latest reply on Jun 22, 2018 by jpilbeam

We have a new database connection and all our MXDs have to be updated. I ran a script to update the layers on a bunch of MXDs. Then I opened a map, right clicked a layer, properties, then checked under the source tab to see if the changes took place. But, the layer was still referencing the old connection. Is there a snippet I could include in this script, or something else, that verifies that the layers have indeed been updated?


There is a print statement in the script, but that just prints the name of the MXD. It doesn't really give concrete proof. 


import arcpy
import os

def find_and_replace(MXD_workspace, oldPath, newPath):
    arcpy.env.workspace = MXD_workspace
    path = arcpy.env.workspace

    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".mxd"):
                mxd_name = name
                fullpath = os.path.join(root,name)
                print mxd_name
                print fullpath
                mxd = arcpy.mapping.MapDocument(fullpath)
                for df in arcpy.mapping.ListDataFrames(mxd):
                    for flayer in arcpy.mapping.ListLayers(mxd, "*", df):
                        if flayer.isFeatureLayer or flayer.isRasterLayer:
                                mxd.findAndReplaceWorkspacePaths(oldPath, newPath, False)
                                print "Repaired the path for " + name
                                print(mxd_name + " cannot replace paths")
                del mxd
    print "complete..."

if __name__ == '__main__':
                     r"Database Connections\BonnScott.sde",
                     r"Database Connections\gisEddy.gissql.sde") #(path2MXDs, path2oldGDB, path2newGDB)