2

I have recently renamed 20 feature layers that are currently saved in approximately 100 MXDs. To fix the broken data source as a result of changing the name, I am using the following code:

import arcpy
mxd = arcpy.mapping.MapDocument(r"//test.mxd") #file path for mxd
  for lyr in arcpy.mapping.ListLayers(mxd)
   if str(lyr.name)=="original":
     lyr.replaceDataSource(r"//test.gdb", "FILEGDB_WORKSPACE", "new") #file path for geodatabase
print("Success")
mxd.save()
del mxd

However, this code means that I have to update one MXD at a time.

Is there a way I can add into this code that I would like it to check multiple MXDs for the same layers and replace with the same new data name?

1
  • BERA has already provided the framework. Your code just needs to be indented inside it. I would recommend creating a .bak copy of the previous document before saving over it.
    – Vince
    Oct 28, 2021 at 13:11

1 Answer 1

3

List the mxd:s using os.walk. I have not tried this with your code, test it on one mxd copy first.

This will list all mxd files in mxdfolder, including any subfolders:

import os, arcpy

mxdfolder = r'C:\your_folder'

for root, folder, files in os.walk(mxdfolder):
    for file in files:
        fullname = os.path.join(root, file)
        if os.path.isfile(fullname) and file.endswith('.mxd'):
            print fullname
            #do something with fullname (which is a full path to a mxd document)
            mxd = fullname
            #The rest of your code goes here
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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