How to: add field, then calculate same field with name of feature class

2743
2
Jump to solution
02-09-2016 10:02 PM
JustinOdell
Occasional Contributor III


Hi all,

This question has come up a lot, but I am struggling to find a working solution to what should be a simple problem.

Scenario:

I have a file geodatabase with 100 feature classes.

I want to add a field to each feature class and call it "Name"

I then want to calculate the field "Name" with the actual name of the feature class, for each feature class in the geodatabase.

I'm not a whiz at python and model builder, but have more luck with the latter.

Any help appreciated.

I've seen a few suggestions in other forums to use VB Script in the field calculator and have for example:

Name = "%Name%" , however this seems to produce a field that is populated with "%Name%" instead of the actual name of the feature class.

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

When it comes to VBScript and ArcGIS, do what Nancy does, "just say no."  Model Builder has its place, it just isn't in my toolbox.  I encourage you to learn Python, it will open up ArcGIS to you much more than Model Builder, and the skills are infinitely more transferrable.

The following should work for you.  Just paste the code into the interactive Python window and call the add_table_names function with the geodatabase in question.  The script will find all tables and feature classes not in feature datasets and append the name of the table or feature class as a new field.  If you want to recurse into the feature datasets, it isn't too hard to update the code.

def add_table_names(workspace):
    desc = arcpy.Describe(workspace)
    for child in desc.children:
        if child.datasetType not in ('Table', 'FeatureClass'): continue
        arcpy.AddField_management(child.catalogPath, "FC_NAME", "TEXT")
        arcpy.CalculateField_management(child.catalogPath,
                                        "FC_NAME",
                                        "'{}'".format(child.baseName),
                                        "PYTHON_9.3")

View solution in original post

2 Replies
JayantaPoddar
MVP Esteemed Contributor

Check this link.

Esri Mapping Centre - Ask a Cartographer

To calculate the field with the feature class name, you need to parse the feature class path. Parse Path is available as a model-specific tool. You can also use Iterators inside the model to loop through each of the feature classes.

EDIT: You could also use the NAME variable in iterator, instead of PARSE PATH.



Think Location
JoshuaBixby
MVP Esteemed Contributor

When it comes to VBScript and ArcGIS, do what Nancy does, "just say no."  Model Builder has its place, it just isn't in my toolbox.  I encourage you to learn Python, it will open up ArcGIS to you much more than Model Builder, and the skills are infinitely more transferrable.

The following should work for you.  Just paste the code into the interactive Python window and call the add_table_names function with the geodatabase in question.  The script will find all tables and feature classes not in feature datasets and append the name of the table or feature class as a new field.  If you want to recurse into the feature datasets, it isn't too hard to update the code.

def add_table_names(workspace):
    desc = arcpy.Describe(workspace)
    for child in desc.children:
        if child.datasetType not in ('Table', 'FeatureClass'): continue
        arcpy.AddField_management(child.catalogPath, "FC_NAME", "TEXT")
        arcpy.CalculateField_management(child.catalogPath,
                                        "FC_NAME",
                                        "'{}'".format(child.baseName),
                                        "PYTHON_9.3")