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 Answer 1
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
Explore related questions
See similar questions with these tags.
.bak
copy of the previous document before saving over it.