2

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

7
  • 3
    You can use a bit of Python and simply call string.replace('_', '') to remove the underscore.
    – Radar
    Commented Jun 18, 2013 at 17:07
  • I downvoted because this was asking someone else to do research for them.
    – Pete
    Commented Jun 18, 2013 at 18:23
  • 3
    I would say the question is well within the bounds of the rules and format of GIS SE.
    – Radar
    Commented Jun 18, 2013 at 19:07
  • @Pete this is the kind of thing which is hard to find if you don't know what words to use when searching, and eyes-closed easy when you do know the words. Commented Jun 18, 2013 at 22:52
  • related: gis.stackexchange.com/questions/25584/… Commented Jun 18, 2013 at 22:57

2 Answers 2

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.

4
  • 2
    A slight clarification. There is a distinction between the string methods of built-in sequence types like str and unicode, and methods of the actual string module. Use of the built-in methods is preferred. So in the Calculate Value tool's expression box, you would enter something like r"%name%".replace("_","").
    – blah238
    Commented 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
    Commented 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
    Commented Jun 18, 2013 at 22:08
  • 1
    I would suggest using Copy Features instead, as the OP would like to copy the shapefiles over to a new folder. Commented Jun 19, 2013 at 5:22
2

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.

6
  • 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".
    – PolyGeo
    Commented 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
    Commented 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, therefore Copy Features will run for each iteration and copy each shp. Commented 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. Commented 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.
    – PolyGeo
    Commented Jun 20, 2013 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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