need to know which mxds are pointing to a certain source path...

3744
5
01-28-2015 09:08 AM
KirstenJohnson
New Contributor

Hello there.

 

We have a vast amount of mxds, and my goal is to search that entire drive and find out which mxds have layers that are referencing a specific source path.  Then I would like to know how to replace that specific source path with the new source path.

 

I would presume that this would require a python script.  All of the scripts I've found so far require that I know the name of the particular mxd in question.  I'm trying to scan all of the mxds in the drive.

 

Thanks,

Kirsten

Tags (1)
Reply
0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor

Here is how to loop through your drive, using Python's os module, to find all the mxds:

import os

for root, dirs, files in os.walk('C:/junk'😞
  
for file in files:
      
if os.path.splitext(file)[1] == '.mxd':
      
print 'It is an MXD!'

...from here.

Reply
0 Kudos
KirstenJohnson
New Contributor

Thanks for that.  So I now how to get a list of mxds, but I need to know which ones have data layers that are pointing to a certain path.  Then I need to replace the paths in those mxds with a new path.

Reply
0 Kudos
DarrenWiens2
MVP Honored Contributor

Refer to the arcpy.mapping help.

Reply
0 Kudos
KirstenJohnson
New Contributor

Oh, thanks for the suggestion to move it to Python.  I've never posted to this forum before, so I was unaware.

Reply
0 Kudos
XanderBakker
Esri Esteemed Contributor

According to the Help you should be able to use arcpy.da.Walk to loop through the mxd files in a folder (and subfolders):

import arcpy

import os

workspace = r"D:\Xander\GeoNet"

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

for dirpath, dirnames, filenames in walk:

    for filename in filenames:

        mxdfile = os.path.join(dirpath, filename)

... but when I tried it didn't give me any result.

However, you can use:

import arcpy

import os

import fnmatch

directory = r"D:\Xander\GeoNet"

pattern = "*.mxd"

for root, dirs, files in os.walk(directory):

    for filename in fnmatch.filter(files, pattern):

        mxdfile = os.path.join(root, filename)

        print "mxd file: {0}".format(mxdfile)

        mxd = arcpy.mapping.MapDocument(mxdfile)

        dfs = arcpy.mapping.ListDataFrames(mxd)

        for df in dfs:

            print " - data frame: {0}".format(df.name)

            for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):

                print "   - layer name: {0}".format(lyr.name)

... to loop through the mxd's, the data frames and layers within the dataframes.

To update any datasource I recommend you to read this page of the Help: ArcGIS Help (10.2, 10.2.1, and 10.2.2)