I recently replaced my existing data server with a new server thus a new server name and path. My existing data server is named skunk while my new server is tesclon. I need to reroute all the datasources in my over 300 mxd files that points to skunk to now point to Tesclone.
The script has been modified and some print statements were used to capture what is going on. It is able to access the new data path and the old datapath, it is also able to list the mxd files and datasource path but it seems not able to execute the "find and replace datasource"
So i really need help at this juncture.
#Tell the script which data path needs to be updated
oldPath = r"\\skunk\GIS\Projects\Irene"
#Tell the script what the new data path is
newDataPath = r"\\isilonc1\dept\tnr\skunk\GIS\projects\Irene"
#### Stop editing the code here ####
#use arcpy to list all of the mxd files
def ListMXDSources(path,extension):
list_dir = []
CountList = []
MapList = []
list_dir = os.listdir(path)
count = 0
for paths, dirctory, files in os.walk(path):
for file in files:
if file.endswith(extension): # eg: '.mxd'
MapList.append(os.path.join(paths, file))
return(MapList)
print MapList
for m in MapList:
count += 1
mxd = arcpy.mapping.MapDocument(m)
#-------------Use below to call the function----------------#
mxdList = ListMXDSources(path=r"\\skunk\GIS\Projects\Irene",extension=".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(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()
print mxds
print dataSourceCheck
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."
script output
Irene,
Your code is displayed on a single line, making it almost unreadable. Please see the link provided to post your code in a formatted manner.
Regards,
Jim
Thanks Jim.
Hello Irene,
I think you are super close. However, your oldPath variable is a UNC path (not mapped) but based on your script output you're comparing this to a mapped (Y) drive path. Arcpy doesn't "know" about mapped drives, unfortunately. So, you will want to compare a string representing the UNC equivalent of that mapped drive path.
Also, this function will apply to a whole MXD, so there's no need to comb through the individual layers within each map:
mxd.findAndReplaceWorkspacePaths(oldPath,newDataPath,False)
I believe that you could just run that method twice on each mxd in your mxd list (once for the UNC oldPath and once for the mapped drive oldPath). This might simplify your process and avoid the risk of line 51 in your code returning a False, which I believe is what's happening currently.
Hope that helps!
Micah