[GIS] ModelBuilder – How to calculate Field using part of path name

arcgis-desktopfield-calculatormodelbuildervbscript

I attempted to include a model parameter for the workspace, rather than use the parse path tool (show in the image).I am trying to calculate a field using part of the feature class path name (specifically, the last three characters of the geodatabase name). Is this possible? I've tried using the parse path tool and including the value as an inline variable subsitution with the VB string right function. Perhaps a more complicated Python script is neccessary?

Best Answer

with Python (you'll need to define workspace, the script loops on all feature classes in the workspace and update the field with the last three characters of its name :

fcs = arcpy.ListFeatureClasses()
for fc in fcs:
    arcpy.CalculateField_management(fc, "field_to_update", "'" + fc[-3:] + "'", "PYTHON")

EDIT : for the gdb name , the expression would be

expression = arcpy.Describe(fc).catalogPath.split(".gdb")[0][:-3]