2

I have multiple shapefiles which each have the same field and the same value in their attribute tables. However, some of the shapefiles have more than one record with that same value. I need to change that value for each shapefile attribute table.

Is there a way to change all of those values for all of the shapefiles in one step, without having to go into each shapefile's attribute table separately?

  • 1
    Short answer: No. – Vince Apr 14 '16 at 0:47
3

Technically, you have to go into each shapefile separately to update it, but you could do it by running a single a Python program that loops through all the files in a directory and uses an update cursor to update the field in each one. Are your files all in a single directory? What is the name of the shared field? Anyway, the basic approach to the program is as follows, which would be run in a standalone python script:

import arcpy
from arcpy import env

# Set the workspace for the ListFeatureClass function
#
env.workspace = "c:/base"

# Use the ListFeatureClasses function to return a list of 
#  all shapefiles.
#
fcList = arcpy.ListFeatureClasses()

fields = ["MyField", "OID@", "MyOtherField"]
counter = 0

# Use an update cursor on each shapefile to update two fields
#
for fc in fcList:
    with arcpy.da.UpdateCursor(fc, fields) as cursor:
        for row in cursor:
            # Example of using a counter to make every sure every value is 
            # unique in MyField in every shapefile.
            counter += 1
            row[0] = counter
            # Example of a conditional update showing that you can update
            # one field based on the values contained in another field
            # Also, unlike the Field Calculator, an update cursor can update 
            # every field in the shapefile in a single pass if you need to
            if (row[1] >= 0 and row[1] <= 10): # ObjectID values checked
                row[2] = 1 # MyOtherField updates to 1 if it meets the condition
            else:
                row[2] = 0 # MyOtherField updates to 0 if it doesn't meet it
            cursor.updateRow(row) # store the updates

If only some of the shapefiles in the directory have the field and other don't, code would have be added to check that the fields exists in the shapefile before attempting to update it. Shapefiles that don't have the field would be skipped.

1

Possible solution to 'automate' your process

  • Add shapefile to mxd, open it's table and click field calculator, Python
  • Apply modified version of this:

    [!thisField!, 7][!thisField!==50]

To modify expression:

  • Replace left pair by your field name, and new value
  • Right part is your condition, replace it by field name and value to be replaced

Try it first on a new field. If it works use copy and paste to apply it on the field of interest in your multiple files.

Alternative # 1: use RESULT window to repeat it on different shapefiles.

Alternative # 2: Create simplest model possible

enter image description here

Right click and select batch. Drag and drop as many shapefiles as you want

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.