Select to view content in your preferred language

Populating Map Document Properties under a Folder

531
2
03-15-2014 09:11 AM
benberman
Regular Contributor
I wrote this script that populates some of the properties for MXDs.

mxd = arcpy.mapping.MapDocument(r"C:\test\test1.mxd")
mxd = arcpy.mapping.MapDocument(r"C:\test\test2.mxd")
mxd = arcpy.mapping.MapDocument(r"C:\test\test3.mxd")
mxd.author = "Test GIS Author"
mxd.title = "Test GIS Title"
mxd.credits = "GIS Credits"
mxd.save()
del mxd

However, what I really want to accomplish is for a script to automatically populate the properties for each MXD under C:\test directory without having to declare each one of the paths for the MXDs individually. I tried working with ListFiles and using various arrays, no success yet. Can someone direct me to achieve this task ?
Tags (2)
0 Kudos
2 Replies
T__WayneWhitley
Honored Contributor
I didn't test this, but something like this should work for you:
import arcpy
import os

workspace = r"C:\test"

walk = arcpy.da.Walk(workspace, datatype="Map")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        mxd = arcpy.mapping.MapDocument(os.path.join(dirpath, filename))
        mxd.author = "Test GIS Author"
        mxd.title = "Test GIS Title"
        mxd.credits = "GIS Credits"
        mxd.save()
        del mxd


(modified walk code sample 1 from http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000023000000)
0 Kudos
T__WayneWhitley
Honored Contributor
Hmmm, I revisited this to test further the supposed capabilities of arcpy.da.Walk to traverse both files and feature classes within gdbs, and so on...but at 10.2.1 I couldn't get the datatype keyword 'Map' to recognize and filter for map documents.  But at least in looking for mxds, you can still use os.walk as in the following:
import arcpy
import os

workspace = r"C:\test"

for dirpath, dirnames, filenames in os.walk(workspace):
 for filename in filenames:
  if '.mxd' in filename:
                    mxd = arcpy.mapping.MapDocument(os.path.join(dirpath, filename))
                    mxd.author = "Test GIS Author"
                    mxd.title = "Test GIS Title"
                    mxd.credits = "GIS Credits"
                    mxd.save()
                    del mxd


The os.walk docs are here:
http://docs.python.org/2.6/library/os.html#os.walk

...and if interested further about arcpy.da.Walk and related, see this:
http://arcpy.wordpress.com/tag/os-walk/


Wayne
0 Kudos