1

I have little experience in scripting and I'm looking for a more efficient way to replace the source file path of over 1000 shape files.

Is there a way I can do this without manually changing each file path?

As an example: I need to replace the S1 to S2 in the following file path

H:\GQRA screening tool\Shapefiles\S1 SOLID Ali _C10-12 0.5 m

this is the script i'm using, however the bit im struggling with is how to replace a partial string section of the file path.

import arcpy  
... mxd = arcpy.mapping.MapDocument(r"H:\GQRA screening 
tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")  
... for lyr in arcpy.mapping.ListLayers(mxd):  
...     if lyr.supports("DATASOURCE"):  
...         if lyr.dataSource == r"H:\GQRA screening tool\Shapefiles":  
...             lyr.findAndReplaceWorkspacePath(r"S1", r"S2")  
... mxd.saveACopy(r"H:\GQRA screening tool\Arcmap\SwecoTemplate_SI_A3_New")  
... del mxd  

This is the updated script, however still not got it right yet.

import arcpy  
... mxd = arcpy.mapping.MapDocument(r"H:\GQRA screening 
tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")  
... for lyr in arcpy.mapping.ListLayers(mxd):  
...     if lyr.supports("DATASOURCE"):  
...         if r"H:\GQRA screening tool\Shapefiles":  
...             lyr.findAndReplaceWorkspacePath(r"S1", r"S2")  
... mxd.saveACopy(r"H:\GQRA screening tool\Arcmap\SwecoTemplate_SI_A3_New")  
... del mxd
  • At least for QGIS projects of the uncompressed .qgs format can be opened in any proper text editor (e.g Notepad++) and edited. Especially you may use ctrl+H to find and replace relevant passages of text/code. Note on the side: Spaces in filepaths may lead to problems. – Erik Feb 11 at 12:19
  • 3
    This is a simple task with Python/ArcPy, but it requires coding. I would urge you to re-evaluate your directory naming scheme -- spaces in directory paths are a disaster waiting to happen. – Vince Feb 11 at 12:23
  • Thanks, not working with my own data hence the spaces, i can can rid of these in the next iteration of the data model. – Corky Feb 11 at 12:36
  • 3
    lyr.dataSource is the full path to a feature class (not a folder). I think your test should be more like if r"H:\GQRA screening tool\Shapefiles" in lyr.dataSource: – Andy Feb 11 at 14:06
  • 2
    @Andy has nailed it, dataSource is the full path so your test is failing. – Hornbydd Feb 11 at 14:30
1

Try this (untested):

import arcpy  
mxd = arcpy.mapping.MapDocument(r"H:\GQRA screening tool\Arcmap\SwecoTemplate_SI_A3_S2.mxd")  
for lyr in arcpy.mapping.ListLayers(mxd):  
    if lyr.supports("DATASOURCE"):  
        if r"h:\gqra screening tool\shapefiles" in lyr.dataSource.lower():  
            lyr.findAndReplaceWorkspacePath(r"S1", r"S2")  
mxd.saveACopy(r"H:\GQRA screening tool\Arcmap\SwecoTemplate_SI_A3_New")  
del mxd

You may find that you need to modify or even simplify the test if xxx in lyr.dataSource. One common way you would modify it is to account for differences in case by converting both sides to lowercase, as I've done here. As @Vince noted above, spaces in the file paths could be troublesome. I'd probably look to test for only "GQRA," or somthing like that, if I could get away with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.