Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

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

$
0
0
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

Code:


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


Viewing all articles
Browse latest Browse all 2485

Trending Articles