Get started by downloading the python script here:
http://sandbox.idre.ucla.edu/tools/python/mxd_converter_AKochaphum.py
The “MXD Data Source Replacer” python script is used for changing the location of data for ArcGIS mxd files. For new users of python the only line which you need to be concerned about is lines 14 and 17.
Line 14 is the old data source path which you would like to change, while Line 17 is the new data path that you would like to replace it with. It is crucial for the code to work that you use two backslashes (e.g.\) in order to identify the paths; the reason for this oddity is that one backslash (e.g.) triggers an escape action in python, so having two of them treats the backslashes as one.
#import the necessary python modules import arcpy,os #setting folderpath folderPath = r"./" #setting environment variables ws = arcpy.env.workspace = folderPath arcpy.env.overwriteOutput = True #### Start editing the code here: Setting the paths #### #Note: Be sure to use two backslashes (eg. \) in order to change directories #Tell the script which data path needs to be updated oldPath = "T:\Users\akocha\python\data\" #Tell the script what the new data path is newDataPath = "C:\akocha\" #### Stop editing the code here #### #use arcpy to list all of the mxd files mxdList = arcpy.ListFiles("*.mxd") #print the total number of mxd files if len(mxdList) == 0: print "There are no mxd files found in this directory." if len(mxdList) == 1: print "There is "+str(len(mxdList))+" mxd file found in this directory." if len(mxdList) > 1: print "There are "+str(len(mxdList))+" mxd files found in this directory." #set the count of the mxds to 0 count = 0 errorCount = 0 #starting the loop to check for each of the mxds try: for mxds in mxdList: #set the mxd to the workspace with the mxd files mxd = arcpy.mapping.MapDocument(ws + mxds) for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): theData = lyr.dataSource dataSourceCheck = lyr.dataSource[:-8] if dataSourceCheck == oldPath: mxd.findAndReplaceWorkspacePaths(oldPath,newDataPath,False) count += 1 mxd.save() del mxd except Exception as e: print "The following error has occured:" print e.message print "Because there was an error, you should try re-running the script." errorCount += 1 successCount = count-errorCount print print "The conversion process has finished and "+str(successCount)+" file(s) have been replaced."
Thanks for this!
I’m getting the following error:
“name ‘dataSourceCheck’ is not defined.
I’m very, very new to python, but my hypothesis is that, for layers where if lyr.supports(“DATASOURCE”) is FALSE, there is no dataSourceCheck defined. I have some grouped layers in my MXD, and the MXD attributes a layerID to the “grouping”, even though there is no datasource. So, how can I modify the code to “continue” to loop through the layers, bypassing those where DATASOURCE is false?
Thank you!
Hello Angela! Welcome to the world of Python and it seems like you have a great understanding so far, so keep it up!
I believe your hypothesis to be correct for the most part. However, I do think that the code should be able to handle grouped layers in an individual fashion. I am working to make the code even more streamlined, so hopefully by early January you will see a second version of this code that automatically generates the dataSourceCheck.
For the time being, to include “dataSourceCheck” in the cases where lyr.supports(“DATASOURCE”) is FALSE you can add the following:
#import the necessary python modules
import arcpy,os
#setting folderpath
folderPath = r”./”
#setting environment variables
ws = arcpy.env.workspace = folderPath
arcpy.env.overwriteOutput = True
#### Start editing the code here: Setting the paths ####
#Tell the script which data path needs to be updated
#Note: Be sure to use two backslashes (eg. \) in order to change directories, the backslashes are an escape character
oldPath = “T:\ArcGIS\ArcGIS_V9_Streetmaps-Data-Maps\usa\census”
#Tell the script what the new data path is
newDataPath = “C:\Users\akocha\Desktop\focusfoodoutlet\”
#### Stop editing the code here ####
#use arcpy to list all of the mxd files
mxdList = arcpy.ListFiles(“*.mxd”)
#print the total number of mxd files
if len(mxdList) == 0:
print “There are no mxd files found in this directory.”
if len(mxdList) == 1:
print “There is “+str(len(mxdList))+” mxd file found in this directory.”
if len(mxdList) > 1:
print “There are “+str(len(mxdList))+” mxd files found in this directory.”
#set the count of the mxds to 0
count = 0
errorCount = 0
#starting the loop to check for each of the mxds
try:
for mxds in mxdList:
#set the mxd to the workspace with the mxd files
mxd = arcpy.mapping.MapDocument(ws + mxds)
for lyr in arcpy.mapping.ListLayers(mxd):
if not lyr.isGroupLayer:
if lyr.supports(“DATASOURCE”):
theData = lyr.dataSource
oldPathLength = len(oldPath)
print oldPathLength
namelength = len(lyr.name)
#TO DO: the name length needs to be fixed here…
#For now, just hard code the length that needs to be checked..
dataSourceCheck = lyr.dataSource[:namelength]
print dataSourceCheck
if dataSourceCheck == oldPath:
mxd.findAndReplaceWorkspacePaths(oldPath,newDataPath,False)
count += 1
mxd.save()
del mxd
except Exception as e:
print “The following error has occured:”
print e.message
print “Because there was an error, you should try re-running the script.”
errorCount += 1
successCount = count-errorCount
print
print “The conversion process has finished and “+str(successCount)+” layer(s) have been replaced.”
Hello! This is exactly what I need, thank you! I am getting an error though, and I cannot figure out why!
There is 1 mxd file found in this directory.
The following error has occured:
Invalid MXD filename.
The name of the MXD is “Overview_Map.mxd”.
Thank you!!
Becky
I’m getting the same error as Becky. Any solution to this?
(Sorry, very new to scripting here)
Me three
Try replacing the 2nd to last section with this code:
# starting the loop to check for each of the mxds
try:
for mxds in mxdList:
# set the mxd to the workspace with the mxd files
mxd_path = os.path.join(folderPath,mxds)
mxd = arcpy.mapping.MapDocument(mxd_path)
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports(“DATASOURCE”):
theData = lyr.dataSource
dataSourceCheck = lyr.dataSource[:-65]
if dataSourceCheck == oldPath:
mxd.findAndReplaceWorkspacePaths(oldPath, newDataPath, False)
count += 1
mxd.save()
del mxd
except Exception as e:
print “The following error has occured:”
print e.message
print “Because there was an error, you should try re-running the script.”
errorCount += 1
Is there a way to change the data source of a layer and keeping the symbology the same as the previous layer?
I am able to change the data source but when I open the new mxd the symbology is different then the one I started with. Changing the data source under properties manually keeps the symbology the same. I would like my python script to do the same.