# Filtering files while copying/moving

**URL:** <https://www.ruby-forum.com/t/filtering-files-while-copying-moving/68306>\
**Category:** Ruby\
**Created:** [September 11, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/filtering-files-while-copying-moving/68306 "2006-09-11T16:46:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ola\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/858c86/32.png) [@Ola\_B](https://www.ruby-forum.com/u/Ola_B)\
**Post date:** [September 11, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/filtering-files-while-copying-moving/68306/1 "2006-09-11T16:46:26Z")

</div>

Hi,

I have the need to filter files, replacing in-place-tokens with  
variables during the copy. I would like to be able to do something like  
this:

FileUtils.filter\_cp\_r from\_dir, to\_dir, { “SERVER1” =\> “[s1.example.com](http://s1.example.com)”,

```
      /LDAP_SERVER_([\d])/ => "l\\1.example.com" }

```

and those files will be copied, but modified so that in the destination  
dir, all occurances of SERVER1 have been changed to [s1.example.com](http://s1.example.com) and  
the regexp substituted with the substitution string.

Is there any library for this? I’ve looked but not found. My current  
approach is to create a small lib for this myself, using FileUtils, Find  
and Tempfile. But it feels like this should be a common operation,  
especially when doing installation scripts.

And before someone suggests it: no, I’m not at liberty to change the  
tokens, so I can’t embed ERb-tags in the source material.

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![Ola\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/858c86/32.png) [@Ola\_B](https://www.ruby-forum.com/u/Ola_B)\
**Post date:** [September 13, 2006, 2:48am UTC](https://www.ruby-forum.com/t/filtering-files-while-copying-moving/68306/2 "2006-09-13T02:48:20Z")

</div>

I’ve done an even uglier hack for my makefiles at work.

I’d definitely be interested in it if you came up with something like  
this.

Matt
