copy a field from one shapefile to another

4220
3
01-06-2012 05:46 AM
danielporter1
New Contributor
Hiyo,

I'm just getting started with Esri's geoprocessing tools and I've got a fairly basic question.  I'm trying to copy the value from a field in one shapefile to another shapefile.  The field already exists in both shapefiles.

I've tried a couple of approaches.  My first approach was to

#Make first shapefile into a feature layer so join will work
gp.MakeFeatureLayer_management(shapefile_one_path, shapefile_one_name)

# Join the Shapefiles
gp.AddJoin_management(shapefile_one_name, join_field, shapefile_two_name, join_field, "KEEP_COMMON")

# Then I write the new joined table to disk so I can run calculate field on it.
gp.Select_analysis(shapefile_one_name, "joined_shapefile.shp", "")


Unfortunately, this approach dorked up all of column names.

Next, I tried to update the column on the joined table:

#Make first shapefile into a feature layer so join will work
gp.MakeFeatureLayer_management(shapefile_one_path, shapefile_one_name)

# Join the Shapefiles
gp.AddJoin_management(shapefile_one_name, join_field, shapefile_two_name, join_field, "KEEP_COMMON")

# Calculate field...?
gp.CalculateField_management(shapefile_one_name, "field_to_update", "field_to_copy")


This resulted in a "general function failure."

Anybody have any tips?
0 Kudos
3 Replies
JeremyLuymes
New Contributor III
I might have a possible solution.

For your second option, in the final calculate field, you need to add the layer in the field to copy.

Lets say shape1 and shape2 are your shapefiles.

gp.CalculateField(shape1,"shape1.field_to_update","[shape2.field_to_copy]","VB")

This usually works for me. You might need to make both your shapefiles into layers.
0 Kudos
danielporter1
New Contributor
Jeremy,

Thank you!  That worked perfectly.  I did not have to make the second shapefile into a layer; I just fixed the syntax of my CalculateField call and it went off without a hitch.

This saves me so much clicking!

Daniel
0 Kudos
DanPatterson_Retired
MVP Emeritus
Check the qualified field names environment setting,
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001w00000008000000
I think it might solve the quirky file.fieldname issue that you allude to
0 Kudos