Changing Data Source for numerous .MXD's

15969
8
Jump to solution
07-02-2014 08:34 AM
ReubenArvin
New Contributor
Hello,

I have 92 different Map Documents that all have the same two layers. I would like to update these 2 layers with a different data source and do this for all 92 Map Documents uniformly.

Any thoughts on how to go about this?

Thank you!
0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor
This can be done fairly easily using python and the arcpy.mapping module.  For the layers, there is a method to replace the data source with a new file path, or can be done for the entire map document, say if the data sources for all layers have changed folders or something similar.

Check out this link

Hope this helps

View solution in original post

0 Kudos
8 Replies
IanMurray
Frequent Contributor
This can be done fairly easily using python and the arcpy.mapping module.  For the layers, there is a method to replace the data source with a new file path, or can be done for the entire map document, say if the data sources for all layers have changed folders or something similar.

Check out this link

Hope this helps
0 Kudos
ReubenArvin
New Contributor
I figured it was too difficult just didn't know the module to use. I will check it out

Thanks for the help
0 Kudos
ReubenArvin
New Contributor
That link was helpful especially the findAndReplaceWorkspacePaths, except the example was for 1 Map Document. I have about 92 that I would like to do it for.

Thanks
0 Kudos
IanMurray
Frequent Contributor
If your maps are in the same folder, then there are easy ways to iterate through all your map documents in a folder.  If you set your workspace to the folder they are in, then use the arcpy.ListFiles("*.mxd"), you will make a list of all the map documents in the folder.  You can then loop through them and put in the code for findandreplaceworkspace.

If you have multiple workspaces, you can loop through workspaces as well with ListWorkspaces.

Check out this link for help with ListFiles, there is also a link to ListWorkspaces in there.

If you have any further questions about this, feel free to ask.
ReubenArvin
New Contributor
It makes sense as far as the process but I'm not sure how to write the code exactly.

Could you write an sample code to get me started?

1. Setting workspace for the folder that contains all the MXDs
2. Finding and replacing the same layer in each MXD with an updated layer (setting data source to a different shapefile and in all the MXDs)


If that makes sense.

Thank you
0 Kudos
IanMurray
Frequent Contributor
sure thing

#Importing Modules
import arcpy
from arcpy import env

#Put file path for the folder containing mxds, where I have my sample file path
env.workspace = r"C:\Test\MXDfolder"

#Put file path for the folder, gdb, feature dataset, or sde connection containing the files your map layers will be referencing here, where I have my sample file path
replace_workspace_path  = r"C:\Test\NewWorkspace"

#Looping through all filepaths in our workspace, make a map document object out of them, then replace their workspace path
for file in arcpy.ListFiles("*.mxd"):
  mxd = arcpy.mapping.MapDocument(file)
  mxd.findAndReplaceWorkspacePaths(" " , replace_workspace_path)
  mxd.save()

del file; del mxd



So the script makes a list of the filepaths for each mxd, then loops through them, creates a python map document object, which we then call the findandReplaceWorkspacePaths method on to replace the workspace path for all layers in the map documents.

I haven't tested this on anything, and there could be a few errors to debug, but it should be enough to get started.  Always feel free to search the arcgis help for any method that you have questions about.
ReubenArvin
New Contributor
I haven't tried it yet but I guess what I'm trying to do is change the dataset for a layer but do it for every MXD's. Does that make sense. Maybe you already knew that...

Either way I'm looking at your code.

Thanks
0 Kudos
IanMurray
Frequent Contributor
I haven't tried it yet but I guess what I'm trying to do is change the dataset for a layer but do it for every MXD's. Does that make sense. Maybe you already knew that...

Either way I'm looking at your code.

Thanks


Thats what the code is designed to do.  The only changes that need to be made are setting the workspace and the workspace path for the new dataset for your layers.  It makes a list of all mxds in the workspace, then goes in and changes the workspace paths for all layers in the map document to the workspace path of your new dataset.