Return the character string minus 13 characters

992
5
Jump to solution
07-20-2021 11:56 AM
MillionMap
New Contributor III

I need the first 13 characters of value %name% to be removed.  %name% is the filename of a csv file, a field name in it is equal to the filename minus the first 13 characters.  For example: filename "bench325_320_al2o3", field name "al2o3".  This model will be running through over 200 hundred csv files and I need the "al2o3" (which will be different for each file) to be a new value for each csv and inserted where the red circle is in the attached image.

Capture2.JPG

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

To get this to work, use the Calculate Value tool. If your names are consistent as you say, either of these expressions would work.

 

r"%Name%"[-5:] # last 5 characters
r"%Name%"[13:] # characters 14-end

 

and connect the output of the Calculate Value as a precondition to the tool where you will be using the output variable (with % around it, for example %Value%). %Value% is entered in the tool dialog where you had %Name% entered before.

Hope this helps.

UPDATE: probably a good idea to connect the variable Name to your Calculate Value tool in the model as a precondition to make sure you have the current value of %Name% used. Also documents the model nicely. I often rename the Calculate Value tool to something like Calculate Value end of Name to make it obvious what's going on in the model.

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor

it is rarely a good idea to hardcode a soution without considering the exceptions

a = "12345678901234567890"
a[:-13]
'1234567'

but... here is one


... sort of retired...
MillionMap
New Contributor III

Thanks for the reply but it doesn't work.  The amount of characters in the filenames are consistent, so hardcoding in this situation is fine.  You're example removes the last 13, I need the first 13 removed and also how it is to be entered in the red circle location.

0 Kudos
curtvprice
MVP Esteemed Contributor

To get this to work, use the Calculate Value tool. If your names are consistent as you say, either of these expressions would work.

 

r"%Name%"[-5:] # last 5 characters
r"%Name%"[13:] # characters 14-end

 

and connect the output of the Calculate Value as a precondition to the tool where you will be using the output variable (with % around it, for example %Value%). %Value% is entered in the tool dialog where you had %Name% entered before.

Hope this helps.

UPDATE: probably a good idea to connect the variable Name to your Calculate Value tool in the model as a precondition to make sure you have the current value of %Name% used. Also documents the model nicely. I often rename the Calculate Value tool to something like Calculate Value end of Name to make it obvious what's going on in the model.

DanPatterson
MVP Esteemed Contributor
a[:-13]
a[:13]
a[-13:]
a[13:]

by example... you only had 4 options to test replacing "a" with your input string.

Which one worked?


... sort of retired...
0 Kudos
JoeBorgione
MVP Emeritus

 

a = "12345678901234567890"
a[13:]
'4567890'

 

You can learn a lot about string manipulation here....

 

That should just about do it....