AnsweredAssumed Answered

changing sde datasources and saving mxd

Question asked by luapknarf on Nov 9, 2012
Latest reply on Feb 7, 2013 by charlesfox123
Hello - we are migrating to new SDE datasources, and I am trying to make the changes to our mxd's.  This is complicated by new names for the geodatabases.  My issue now seems to be when saving the mxd's.  I have a number of loops, and perhaps the problem is I do not have my indentations correct.  In the end, I am not seeing the changes in the mxds.  Any suggestions are appreciated!

Paul Frank

# Author:  Paul Frank, City of Austin # Date:    November 7, 2012 # Version: ArcGIS 10.0 # Purpose:  This script will iterate through each MXD in a folder and change the SDE connection and dataset name. #           Then it will save the mxd that it has finished changing.  The script is intended to run from a script #           tool that requires a parameter with the folder containing MXDs.  #setting variables import arcpy, os arcpy.env.overwriteOutput = True print arcpy.GetMessages()  #setting variables for new data source and table that lists the old and new dataset names newdatasource = r"C:\Documents and Settings\frankp\Application Data\ESRI\Desktop10.0\ArcCatalog\DataMart.sde" sdechangetable = r"G:\NEIGHBOR PLAN\ArcView\Themes\GIS_Administration\sde_change.dbf"  #Read input parameters from GP dialog #folderPath = arcpy.GetParameterAsText(0) folderPath = r"C:\arcinfo\New Folder"  try:     #Loop through each MXD file     for filename in os.listdir(folderPath):         fullpath = os.path.join(folderPath, filename)         if os.path.isfile(fullpath):             if filename.lower().endswith(".mxd"):                 #Reference MXD                 mxd = arcpy.mapping.MapDocument(fullpath)                 arcpy.AddMessage("Changing " + mxd.filePath)                 print "changing " + mxd.filePath                 #Reference each data frame and report data                 DFList = arcpy.mapping.ListDataFrames(mxd)                 for df in DFList:                     #Reference each layer in a data frame                     lyrs = arcpy.mapping.ListLayers(mxd, "", df)                     for lyr in lyrs:                         if lyr.isFeatureLayer:                             olddataset = lyr.datasetName                             arcpy.AddMessage("Setting " + olddataset)                             print "Setting " + olddataset                             rows, row = None,None                             rows = arcpy.SearchCursor(sdechangetable, "\"OLDSDE\" = " + "'" + olddataset + "'")                             row = rows.next()                             #proceed if a match is found in sde layers list                             if row <> None:                                 newdataset = row.getValue("NEWSDE")                                 arcpy.AddMessage("Changing " + olddataset + " to " + newdatasource + " " + newdataset)                                 print "Changing " + olddataset + " to " + newdatasource + " " + newdataset                                 #attempt to change the datasource.  Datasource may not actually exist even if found in list                                 try:                                     lyr.replaceDataSource(newdatasource, "SDE_WORKSPACE", newdataset)                                     lyr.name = newdataset                                     arcpy.AddMessage(olddataset + " changed!")                                     print olddataset + " changed!"                                 except:                                     arcpy.AddError(arcpy.GetMessages(2))                             #do not proceed because no match was found in sde layers list                             else:                                 arcpy.AddMessage("Can not replace " + lyr.name)                                 print "Can not replace " + lyr.name                         else:                             arcpy.AddMessage("Trying next layer")                             print "trying next layer"     arcpy.AddMessage("Finished replacing " + filename)     mxd.save()     arcpy.AddMessage("Saved " + filename)     print "saved copy of mxd"     del mxd     del row     del rows     del folderPath, fullpath except Exception, e:   import traceback   map(arcpy.AddError, traceback.format_exc().split("\n"))   arcpy.AddError(str(e))

Outcomes