Copy attribute table fields from one shapefile to another

I have about 20 shapefiles that all need to have the same attribute table fields. I have the attribute table fields all setup the way I want for one of the shapefiles. I want to copy this attribute table setup over to the other shapefiles. The entries of the tables are all empty for now, I just want to use the same columns/fields for all the shapefiles. How can I copy the attribute table into the remaining shapefiles? Using ArcInfo 10.0

1 Solution
Accepted Solutions Frequent Contributor ‎09-09-2015 01:18 PM

If you are comfortable with python, you could make a list of all the fields of the one (template) shapefile you have set-up, then loop through each other shapefile and add a field for each field that exists in the template shapefile.

If this is the way you want to go about it and you need help with this, I can get you some code to use to manage this process.

13 Replies Regular Contributor III ‎09-09-2015 01:07 PM

Do you have a common field that you can use to to join the two feature classes or are you going to need to do a spatial join to link the data?

Occasional Contributor III ‎09-09-2015 01:17 PM

The only field they all have in common is SHAPE (all polygons), can I use that for a join?

Regular Contributor III ‎09-09-2015 01:54 PM

If you use a spatial join the fields from both feature classes would be present in a new feature class, but i don't think that will do what you are after. i miss read your question sorry. I would suggest using model builder set it up for each of the fields you want to add on one feature class and let it run then change the feature class to another and run it again until you get all 20

Frequent Contributor ‎09-09-2015 01:18 PM

If you are comfortable with python, you could make a list of all the fields of the one (template) shapefile you have set-up, then loop through each other shapefile and add a field for each field that exists in the template shapefile.

If this is the way you want to go about it and you need help with this, I can get you some code to use to manage this process.

Occasional Contributor III ‎09-09-2015 02:02 PM

I figured out a workaround by merging all the shapefiles together then setting up the attribute table & breaking each feature back out into its individual shapefile. Not ideal but takes less time than entering each field for all 20 shapefiles. Can't believe I didn't think of that sooner, I guess that's because I assumed there would be an easy way to copy/paste attribute table templates.

I am learning Python so I would be interested in seeing code to do this for some arbitrary field names, say: A, B & C. It could also come in handy if I run into this situation again for a great many shapefiles and I can't come up with a workaround.

Frequent Contributor ‎09-09-2015 02:41 PM

This is far from an ideal solution, since I am uncomfortable with dictionaries, I make a list of lists that contain the attributes for existings fields using the arcpy module. In the below example, your "template" shapefile needs to be in a seperate directory from the rest, since this script does not check to see if a field already exists. If your other shapefiles were in multiple directories or subdirectories, we would need to use the os module walk function, or arcpy.da.Walk to create the list of shapes.

I do not have data to test on at the moment, so this is untested and I can't show the output.

#importing Modules import arcpy #Creating variable for template file path template_shp = "FilePathtoShp" #Making a list of Field Objects fields = arcpy.ListFields(template_shp) #Creating empty list to append field attribute list to allfields = [] #Looping through field objects for field in fields: #Creating empty list to append field attributes to field_attributes = [] #Appending all relevant field object elements as strings to list field_attributes.append(field.name , field.type , field.precision , field.scale, field.length, field.aliasName , field.isNullable , field.required) #Appending list of field elements to new field list allfields.append(field_attributes) #Setting workspace arcpy.env.workspace = "directoryofshapefiles" #Creating List of FeatureClasses in workspace shapes = arcpy.ListFeatureClasses() #Looping through each shape for shape in shapes: #Looping through each field for field in allfields: #Adding field based on field attributes of original field arcpy.AddField_management(shape , field[0] , field[1] , field[2], field[3], field[4] , field[5], field[6] , field[7])

I'm sure someone more proficient with python could show a better way.