AnsweredAssumed Answered

mxd files data source replacement

Question asked by EgbuleI on Oct 21, 2019
Latest reply on Oct 28, 2019 by mbabinski1988

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

Outcomes