# 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))
Solved! Go to Solution.
if filename.lower().endswith(".mxd"): ... arcpy.AddMessage("Finished replacing " + filename) mxd.save()
if filename.lower().endswith(".mxd"): ... arcpy.AddMessage("Finished replacing " + filename) mxd.save()
It definitely looks like you have your mxd.save() outside of your loop. I would have it as the last line under your if statement to verify your file is an mxd.if filename.lower().endswith(".mxd"): ... arcpy.AddMessage("Finished replacing " + filename) mxd.save()
for lyr in lyrs: if lyr.isFeatureLayer and 'sde' in lyr.dataSource: #added the 'sde' code to proceed on sde sources
Paul:
In your last post you stated:
"But there are also a lot of definition queries and symbology that will require additional changes in the mxd's themselves."
Can you please explain what additional work would need to be done with definition queries and symbology when moving to a new geodatabase?