Change data source - solution

4091
2
01-20-2015 11:39 AM
Raphael_Augusto_FoscariniFerre
Occasional Contributor

I have in my job several MXDs (more than 500) and each one is different from another.

There is one specific shapefile (project_area_03-jan-2015.shp) that every map uses.

The big problem: Every week, the shapefile changes, and also changes the name of the shapefile:

 

project_area_03-jan-2015 means its the 3rd week of january 2015

project_area_04-jan-2015 means its the 3th week of january 2015, and goes forever.

 

For our actual projects, we changed the way we built the mxds and the shapefiles, so we dont have any problems.

But for this specific old (very old, before 2008), we still have the old architecture of the files.

 

Every week, we make a copy to another folder and open all +500 MXDs, then 'change data source' in that shapefile, to the new one.

 

We tried to use 'MapDocument.findAndReplaceWorkspacePaths' python, but it changes only the path. We need a script to change the entire path from the shapefile.

 

I want some help, that ghanges the shapefiles for me, in all MXDs inside the folder, like;

 

from  d:\projectalpha\project_area_03-jan-2015.shp  to  d:\projectalpha\project_area_04-jan-2015.shp

 

Thanks

0 Kudos
2 Replies
Raphael_Augusto_FoscariniFerre
Occasional Contributor

any help??

0 Kudos
ErinBrimhall
Occasional Contributor II

Hi Raphael,

I believe what you want to use is the replaceDataSource method off of the Layer class.  This method should allow you to change the name of the layer file serving as the datasource for the layer in your MXDs.  Below is some sample code to help you get started.  I added {} bracket placeholders to describe the actual values that should be used.

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):

    if lyr.supports("DATASOURCE"):

        if lyr.dataSource == r"{Full path to old layer file}\{Layer name in layer file}":

            lyr.replaceDataSource(r"{Full path to new layer file}", "NONE", "{Layer name in layer file}")

mxd.save()

Good luck!