[GIS] Calculate field in model builder using file name

arcgis-desktoperror-000539field-calculatormodelbuilder

I have browsed answers to similar questions but I still can't figure it out. I have created a new field called "namemonyr" in all of 646 shapefiles. I now want to populate that field using (preferably part of) the input file name. My files are currently named e.g. "cl_Arthur_Apr_12.shp". I want the namemonyr field to be filled in with the string "Arthur_Apr_12". The name, month and year elements are different in every shapefile name. I need to know when I output the tables as CSV files which subject the data came from and for what period – I need this info in the table, not just in the file name. I have the iterator working fine.

I have tried using the field calculator in model building using the expression

!%Name%[3:]! to remove the cl_ element of the file name.

The error message reads:

ERROR 000539: Invalid field
cl_Arthur_Apr_12
failed to execute (Calculate Field)

I have tried using the Parse Path tool to get the file name as the Value that then goes into the Expression part of the field calculator. But I don't really understand this tool.

I have tried variations on these. Nothing is working!

I am very new to ArcGIS and Python and have never used Python coding.

Best Answer

I'm not sure where %Name% is coming from, so I may not be able to answer your question entirely, but wrapping something in exclamation points is how you get a value out a field in a python code block. %Name% is a model builder variable, not a field name. So remove the the exclamation points and just use %Name%[3:] and see if that works.