How to search for .mxd that contain a specific word within its filename

649
2
03-21-2013 12:21 PM
ElizabethMittelstaedt
New Contributor
Hi,

I'm fairly new to coding with Python. I have to walk through every folder and change the data source of all layers within each map document. But before doing so, I want to make a copy of each .mxd (and add the word 'Copy' to the filename) and then only apply changes to the .mxds that contain 'Copy' in the file name (in order not to alter any original .mxd files). I have successfully walked through every folder and saved a copy of every map but am stuck on how to iterate through all the .mxds that contain 'Copy' in the file name and only change the data source in those mxds.
Thanks, Elizabeth


for subdir, dirs, files in os.walk(rootfolder):
    for filename in files:
        fullPath = os.path.join(subdir, filename)
        if os.path.isfile(fullPath):
            basename, extension = os.path.splitext(fullPath)
            if extension.lower() == ".mxd":
                mxd = arcpy.mapping.MapDocument (fullPath)
                mxd.saveACopy(subdir +"\\Copy_" + filename)

                #This is where I am encountering problems with how to only apply changes to mxd's file names that contain 'Copy'              
                term = "Copy"
                if term in basename:
                    for lyr in arcpy.mapping.ListLayers(mxd):
                        if lyr.supports("DATASOURCE"):
                        # List the layer's data source
                            arcpy.AddMessage(lyr.dataSource)
                else:
                    arcpy.AddMessage("The file is not a copy of the original")
Tags (2)
0 Kudos
2 Replies
ChrisFox3
Occasional Contributor III
This is assuming based on your code that you only need to find MXDs whose first 5 characters of the name is "Copy_":

#This is where I am encountering problems with how to only apply changes to mxd's file names that contain 'Copy'              
#term = "Copy"
#if term in basename:

if basename[0:5] == "Copy_":


Strings are indexable so what I am doing is returning the first 5 characters of the name of the MXD and testing if it is equal to Copy_
0 Kudos
ElizabethMittelstaedt
New Contributor
Thanks alot for your help!
0 Kudos