Python script to export file paths of all features in mxd into text document

16032
10
01-07-2011 03:29 PM
by Anonymous User
Not applicable
Hey guys,
I'm currently running 9.3.1, but will soon be switching over to 10. I've seen how a Python shell is integrated into ArcMap now and the new arcpy.module (FYI-I'm a beginner at Python); My essential goal, given many project folders with numerous shapes/features both useful and useless, is to use a script to retrieve file paths of files used within my .mxd's and exporting that list to a text file. Ultimately I'd like to batch the process for all .mxd's within the project folder so I can single out unused data and delete, while also gathering all the data into a central GDB.

Any help? Thanks!
0 Kudos
10 Replies
JeffBarrette
Esri Regular Contributor
The following is a link to some sample scripts on the Resource Center:

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=A910AB18-1422-2418-3418-3885D...

It contains one script called Multi-MXD report.  It writes a bunch of MXD information to a text file (including file path).  This should help you get going.

Jeff
0 Kudos
V_StuartFoote
MVP Frequent Contributor
Oops...

Looks to have been the wrong link pasted in.

But, you can find the script Jeffrey referred to in the "20 script tools" from this Getting started with Python Map Automation post to the ArcGIS Desktop BLOG.

Here is just the MultiMXDReport.py Python script extracted:

# Author:  ESRI
# Date:    July 5, 2010
# Version: ArcGIS 10.0
# Purpose: This script will iterate through each MXD in a folder and report information about each
#          map document, it's data frames and layers.  The script is intended to run from a script
#          tool that requires two input parameters:
#               1) folder containing MXDs,
#               2) an output text file.
#
#          The resulting text file will automatically open.

import arcpy, datetime, os

try:

    arcpy.gp.overwriteOutput = True

    #Read input parameters from GP dialog
    folderPath = arcpy.GetParameterAsText(0)
    output = arcpy.GetParameterAsText(1)

    #Create an output file
    outFile = open(output, "w")

    #Report header
    outFile.write("MXD REPORT: \n")
    outFile.write("\n")
    outFile.write("This report is for all MXDs in a folder.  It lists relevant information about\n")
    outFile.write("map document properties, data frame, layer, and table information for each MXD\n")
    outFile.write("in a system folder\n")
    outFile.write("\n")
    outFile.write("Date: " + str(datetime.datetime.today().strftime("%B %d, %Y")) + "\n")

    #Loop through each MXD file
    count = 0
    for filename in os.listdir(folderPath):
        fullpath = os.path.join(folderPath, filename)
        if os.path.isfile(fullpath):
            if filename.lower().endswith(".mxd"):

                #Reference MXD
                mxd = arcpy.mapping.MapDocument(fullpath)
                count = 1

                #Format output value
                if mxd.author =="": authorValue = "None"
                else: authorValue = mxd.author
                if mxd.summary =="": summaryValue = "None"
                else: summaryValue = mxd.summary
                BDS = arcpy.mapping.ListBrokenDataSources(mxd)
                if len(BDS) == 0: BDSValue = "None"
                else: BDSValue = "A total of " + str(len(BDS)) + " broken data source(s)."
               
                #Write MXD data to file
                outFile.write("\n")
                outFile.write("\n")
                outFile.write("------------------------------------------------------------------- \n")
                outFile.write("MAPDOCUMENT: " + os.path.basename(mxd.filePath) + "\n")
                outFile.write("------------------------------------------------------------------- \n")
                outFile.write("\n")
                outFile.write("\t Path:                 " + mxd.filePath + "\n")
                outFile.write("\t Last Saved:           " + str(mxd.dateSaved) + "\n")
                outFile.write("\t Author:               " + authorValue + "\n")
                outFile.write("\t Summary:              " + summaryValue + "\n")
                outFile.write("\t Relative Paths:       " + str(mxd.relativePaths) + "\n")
                outFile.write("\t Broken Data Sources:  " + BDSValue + "\n")

                #Reference each data frame and report data
                DFList = arcpy.mapping.ListDataFrames(mxd)
                for df in DFList:
                    #Format output values
                    if df.description == "": descValue = "None"
                    else: descValue = df.description

                    #Write data frame data to file
                    outFile.write("\n")
                    outFile.write("\n")
                    outFile.write("\t DATA FRAME: " + df.name + "\n")
                    outFile.write("\n")
                    outFile.write("\t\t Description:        " + descValue + "\n")
                    outFile.write("\t\t Spatial Reference:  " + df.spatialReference.name + "\n")
                    outFile.write("\t\t Transformation(s):  " + str(df.geographicTransformations) + "\n")
                    outFile.write("\t\t Map Units:          " + df.mapUnits + "\n")
                    try:
                        outFile.write("\t\t Scale:              " + str(df.scale) + "\n")
                    except:
                        outFile.write("\t\t Scale:              Unknown \n")
                    outFile.write("\t\t Rotation:           " + str(df.rotation) + "\n")
                 
                    #Reference each layer in a data frame
                    lyrList = arcpy.mapping.ListLayers(mxd, "", df)
                    for lyr in lyrList:
                        outFile.write("\n")
                        outFile.write("\t\t LAYER: " + lyr.name + "\n")
                        outFile.write("\t\t\t Group Layer Path:  " + lyr.longName + "\n")
                        if lyr.supports("dataSource"):
                            outFile.write("\t\t\t Data Source:       " + lyr.dataSource + "\n")
                            try:
                                outFile.write("\t\t\t Dataset type:      " + arcpy.Describe(lyr.dataSource).datasettype + "\n")
                            except:
                                outFile.write("\t\t\t Dataset type:     Unknown (could be a broken data source) \n")
                        else: outFile.write("\t\t\t Data Source:       N/A \n")
                        if lyr.supports("definitionQuery"):
                            if lyr.definitionQuery == "":
                                outFile.write("\t\t\t Query Definition:  None \n" )
                            else: outFile.write("\t\t\t Query Definition:  " + lyr.definitionQuery + "\n")
                        else: outFile.write("\t\t\t Query Definition:  N/A \n")
                       
                    #Reference each table in a data frame
                    tableList = arcpy.mapping.ListTableViews(mxd, df, "")
                    for table in tableList:
                        outFile.write("\n")
                        outFile.write("\n")
                        outFile.write("\t\t TABLEVIEW: " + table.name + "\n")
                        outFile.write("\n")
                        outFile.write("\t\t\t Data Source:           " + table.dataSource + "\n")
                        if table.definitionQuery == "":
                            outFile.write("\t\t\t Query Definition:      None \n")
                        else: outFile.write("\t\t\t Query Definition:      " + table.definitionQuery + "\n")

                del mxd
           
    if count ==0:
        outFile.write("\n")
        outFile.write("\n")
        outFile.write("---------------------------------------------------------------------------------- \n")
        outFile.write("                            NO MXD FILES FOUND \n")
        outFile.write("---------------------------------------------------------------------------------- \n")                         

    outFile.close()

    #Open resulting text file
    os.startfile(output)

    #Delete variables that reference data on disk
    del folderPath, output, outFile, fullpath

except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))
  arcpy.AddError(str(e))
