Python Find & Replace WorkspacePaths

3244
3
05-28-2015 07:49 AM
JohnByrne
New Contributor

Hi all,

I have created a python script that lists all the mxds within a folder directory and then replaces the workspace paths. Now it works correctly for the majority of mxds but on a couple of the mxds it is failing to to replace the workspace path. I would appreciate any advice. I have copied the code into this question but also attached the code. Please note I have more than 1 database connection hence why there is the two find and replace lines for the two connections.

Thanks

# ##Lists all MXDs within a folder and replaces the SDE connection

for fileName in os.listdir(path):

     fullPath = os.path.join(path, fileName)

     if os.path.isfile(fullPath):

          basename, extension = os.path.splitext(fullPath)

          if extension.lower() == ".mxd":

              mxd = arcpy.mapping.MapDocument(fullPath)

              mxd.findAndReplaceWorkspacePaths(devconn1, conn1)

              mxd.findAndReplaceWorkspacePaths(devconn2, conn2)

              mxd.saveACopy(os.path.join(output, fileName))

             del mxd

0 Kudos
3 Replies
RebeccaStrauch__GISP
MVP Emeritus

A couple things to check.

1) check to make sure the values of devconn1 and devconn2 are the exact same in both MXDs and match the variable (i.e. upper/lower case)

2) I had this problem with on and running MXD doctor fixed it  ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Also, I had an issue when trying to run my script thru ArcCatalog 10.2.x  on a mxd saved as 10.3.  (we have a mixed environment)

Hope this helps.  I'm working on a similar project right now....but with many more broken links.

0 Kudos
BillDaigle
Occasional Contributor III

If the new data sources are SDE databases, I have had better luck updating each individual layers using replaceDataSource.

Here is a sample:

for fileName in os.listdir(path):
    fullPath = os.path.join(path, fileName)
    if os.path.isfile(fullPath):
        basename, extension = os.path.splitext(fullPath)
        if extension.lower() == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullPath)
            for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.supports("DATASOURCE"):
                    #add some code here to check if the old database is being used

                    #update the layer datasource if necessary
                    lyr.replaceDataSource(newSdeCnctPath, "SDE_WORKSPACE", newDataLayerName, False)

            mxd.saveACopy(os.path.join(output, fileName))
            del mxd
JohnByrne
New Contributor

Hi folks,

Rebecca - Yeah I made all though checks and they were ok.

Bill - your solution worked perfectly until I had an mxd that had more than one database connection in there e.g: DARDOFMD@lpisapp.sde & DARDOFMD@suppapp.sde.

So I get an mxd with only some of the layers replaced and others with broken links!

So close...

0 Kudos