leftieant76

replaceDataSource - can't get to fire

Discussion created by leftieant76 on Sep 12, 2012
Latest reply on Sep 13, 2012 by leftieant76
Hi all

I'm trying to write a script that will interrogate an mxd, and change the file path on certain layers.

Context - we have a dynamic version of our corporate data stored at J:\gis_public (network location) in file geodatabases, and a cloned static copy on the local machine at U:\gis_public in file geodatabases.

The cloned version is updated weekly via another process, but the most up to date data is at the J: location.

When completing large or complicated editing tasks, it is far more preferable (and quicker) to work from the U: drive data.

So what I am trying to write is a script that will flick through all the layers in an mxd, and if they are stored at J:\gis_public, change the file path to U:\gis_public

I am having issues with the replaceDataSource function though, and can't find any helpful documentation or code examples anywhere.

This is what I have so far:

# Author: ac0t
# Date: 12 Sep 2012
# Purpose: Interrogate layers within an MXD
# If layer is in U:\gis_public, change path to J:\gis_public

import arcpy

# create mxd variable
mxd = arcpy.mapping.MapDocument(r"U:\Burn Units\20120531_BurnUnits_MUR.mxd")

# Loop thru layers in mxd
for lyr in arcpy.mapping.ListLayers(mxd):
    try:
        print "Layer Name: " + str(lyr)
        datasource = lyr.dataSource
        print "Original Source: " + str(datasource)
        pathlen = len(datasource)
        print "Path Length: " + str(pathlen)
        if datasource[0:13] == "U:\gis_public":
            fileloc = datasource[13:pathlen]
            print "Path location: " + str(fileloc)
            newpath = "J:\gis_public" + fileloc
            print "New file path: " + newpath + "\n"
            newfullpath = "r" + "\"" + newpath + "\""
            arcpy.mapping.replaceDataSource(newfullpath,"FILEGDB_WORKSPACE") #this line may not be working
            del newpath, fileloc, datasource, pathlen
        else:
            print "Not in gis_public\n"
        
    except:
        print "Could not access dataSource."
        print "This could be due to either 1/ being a group layer, or 2/ being an IWS layer.\n"

    del lyr
    
mxd.save()


The code runs well, generates the new file path, and prints to screen so I can verify, but the replaceDataSource line is not working.

Any thoughts?

Outcomes