1

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.

1

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.

  • Hi there, I tried that but it got a slightly different error message which now says NameError: name 'cl_Aurthur_Apr_12' is not defined. Name is the name of the files in the iterator. My understanding is that %Name% iterates through each shapefile in the folder you are working in. – Aoibh Nov 13 '14 at 17:14
  • 1
    oh, then you probably need to wrap it in quotes so python treats it as a string, instead of a variable name. "%Name%"[3:] – MWrenn Nov 13 '14 at 18:35
  • That worked @MWrenn! Is it in appropriate to say I love you?! thanks so much! – Aoibh Nov 14 '14 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.