Select to view content in your preferred language

shapefiles had been renamed

1862
8
Jump to solution
01-12-2018 01:28 AM
jeromeazul
Emerging Contributor

Howdy!

I had a bunch of shp files inputted in my mxds. and we renamed these shp files and the link in my mxds got broken. The easiest way is to open the mxd, set the data source and click the renamed shp files or use the arc catalog. However, I need to do the fixing of broken links to 700 mxds., this can be overwhelming. I am trying to find a python code/script that can be used in order to fix these broken links but unfortunately, I can't find any that works. Can anyone help me with this? It will be greatly appreciated if anyone can give me a script for this. 

Ps: I tried using this script from ArcGIS Help 10.1 but it is not working 

import arcpymxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")for lyr in arcpy.mapping.ListBrokenDataSources(mxd):    if lyr.supports("DATASOURCE"):        if lyr.dataSource == r"C:\Project\Data\Transportation.gdb\MajorRoads":            lyr.replaceDataSource(r"C:\Project\Data\Transportation.gdb", "FILEGDB_WORKSPACE", "Highways")            lyr.name = "Highways"mxd.saveACopy(r"C:\Project\Project2.mxd")del mxd

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

If it is a simple rename all "earthquake_PHI" to "earthquake_PHL", then this may give you an idea.  If it is a bunch of renames, you may need to use a dictionary in the renaming. 

import arcpy
import os
from arcpy import env

# path to search for mxd
path = r'C:\Path\To\Use"

oldShp = "earthquake_PHI"  # old shapefile name without extension
newShp = "earthquake_PHL"  # for replaceDataSource - does not use ".shp"

# iterates through folder searching for mxds
for fileName in os.listdir(path):
    fullPath = os.path.join(path,fileName)
    if os.path.isfile(fullPath) and fileName[-3:].lower() == 'mxd':
        mxd = arcpy.mapping.MapDocument(fullPath)
        print fullPath
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.supports("DATASOURCE"):
                if oldShp in lyr.dataSource:
                    print "\t" + lyr.dataSource
                    shpPath = os.path.dirname(lyr.dataSource)
                    print "\t" + shpPath
                    shpFile = os.path.basename(lyr.dataSource)
                    print "\t" + shpFile
                    # this will replace the data source in the mxd
                    lyr.replaceDataSource(shpPath, "SHAPEFILE_WORKSPACE", newShp)
                    # if layer needs to be renamed
                    lyr.name = newShp 
        mxd.save() # save mxd changes, perhaps use:  mxd.saveACopy(r"C:\Project\Output\\" + newname + ".mxd")

del mxd

View solution in original post

8 Replies
XanderBakker
Esri Esteemed Contributor

Do you have version 10.1? What is not working, any error messages? You will probably have to create a loop through the MXD's and specify what the old and new location and names of the shapefile are. 

jeromeazul
Emerging Contributor

Hello Xander, 

Thanks for your reply.

Yes, i run the code in version 10.1 and even tried it in version 10.3.

We modify the code by specifying the old and new locations and names of the shapefiles and we run the code. 

The code works fine and there are no error messages, however, when we check the mxds, links are still broken.

0 Kudos
DanPatterson_Retired
MVP Emeritus

That code won't work without modification since you have to provide the paths to the projects (mxd's) and their data if the location of the data are not 'relative' to the projects.  Renaming them along the way is going to further complicate things.  Do you have some sort of 'old-new' name key that can facilitate the move?

If the number of projects to fix greatly outnumbers the number of shapefiles you renamed, then it may be best to backtrack and rename them back to their original names, until you can get the rest of this resolved.

It would be discouraging that renaming 3 shapefiles ruined 700 projects.

jeromeazul
Emerging Contributor

Hello Dan, 

Thanks for this.

 

this is our situation, we have a shp file named "earthquake_PHI.shp", and we renamed it manually to "earthquake_PHL.shp". This shapefile was used in over 700 mxds. we checked these mxds, and the links got broken. We wanted to find a way on how to set the data source again but with the newly renamed shpfile. 

 

By the way, we still need to rename 70 shapefiles.

Thank you very much! 

0 Kudos
RandyBurton
MVP Alum

As Dan Patterson‌ mentioned, with whatever script you find, you will need to some modifications.  It sounds like you will want to start with a loop to find all your MXDs, and then loop through the feature layers in each file.  Here's a code example that might be of interest: Write Broken Source List to Text File.

Can you explain a bit more about your situation.  I am assuming that you are working with shape files (ending in .shp) and not a feature in a geodatabase.  Were the shape files moved, renamed or both?  If they were renamed, was there a logic/process to the renaming that can be written with code?  If the files were moved, again, was there specific process used?

jeromeazul
Emerging Contributor

Hello Randy,

Thanks for this.

Yes,  we are working with shp files and not with features in a geodatabase. 

We're here in this situation where we have a shp file named "earthquake_PHI.shp", and we renamed it to "earthquake_PHL.shp". This shapefile was used in over 700 mxds. we checked these mxds, and the links got broken. We wanted to find a way on how to set the data source again but with the newly renamed shpfile. 

"Was there a process to the renaming that can be code?" We just renamed the shp and its extensions (.dbf, .shx, etc) manually inside a folder. 

We have another situation where we didn't rename the shpfile ("earthquake_PHI.shp") anymore, instead we 'save as' another file called  "earthquake_PHL.shp". In this case, we have now 2 shapefiles. Checking the mxds, links are not broken but the layer in mxd still has the old shapefile as its source, but we wanted to change it to the new shapefile by setting the data source. Is this possible with a script to set the data source in 700 mxds?

Thank you very much! 

0 Kudos
RandyBurton
MVP Alum

If it is a simple rename all "earthquake_PHI" to "earthquake_PHL", then this may give you an idea.  If it is a bunch of renames, you may need to use a dictionary in the renaming. 

import arcpy
import os
from arcpy import env

# path to search for mxd
path = r'C:\Path\To\Use"

oldShp = "earthquake_PHI"  # old shapefile name without extension
newShp = "earthquake_PHL"  # for replaceDataSource - does not use ".shp"

# iterates through folder searching for mxds
for fileName in os.listdir(path):
    fullPath = os.path.join(path,fileName)
    if os.path.isfile(fullPath) and fileName[-3:].lower() == 'mxd':
        mxd = arcpy.mapping.MapDocument(fullPath)
        print fullPath
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.supports("DATASOURCE"):
                if oldShp in lyr.dataSource:
                    print "\t" + lyr.dataSource
                    shpPath = os.path.dirname(lyr.dataSource)
                    print "\t" + shpPath
                    shpFile = os.path.basename(lyr.dataSource)
                    print "\t" + shpFile
                    # this will replace the data source in the mxd
                    lyr.replaceDataSource(shpPath, "SHAPEFILE_WORKSPACE", newShp)
                    # if layer needs to be renamed
                    lyr.name = newShp 
        mxd.save() # save mxd changes, perhaps use:  mxd.saveACopy(r"C:\Project\Output\\" + newname + ".mxd")

del mxd
jeromeazul
Emerging Contributor

Hello Randy,

Just tried this script and it worked! Thank you very much.

I also made a dictionary for renaming a bunch of shapefiles as you recommended. Thank you!

Jerome

0 Kudos