Does anyone have a script I can use in modelbuilder that will?:
parse an "_" from a filename
usable in modelbuilder with an inline variable (%name%) and Feature class iterator
transfer or copy the actual renamed shapefiles to another folder?
Any help or suggestions will be greatly appreciated.
enter image description here
2 Answers 2
Rather than writing a Python script tool you should be able to use a Calculate Value tool with the Python code recommended by @blah238:
r"%name%".replace("_","")
The next step will be to use the Rename tool to use the text string this creates to rename the feature class.
-
2A slight clarification. There is a distinction between the string methods of built-in sequence types like
str
andunicode
, and methods of the actualstring
module. Use of the built-in methods is preferred. So in the Calculate Value tool's expression box, you would enter something liker"%name%".replace("_","")
.blah238– blah2382013年06月18日 21:02:39 +00:00Commented Jun 18, 2013 at 21:02 -
The calculate value with the expression r"%name%".replace("_","") still doesn't work. This operation seems simple enough but still no luck. Do I need to define the workspace or parse a path?galindo– galindo2013年06月18日 21:47:01 +00:00Commented Jun 18, 2013 at 21:47
-
Actually, I take that back; the Calculate Value tool works with the expression r"%name%".replace("_","") but how do you transfer or copy the actual renamed shapefiles to another folder? I can see the renamed shapefiles with the Collect Values. Almost there!galindo– galindo2013年06月18日 22:08:39 +00:00Commented Jun 18, 2013 at 22:08
-
1I would suggest using Copy Features instead, as the OP would like to copy the shapefiles over to a new folder.Cindy Jayakumar– Cindy Jayakumar2013年06月19日 05:22:38 +00:00Commented Jun 19, 2013 at 5:22
Building on PolyGeo's answer above, the Collect Values
tool is not necessary, because as you iterate over the shapefiles, each one should be renamed and copied once only.
enter image description here
In the image above, the shapefiles are renamed and copied one at a time as the iterator iterates over them. Make sure to check the recursive
checkbox in the iterator dialog if your folder contains subfolders.
-
Renaming as part of the copy makes sense (+1) but I think when you said "the Collect Values tool is not necessary" you meant "the Rename tool is not necessary".2013年06月19日 11:39:11 +00:00Commented Jun 19, 2013 at 11:39
-
I duplicated the model above but I'm still getting the the "_" underscore in the filename for my output files. Any suggestions?galindo– galindo2013年06月19日 15:21:29 +00:00Commented Jun 19, 2013 at 15:21
-
1@PolyGeo I meant that if the recursive checkbox is ticked, the
Collect Values
is not necessary because it will iterate over each shapefile anyway, thereforeCopy Features
will run for each iteration and copy each shp.Cindy Jayakumar– Cindy Jayakumar2013年06月20日 04:55:47 +00:00Commented Jun 20, 2013 at 4:55 -
@galindo If you used PolyGeo's string replacement in the Calculate Value tool, then your
parsed_filename
contains the new shp name without underscores.Cindy Jayakumar– Cindy Jayakumar2013年06月20日 04:58:26 +00:00Commented Jun 20, 2013 at 4:58 -
I see what you mean - I had been talking about Calulate Value and had not noticed that @Galinda had a Collect Values tool in his/her model.2013年06月20日 09:08:08 +00:00Commented Jun 20, 2013 at 9:08
string.replace('_', '')
to remove the underscore.