JeffBarrette
Esri Regular Contributor
0 Kudos
KevinHighland
Occasional Contributor II
I'm very, VERY new to geoprocessing with python, so go easy on me.

Just a question- Does anyone know what can I add to the Multi MXD Report script (posted by vsfoote above) to make the "Read input parameters" include all subfolders of the specified folder path?
0 Kudos
JeffBarrette
Esri Regular Contributor
Great question,  try this:

import arcpy, os
parentDir = r"c:\some\path"
for (path, dirs, files) in os.walk(parentDir):
>>for file in files:
>>>>if file.lower().endswith(".mxd"):
>>>>>>mxd = arcpy.mapping.MapDocument(os.path.join(path, file))
>>>>>>print mxd.filePath

Get rid of the ">>" symbols.

Hope this helps,
Jeff
0 Kudos
TimShields
New Contributor
Great question,  try this:

import arcpy, os
parentDir = r"c:\some\path"
for (path, dirs, files) in os.walk(parentDir):
>>for file in files:
>>>>if file.lower().endswith(".mxd"):
>>>>>>mxd = arcpy.mapping.MapDocument(os.path.join(path, file))
>>>>>>print mxd.filePath

Get rid of the ">>" symbols.

Hope this helps,
Jeff


I have been trying to add this to the code because I want to have the script search folders within a folder as well. Unfortunately I have been unsuccessful in making this happen. Where exactly do I need to put this in the code for it to work?

import arcpy, datetime, os

