0

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."

enter print mxds

Sceen shot of the print commands used

enter image description here

1 Answer 1

2

There is a lot going on here, i hope this answer gives you a start

1. Your function doesn't return anything! Let's take out some unnecessary stuff, change it to return a list of paths for each mxd file (we'll save arcpy stuff for later).

def ListMXDSources(path,extension):
    MapList = []
    list_dir = os.listdir(path)
    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) 

2. You aren't storing the result of your function call in a variable! let's fix that.

 #-------------Use below to call the function----------------#
 mxdList = ListMXDSources(path=r"\\skunk\GIS\Projects\sade\CIP\Location_Maps", 
 extension=".mxd")

... and print out the length of that list rather than the length of the path string

#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." 

3. for mxds in ListMXDSources: is attempting to iterate over the function object itself. This is the proximal cause of your error message Instead, iterate over the object returned by the function ( a list that we've already stored as mxdList) Also put the try statement inside the loop, if you want to try every mxd (otherwise the whole loop will stop at the first erro) :

for mxds in mxdList:
    #set the mxd to the workspace with the mxd files
    try:
        mxd = arcpy.mapping.MapDocument(mxds)
        ....
    except:
       ...
5
  • Thanks a lot Nick! I have effected the corrections you gave here. It made a lot of difference. the script runs now and returns no errors but it does not replace the datasource. I am pasting my updated script here for your review please
    – Emmy
    Commented Oct 23, 2019 at 3:52
  • Without being able to see your data sources I'm not sure how much help I can provide.
    – Nick
    Commented Oct 23, 2019 at 14:56
  • I would approach this by using a debugger (or simple print statements) to inspect the values of the various variables and attempt to understand how your code is running. Does mxdList, for example, have a complete list of the MXD paths you want to edit? Does dataSourceCheck actually contain a valid workspace path (i'm skeptical of the lry.datasource[:-8] ... maybe use lyr.workspacePath)? Even so, if the data sources are in a subdirectory of ~\Rolls\ you may have edit the if dataSourceCheck == oldPath: conditional. Place a print in that conditional block and see if your code ever gets there.
    – Nick
    Commented Oct 23, 2019 at 15:13
  • Finally, are you sure you want to list mxds located on skunk? The data moved to tesclone but mxds stayed on skunk?
    – Nick
    Commented Oct 23, 2019 at 15:15
  • Hi Nick, I added a 'Print mxds' at the end of the "dataSourceCheck == oldPath: con ditional statement, the script was able to print our the list and directory path of all the mxds in that directory telling me that it sees the files in there. i am reposting the edited script and the output above
    – Emmy
    Commented Oct 28, 2019 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.