The following script is supposed to find and replace all shapefiles in a mxd (that exist in the folder) to the new SDE data source. No errors when running the script however, when I open the MXD in ArcGIS 10.1SP1 nothing changed. Any suggestions what is wrong?

import arcpy, os, glob

#find all the MXD's in the directory tree
for root, subFolders, files in os.walk(r"C:\Users\lmuhammad\Documents\ArcGIS\SearchandReplace\Testing"):
for filename in files:
    fullpath = os.path.join(root, filename)
    basename, extension = os.path.splitext(fullpath)
    if extension.lower() == ".mxd":
        print "------------------------------"
        print filename
        #open the map document
        MXD = arcpy.mapping.MapDocument(fullpath)
        #get all the layers
        for lyr in arcpy.mapping.ListLayers(MXD):
            #get the source from the layer
            if lyr.supports("datasource"):
                source = r"R:\Shapefiles\Road_Casings\RoadCasing.shp"
                print "%s -> %s" % (lyr, source)
                basename, extension = os.path.splitext(source)
                if extension.lower() == ".sde":
                    #This is the NEW SOURCE that you want to point to
                    datapath = r"Database Connections\Connection to GISD.sde\GISDEV.TRANSPORTATION\GISDEV.PAVEMENTEDGE"
                    #replace the old path wih the new
                    lyr.findAndReplaceDatasource(source, datapath, "NEWROAD", False)
        #save your changes
        del MXD
  • It is obvious that the code is not going to do what you want as it is currently structured. Did you just copy and paste it from somewhere? – blah238 Feb 4 '13 at 16:50
  • Yes I copied and paste it.. no one at ESRI Support or on GIS forums seem to know how to do a basic search and replace of datasources - from a shapefile to a sde_connection with multiple datasets & feature class for multiple mxds in a folder. – user14690 Feb 5 '13 at 20:00
  • Where did you find the original script? – blah238 Feb 5 '13 at 21:27
  • 1
    I found it here - sspinnovations.com/blog/2012/05/07/… – user14690 Feb 6 '13 at 20:19
  • It works for feature classes that are in the same dataset..but not for different datasets. – user14690 Feb 6 '13 at 20:19

I would recommend breaking this up into two functions:

  1. A function to replace the data sources in one MXD
  2. A function that calls the previous function on each MXD in a directory tree

You'll want to test the first function thoroughly by itself before moving on to the second.

I recommend using a Python IDE such as PyScripter to develop, test and debug your scripts.

Please try to work at this yourself and if you run into a wall, edit your question to include the pertinent information such as what you tried (including code!), what you expected to happen, and what error messages or unexpected output you got.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.