try:

    arcpy.gp.overwriteOutput = True

    #Read input parameters from GP dialog
             
    folderPath = arcpy.GetParameterAsText(0)        
    output = arcpy.GetParameterAsText(1)

    #Create an output file
    outFile = open(output, "w")

    #Report header
    outFile.write("MXD REPORT: \n")
    outFile.write("\n")
    outFile.write("This report is for all MXDs in a folder.  It lists relevant information about\n")
    outFile.write("map document properties, data frame, layer, and table information for each MXD\n")
    outFile.write("in a system folder\n")
    outFile.write("\n")
    outFile.write("Date: " + str(datetime.datetime.today().strftime("%B %d, %Y")) + "\n")

    #Loop through each MXD file
    count = 0
    for filename in os.listdir(folderPath):
        fullpath = os.path.join(folderPath, filename)
        if os.path.isfile(fullpath):
            if filename.lower().endswith(".mxd"):

                #Reference MXD
                mxd = arcpy.mapping.MapDocument(fullpath)
                count = 1

                #Format output value
                if mxd.author =="": authorValue = "None"
                else: authorValue = mxd.author
                if mxd.summary =="": summaryValue = "None"
                else: summaryValue = mxd.summary
                BDS = arcpy.mapping.ListBrokenDataSources(mxd)
                if len(BDS) == 0: BDSValue = "None"
                else: BDSValue = "A total of " + str(len(BDS)) + " broken data source(s)."
               
                #Write MXD data to file
                outFile.write("\n")
                outFile.write("\n")
                outFile.write("------------------------------------------------------------------- \n")
                outFile.write("MAPDOCUMENT: " + os.path.basename(mxd.filePath) + "\n")
                outFile.write("------------------------------------------------------------------- \n")
                outFile.write("\n")
                outFile.write("\t Path:                 " + mxd.filePath + "\n")
                outFile.write("\t Last Saved:           " + str(mxd.dateSaved) + "\n")
                outFile.write("\t Author:               " + authorValue + "\n")
                outFile.write("\t Summary:              " + summaryValue + "\n")
                outFile.write("\t Relative Paths:       " + str(mxd.relativePaths) + "\n")
                outFile.write("\t Broken Data Sources:  " + BDSValue + "\n")

                #Reference each data frame and report data
                DFList = arcpy.mapping.ListDataFrames(mxd)
                for df in DFList:
                    #Format output values
                    if df.description == "": descValue = "None"
                    else: descValue = df.description

                    #Write data frame data to file
                    outFile.write("\n")
                    outFile.write("\n")
                    outFile.write("\t DATA FRAME: " + df.name + "\n")
                    outFile.write("\n")
                    outFile.write("\t\t Description:        " + descValue + "\n")
                    outFile.write("\t\t Spatial Reference:  " + df.spatialReference.name + "\n")
                    outFile.write("\t\t Transformation(s):  " + str(df.geographicTransformations) + "\n")
                    outFile.write("\t\t Map Units:          " + df.mapUnits + "\n")
                    try:
                        outFile.write("\t\t Scale:              " + str(df.scale) + "\n")
                    except:
                        outFile.write("\t\t Scale:              Unknown \n")
                    outFile.write("\t\t Rotation:           " + str(df.rotation) + "\n")
                 
                    #Reference each layer in a data frame
                    lyrList = arcpy.mapping.ListLayers(mxd, "", df)
                    for lyr in lyrList:
                        outFile.write("\n")
                        outFile.write("\t\t LAYER: " + lyr.name + "\n")
                        outFile.write("\t\t\t Group Layer Path:  " + lyr.longName + "\n")
                        if lyr.supports("dataSource"):
                            outFile.write("\t\t\t Data Source:       " + lyr.dataSource + "\n")
                            try:
                                outFile.write("\t\t\t Dataset type:      " + arcpy.Describe(lyr.dataSource).datasettype + "\n")
                            except:
                                outFile.write("\t\t\t Dataset type:     Unknown (could be a broken data source) \n")
                        else: outFile.write("\t\t\t Data Source:       N/A \n")
                        if lyr.supports("definitionQuery"):
                            if lyr.definitionQuery == "":
                                outFile.write("\t\t\t Query Definition:  None \n" )
                            else: outFile.write("\t\t\t Query Definition:  " + lyr.definitionQuery + "\n")
                        else: outFile.write("\t\t\t Query Definition:  N/A \n")
                       
                    #Reference each table in a data frame
                    tableList = arcpy.mapping.ListTableViews(mxd, df, "")
                    for table in tableList:
                        outFile.write("\n")
                        outFile.write("\n")
                        outFile.write("\t\t TABLEVIEW: " + table.name + "\n")
                        outFile.write("\n")
                        outFile.write("\t\t\t Data Source:           " + table.dataSource + "\n")
                        if table.definitionQuery == "":
                            outFile.write("\t\t\t Query Definition:      None \n")
                        else: outFile.write("\t\t\t Query Definition:      " + table.definitionQuery + "\n")

                del mxd
           
    if count ==0:
        outFile.write("\n")
        outFile.write("\n")
        outFile.write("---------------------------------------------------------------------------------- \n")
        outFile.write("                            NO MXD FILES FOUND \n")
        outFile.write("---------------------------------------------------------------------------------- \n")                         

    outFile.close()

    #Open resulting text file
    os.startfile(output)

    #Delete variables that reference data on disk
    del folderPath, output, outFile, fullpath

except Exception, e:
  import traceback
  map(arcpy.AddError, traceback.format_exc().split("\n"))
  arcpy.AddError(str(e))

Thanks for your help,

Tim
0 Kudos
ZhengHuang
New Contributor
I know this is an old thread but I was just wondering how this went for you.

ZYH
0 Kudos
jonathantuscanes
New Contributor II
bump
where do you add the extra code to search sub folders?
0 Kudos
RyanPaquette
Occasional Contributor
Newbie to arcpy / having some issues...

I tried this in both 10.1 and 10.0 SP4.

I added the script as a tool, added the parameters to the tool (Input Folder) & (Output Text File).

When I run the tool, the input script file opens in Notepad while the tool executes. Although it is seemingly doing nothing at the time.

If I close the text file that was auto-opened (the tool's input script file) then the tool stops as "completed" although no output file is generated. If I do nothing (don't close the text file) the script runs forever (at least for an hour...when I gave up)...
0 Kudos