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 tesclone
.
I need to change all the datasources in my over 400 MXD files that currently point to skunk
so that they point to tesclone
.
The script has been modified using input and suggestions from members on this forum and now the script runs without error but unfortunately it still does not change the datasource. I have tried using the print command to check if the scripts is able to access both the new datapath and the old datapath as well as the layer sources, all these were done successfully.
But still i do not understand why the line "mxd.findAndReplaceWorkspacePaths(oldPath,newDataPath,False)" is not working
Modified script is shown below and the new generated output as well
#import the necessary python modules
import arcpy,os,csv
#setting folderpath
folderPath = r"./"
#setting environment variables
ws = arcpy.env.workspace = folderPath
arcpy.env.overwriteOutput = True
#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
#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."