# Moving files matching Regex

**URL:** <https://rubytalk.org/t/moving-files-matching-regex/42484>\
**Category:** ruby-talk\
**Created:** [25 November 2007 10:00 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484 "2007-11-25T10:00:04Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark9](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark9](https://rubytalk.org/u/Mark9)\
**Post date:** [25 November 2007 10:00 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/1 "2007-11-25T10:00:04Z")

</div>

Hi all,

A question relating to the code below.

What I'm trying to do is:  
Files named CAL221107.xls, NCPH141202.xls or GOH333333.xls for example  
in the current directory should be appended to an array (just the file  
name, not the full path) and moved to ./sent/

It seems to be working, but is there a more Rubyish way of doing it?

> **···**
>
> -------------------------------------------------------------------------  
> require 'find'  
> require 'fileutils'
> 
> a=[]  
> fglob = Regexp.new('(CAL|NCPH|GOH)\d{6}\.xls')
> 
> Find.find('./') do |path|  
> &nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;if curr\_file =~ /#{fglob}/  
> &nbsp;&nbsp;&nbsp;&nbsp;a \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;new\_file = "./sent/#{curr\_file}"  
> &nbsp;&nbsp;&nbsp;&nbsp;[FileUtils.mv](http://FileUtils.mv)(curr\_file, new\_file) if not File.exists?(new\_file)  
> &nbsp;&nbsp;end  
> end
> 
> puts a
> 
> -------------------------------------------------------------------------
> 
> thanks,
> 
> --  
> Mark

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [25 November 2007 10:30 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/2 "2007-11-25T10:30:10Z")

</div>

Alle domenica 25 novembre 2007, Mark Woodward ha scritto:

> Hi all,
> 
> A question relating to the code below.
> 
> What I'm trying to do is:  
> Files named CAL221107.xls, NCPH141202.xls or GOH333333.xls for example  
> in the current directory should be appended to an array (just the file  
> name, not the full path) and moved to ./sent/
> 
> It seems to be working, but is there a more Rubyish way of doing it?
> 
> -------------------------------------------------------------------------  
> require 'find'  
> require 'fileutils'
> 
> a=  
> fglob = Regexp.new('(CAL|NCPH|GOH)\d{6}\.xls')
> 
> Find.find('./') do |path|  
> &nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;if curr\_file =~ /#{fglob}/  
> &nbsp;&nbsp;&nbsp;&nbsp;a \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;new\_file = "./sent/#{curr\_file}"  
> &nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv(curr\_file, new\_file) if not File.exists?(new\_file)  
> &nbsp;&nbsp;end  
> end
> 
> puts a
> 
> -------------------------------------------------------------------------
> 
> thanks,

Not very different:

require 'find'  
require 'fileutils'

a=  
fglob = /(CAL|NCPH|GOH)\d{6}\.xls/

Find.find('.') do |f|  
&nbsp;&nbsp;name = File.basename f  
&nbsp;&nbsp;if name=~ fglob  
&nbsp;&nbsp;&nbsp;&nbsp;a \<\< name  
&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv f, "sent/#{name}" unless File.exist? "sent/#{name}"  
&nbsp;&nbsp;end  
end

A couple of notes:  
\* You can create a regexp using the // syntax, instead of using Regexp.new  
\* when you call String#=~ you don't need to use /#{...}/, if you already have  
a regexp  
\* I think you need to pass path, and not curr\_file, as the first argument of  
FileUtils.mv, otherwise it won't work if the file is in a subdirectory.

Stefano

---

<div class="post-metadata">

**Author:** ![Mark9](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark9](https://rubytalk.org/u/Mark9)\
**Post date:** [26 November 2007 10:45 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/3 "2007-11-26T10:45:06Z")

</div>

On Sun, 25 Nov 2007 20:59:46 +1100

> **···**
>
> Mark Woodward \<[markonlinux@internode.on.net](mailto:markonlinux@internode.on.net)\> wrote:
> 
> thanks to all replies. Very informative.  
> I've benchmarked this and 'get\_file\_names2' is considerably quicker!  
> (if I'm benchmarking correctly?)
> 
> Could someone explain why? ie what is it about get\_file\_names2 that  
> makes it so much quicker? What is it about get\_file\_names that slows it  
> down?
> 
> def get\_file\_names  
> &nbsp;&nbsp;fn=[]  
> &nbsp;&nbsp;Find.find('./') do |path|  
> &nbsp;&nbsp;&nbsp;&nbsp;Find.prune if File.basename(path) == 'sent'  
> &nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ FILE\_REGEX  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;fn  
> end
> 
> def get\_file\_names2  
> &nbsp;&nbsp;fglob= %r{^(CAL|NCPH|GOH)\d{6}\.xls$}x  
> &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> &nbsp;&nbsp;my\_files = all\_files.grep(fglob)  
> end
> 
> [Benchmark.bm](http://Benchmark.bm)(5) do |timer|  
> &nbsp;&nbsp;timer.report('get\_file\_names') {10000.times {get\_file\_names}}  
> &nbsp;&nbsp;timer.report('get\_file\_names2') {10000.times {get\_file\_names2}}  
> end
> 
> Results:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> get\_file\_names 3.040000 0.700000 3.740000 (5.440394)  
> get\_file\_names2 0.570000 0.160000 0.730000 (0.997355)
> 
> cheers,
> 
> --  
> Mark

---

<div class="post-metadata">

**Author:** ![Mark9](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark9](https://rubytalk.org/u/Mark9)\
**Post date:** [25 November 2007 11:10 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/4 "2007-11-25T11:10:08Z")

</div>

Hi Stefano,

> Not very different:
> 
> require 'find'  
> require 'fileutils'
> 
> a=  
> fglob = /(CAL|NCPH|GOH)\d{6}\.xls/
> 
> Find.find('.') do |f|  
> &nbsp;&nbsp;name = File.basename f  
> &nbsp;&nbsp;if name=~ fglob  
> &nbsp;&nbsp;&nbsp;&nbsp;a \<\< name  
> &nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv f, "sent/#{name}" unless File.exist? "sent/#{name}"  
> &nbsp;&nbsp;end  
> end
> 
> A couple of notes:  
> \* You can create a regexp using the // syntax, instead of using  
> Regexp.new

that's better, thanks

> \* when you call String#=~ you don't need to use /#{...}/, if you  
> already have a regexp

another good thing to know.

> \* I think you need to pass path, and not curr\_file, as the first  
> argument of FileUtils.mv, otherwise it won't work if the file is in a  
> subdirectory.

There won't be any sub directories except for /sent.  
I've discovered from the cookbook I can use prune to ignore it.  
Below's where I'm up to.

> Stefano

appreciate your help,  
cheers,

> **···**
>
> On Sun, 25 Nov 2007 05:30:10 -0500 Stefano Crocco \<stefano.crocco@alice.it\> wrote:
> 
> --  
> Mark
> 
> ---------------------------------------------------------------------------  
> require 'find'  
> require 'fileutils'
> 
> FILE\_REGEX = /(CAL|NCPH|GOH)\d{6}\.xls/  
> SENT\_DIR = "./sent/"
> 
> def get\_file\_names  
> &nbsp;&nbsp;fn=  
> &nbsp;&nbsp;Find.find('./') do |path|  
> &nbsp;&nbsp;&nbsp;&nbsp;Find.prune if File.basename(path) == 'sent'  
> &nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ FILE\_REGEX  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;fn  
> end
> 
> def move\_files  
> &nbsp;&nbsp;@a.each do |fn|  
> &nbsp;&nbsp;&nbsp;&nbsp;new\_loc = SENT\_DIR + fn  
> &nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv(fn, new\_loc) if not File.exists?(new\_loc)  
> &nbsp;&nbsp;end  
> end
> 
> @a = get\_file\_names  
> move\_files  
> puts @a

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 November 2007 12:35 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/5 "2007-11-26T12:35:41Z")

</div>

> On Sun, 25 Nov 2007 20:59:46 +1100
> 
> thanks to all replies. Very informative.  
> I've benchmarked this and 'get\_file\_names2' is considerably quicker!  
> (if I'm benchmarking correctly?)
> 
> Could someone explain why? ie what is it about get\_file\_names2 that  
> makes it so much quicker? What is it about get\_file\_names that slows it  
> down?

In \_2 you do a dir glob in \*one\* directory only while find recursively  
descends a \*directory tree\*. Even though you prune in some cases it  
comes as no surprise that this is slower.

> def get\_file\_names  
> &nbsp;&nbsp;fn=  
> &nbsp;&nbsp;Find.find('./') do |path|  
> &nbsp;&nbsp;&nbsp;&nbsp;Find.prune if File.basename(path) == 'sent'  
> &nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ FILE\_REGEX  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;fn  
> end
> 
> def get\_file\_names2  
> &nbsp;&nbsp;fglob= %r{^(CAL|NCPH|GOH)\d{6}\.xls$}x  
> &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> &nbsp;&nbsp;my\_files = all\_files.grep(fglob)  
> end
> 
> Benchmark.bm(5) do |timer|  
> &nbsp;&nbsp;timer.report('get\_file\_names') {10000.times {get\_file\_names}}  
> &nbsp;&nbsp;timer.report('get\_file\_names2') {10000.times {get\_file\_names2}}  
> end
> 
> Results:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> get\_file\_names 3.040000 0.700000 3.740000 (5.440394)  
> get\_file\_names2 0.570000 0.160000 0.730000 (0.997355)

Kind regards

robert

> **···**
>
> 2007/11/26, Mark Woodward \<markonlinux@internode.on.net\>:
> 
> > Mark Woodward \<markonlinux@internode.on.net\> wrote:
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Raul\_Parolari](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Raul\_Parolari](https://rubytalk.org/u/Raul_Parolari)\
**Post date:** [25 November 2007 11:48 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/6 "2007-11-25T11:48:30Z")

</div>

Mark Woodward wrote:

> There won't be any sub directories except for /sent.

I add this to the advice from Stefano:

1) if you just want to find the files in the current directory, then we  
do not need to use Find (which, as Stefano pointed out, could find files  
in subdirectories that you may not foresee now..); and we can collect  
the files in one shot with grep.

2) for precaution, it is better to match the complete file name (just in  
case there may be other files with that name + an extension, eg like  
.saved, etc).

3) we could place the method that moves the files into class Array,  
rather than leaving it in the toplevel. (Consider a param for the  
destination dir, unless you are sure that it will be always a Constant).

class Array  
def move\_to\_subdir  
&nbsp;&nbsp;&nbsp;self.each do |fn|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_loc = SENT\_DIR + fn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv(fn, new\_loc) unless File.exists?(new\_loc)  
&nbsp;&nbsp;&nbsp;end  
end  
end

# the main is now 4 lines:

fglob= %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x

all\_files = Dir.glob("\*")  
my\_files = all\_files.grep(fglob)

my\_files.move\_to\_subdir

Regards,

Raul

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Raul\_Parolari](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Raul\_Parolari](https://rubytalk.org/u/Raul_Parolari)\
**Post date:** [26 November 2007 19:39 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/7 "2007-11-26T19:39:22Z")

</div>

Mark Woodward wrote:

> I've benchmarked this and 'get\_file\_names2' is considerably quicker!
> 
> Could someone explain why? ie what is it about get\_file\_names2 that  
> makes it so much quicker? What is it about get\_file\_names that slows it  
> down?

Robert Klemme wrote:

> In \_2 you do a dir glob in \*one\* directory only while find recursively  
> descends a \*directory tree\*. Even though you prune in some cases it  
> comes as no surprise that this is slower.

Actually, I think that most of the gain in the final solution comes from  
this:  
a) in the original solution, the result array is built inserting  
manually each file that matches the pattern in the array..  
b) in the final solution (get\_file\_names2) not only Find is replaced by  
Dir.glob, but the result array is built by grep.

This may sound surprising, but map (collect) and grep are highly  
optimized C mechanisms; moreover they can do smart guesses on the size  
of the result array (in particular, grep can inmediately conclude that  
the size of the result array will be less than the array given to him).  
In the manual solution instead, every item added to the array probably  
requires some memory reallocation.

Let's verify if this is true, benchmarking 3 variants:

0) get\_file\_names : Find + manual array build  
1) get\_file\_names1: Dir.glob + manual array build  
2) get\_file\_names2: Dir.glob + grep

# uses Find + manual array build  
def get\_file\_names  
&nbsp;&nbsp;fn=  
&nbsp;&nbsp;Find.find('./') do |path|  
&nbsp;&nbsp;&nbsp;&nbsp;Find.prune if File.basename(path) == 'sent'  
&nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
&nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;fn  
end

# uses Dir.glob + manual array build  
def get\_file\_names1  
&nbsp;&nbsp;all\_files = Dir.glob("\*")  
&nbsp;&nbsp;my\_files.each do |path|  
&nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
&nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;fn  
end

# uses Dir.glob + grep  
def get\_file\_names2  
&nbsp;&nbsp;all\_files = Dir.glob("\*")  
&nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
end

# with 10 files matching the pattern  
Benchmark.bm(5) do |timer|  
&nbsp;&nbsp;timer.report('get\_file\_names') {10000.times {get\_file\_names}}  
&nbsp;&nbsp;timer.report('get\_file\_names1') {10000.times {get\_file\_names}}  
&nbsp;&nbsp;timer.report('get\_file\_names2') {10000.times {get\_file\_names2}}  
end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
get\_file\_names 4.680000 3.870000 8.550000 ( 8.567400)  
get\_file\_names1 4.670000 3.870000 8.540000 ( 8.564753)  
get\_file\_names2 0.690000 0.780000 1.470000 ( 1.470924)

The 580% improvement in the final solution comes almost exclusively from  
the grep.

> **···**
>
> ---  
> I add this for Mark:  
> it is best in any case to avoid the unnecessary 'Find', for several  
> reasons; the first is that what you need is the simple Dir.glob.
> 
> But let me cite a more insidious problem in that code: the idea of  
> pruning explicitly 'sent'. Even if you think that the only subdirectory  
> present will be 'sent', what happens if 8 months from now another person  
> (or yourself, after some more thousands lines of code!) needs to add  
> another subdir with files matching that pattern (perhaps just to save a  
> version of them)? will he remember the assumption done months before?
> 
> Moreover, as Stefano pointed out at the beginning of the thread, this  
> would cause subtle file corruptions, as the code is not set up to deal  
> with the path correctly (a testing nightmare would follow, until  
> somebody remembers the 'assumption'; some people have seen this hundreds  
> of times..:-).
> 
> So, the best is not to code 'assumptions' in the code; and of course to  
> use the right tools for the problem at hand: in this case, Dir.glob and  
> the beautiful grep
> 
> Regards
> 
> Raul
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [25 November 2007 16:24 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/8 "2007-11-25T16:24:59Z")

</div>

I would not do that because it is not really an Array operation. Having a top level method (aka function) is better IMHO.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 25.11.2007 12:48, Raul Parolari wrote:
> 
> > 3) we could place the method that moves the files into class Array, rather than leaving it in the toplevel. (Consider a param for the destination dir, unless you are sure that it will be always a Constant).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 November 2007 20:35 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/9 "2007-11-26T20:35:45Z")

</div>

> Mark Woodward wrote:
> 
> > I've benchmarked this and 'get\_file\_names2' is considerably quicker!
> > 
> > Could someone explain why? ie what is it about get\_file\_names2 that  
> > makes it so much quicker? What is it about get\_file\_names that slows it  
> > down?
> 
> Robert Klemme wrote:
> 
> > In \_2 you do a dir glob in \*one\* directory only while find recursively  
> > descends a \*directory tree\*. Even though you prune in some cases it  
> > comes as no surprise that this is slower.
> 
> Actually, I think that most of the gain in the final solution comes from this:  
> a) in the original solution, the result array is built inserting manually each file that matches the pattern in the array..  
> b) in the final solution (get\_file\_names2) not only Find is replaced by Dir.glob, but the result array is built by grep.
> 
> This may sound surprising, but map (collect) and grep are highly optimized C mechanisms; moreover they can do smart guesses on the size of the result array (in particular, grep can inmediately conclude that the size of the result array will be less than the array given to him). In the manual solution instead, every item added to the array probably requires some memory reallocation.
> 
> Let's verify if this is true, benchmarking 3 variants:
> 
> 0) get\_file\_names : Find + manual array build  
> 1) get\_file\_names1: Dir.glob + manual array build  
> 2) get\_file\_names2: Dir.glob + grep
> 
> # uses Find + manual array build  
> def get\_file\_names  
> &nbsp;&nbsp;fn=  
> &nbsp;&nbsp;Find.find('./') do |path|  
> &nbsp;&nbsp;&nbsp;&nbsp;Find.prune if File.basename(path) == 'sent'  
> &nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;fn  
> end
> 
> # uses Dir.glob + manual array build  
> def get\_file\_names1  
> &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> &nbsp;&nbsp;my\_files.each do |path|  
> &nbsp;&nbsp;&nbsp;&nbsp;curr\_file = File.basename(path)  
> &nbsp;&nbsp;&nbsp;&nbsp;if curr\_file =~ %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fn \<\< curr\_file  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;fn  
> end
> 
> # uses Dir.glob + grep  
> def get\_file\_names2  
> &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> &nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
> end

What about this one?

def get\_file\_names\_3  
&nbsp;&nbsp;&nbsp;Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]  
end

> # with 10 files matching the pattern  
> Benchmark.bm(5) do |timer|  
> &nbsp;&nbsp;timer.report('get\_file\_names') {10000.times {get\_file\_names}}  
> &nbsp;&nbsp;timer.report('get\_file\_names1') {10000.times {get\_file\_names}}  
> &nbsp;&nbsp;timer.report('get\_file\_names2') {10000.times {get\_file\_names2}}  
> end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> get\_file\_names 4.680000 3.870000 8.550000 ( 8.567400)  
> get\_file\_names1 4.670000 3.870000 8.540000 ( 8.564753)  
> get\_file\_names2 0.690000 0.780000 1.470000 ( 1.470924)
> 
> The 580% improvement in the final solution comes almost exclusively from the grep.
> 
> ---  
> I add this for Mark:  
> it is best in any case to avoid the unnecessary 'Find', for several reasons; the first is that what you need is the simple Dir.glob.
> 
> But let me cite a more insidious problem in that code: the idea of pruning explicitly 'sent'. Even if you think that the only subdirectory present will be 'sent', what happens if 8 months from now another person (or yourself, after some more thousands lines of code!) needs to add another subdir with files matching that pattern (perhaps just to save a version of them)? will he remember the assumption done months before?
> 
> Moreover, as Stefano pointed out at the beginning of the thread, this would cause subtle file corruptions, as the code is not set up to deal with the path correctly (a testing nightmare would follow, until somebody remembers the 'assumption'; some people have seen this hundreds of times..:-).
> 
> So, the best is not to code 'assumptions' in the code; and of course to use the right tools for the problem at hand: in this case, Dir.glob and the beautiful grep

Good point! That's basically the same reason why it's better to explicitely open ports in firewall configs than to close "dangerous" ports.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 26.11.2007 20:39, Raul Parolari wrote:

---

<div class="post-metadata">

**Author:** ![Raul\_Parolari](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Raul\_Parolari](https://rubytalk.org/u/Raul_Parolari)\
**Post date:** [25 November 2007 22:46 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/10 "2007-11-25T22:46:24Z")

</div>

Robert Klemme wrote:

> I would not do that because it is not really an Array operation.

You are right; it does not belong in class Array.

> Having a top level method (aka function) is better IMHO.

Here I use the occasion to share a perplexity I have, when I look at  
Ruby programs, and I sometimes see a myriad of top-level methods. If I  
ask the reason, the answer is a vague "they do not really fit well  
anywhere", which leaves me a bit disconcerted (especially when the  
script is part of a project that may grow, not something written to be  
used once).

In the case discussed, I would place this method in a module (that would  
just need to be required):

module MyUtils  
&nbsp;&nbsp;def self.mv\_files\_to\_subdir(files, subdir)  
&nbsp;&nbsp;&nbsp;&nbsp;files.each do |curr\_file|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;..  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

or, as this is in fact a file operation, I would consider to reopen  
FileUtils and add the method:

> **···**
>
> #--  
> module FileUtils  
> &nbsp;&nbsp;def self.mv\_files\_to\_subdir(files, subdir)  
> &nbsp;&nbsp;&nbsp;&nbsp;files.each do |curr\_file|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_file = "./#{subdir}/#{curr\_file}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv(curr\_file, new\_file) unless File.exists?(new\_file)  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> fglob= %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x  
> all\_files = Dir.glob("\*")  
> my\_files = all\_files.grep(fglob)
> 
> FileUtils.mv\_files\_to\_subdir(my\_files, 'sent')  
> #-------
> 
> It is a small point (some may say 'but what's the difference with a  
> top-level method?'), but let me know your opinion, if you have a chance
> 
> Regards,
> 
> Raul  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Raul\_Parolari](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Raul\_Parolari](https://rubytalk.org/u/Raul_Parolari)\
**Post date:** [26 November 2007 23:17 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/11 "2007-11-26T23:17:37Z")

</div>

Robert Klemme wrote:

> What about this one?
> 
> def get\_file\_names\_3  
> &nbsp;&nbsp;&nbsp;Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]  
> end

Robert

&nbsp;&nbsp;I had not tested case 1 in the benchmark (I dropped digit '1' in the  
call); it turns out that it is the Find, as you had thought, to be the  
one that causes most inefficiency; without repeating all the code, these  
are the results, including also your last suggestion.

# uses Find, and selects files manually  
def get\_file\_names  
&nbsp;&nbsp;fn=  
&nbsp;&nbsp;..  
&nbsp;&nbsp;fn  
end

# 1) uses Dir.glob and builds array with a loop  
def get\_file\_names1  
&nbsp;&nbsp;fn=  
&nbsp;&nbsp;all\_files = Dir.glob("\*")  
&nbsp;&nbsp;..  
&nbsp;&nbsp;fn  
end

# 2) uses Dir.glob and grep  
def get\_file\_names2  
&nbsp;&nbsp;all\_files = Dir.glob("\*")  
&nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
end

# 3) variation of solution 2  
def get\_file\_names3  
&nbsp;&nbsp;&nbsp;Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]  
end

# with 30 files  
Benchmark.bm(5) do |timer|  
&nbsp;&nbsp;timer.report('get\_file\_names') {10\_000.times {get\_file\_names} }  
&nbsp;&nbsp;timer.report('get\_file\_names1') {10\_000.times {get\_file\_names1} }  
&nbsp;&nbsp;timer.report('get\_file\_names2') {10\_000.times {get\_file\_names2} }  
&nbsp;&nbsp;timer.report('get\_file\_names3') {10\_000.times {get\_file\_names3} }  
end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
get\_file\_names 14.640000 9.080000 23.720000 ( 23.778029)  
get\_file\_names1 1.690000 1.200000 2.890000 ( 2.903737)  
get\_file\_names2 1.370000 1.210000 2.580000 ( 2.581539)  
get\_file\_names3 1.430000 3.530000 4.960000 ( 4.968951)

Solution 2) is, as we saw before, the winner: 10 times faster than the  
original solution. But the grep only improves things by 10%; 90% of the  
contribution comes from removing Find (as Robert had guessed)!

Regarding the last one (called solution 3) from you:

> &nbsp;&nbsp;&nbsp;Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]

This turned out to be 2 times slower than solution 2. Checking if  
something is between 0-9 is apparently quite slower than checking for  
'digit'.

In conclusion:  
Solution 2 is the fastest; but the reason is not the grep (as I had  
theorized), which accounts only for 10% of the improvement; the other  
90% comes from removing Find, as Robert had guessed.

Last consideration: increasing the number of files, the weight of 'grep'  
in the improvement increases (but, enough of benchmarks for to-day :-).

Regards

Raul

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 November 2007 07:34 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/12 "2007-11-26T07:34:53Z")

</div>

Definitively the right way to go if there is a chance that the code is  
reused and might create namespace collisions in a larger project. If  
if is just used in a simple script I would stick with the top level  
method defined inside the script because distributing the code would  
be unnecessarily complex in that case. One can refactor later if  
needed.

Kind regards

robert

> **···**
>
> 2007/11/25, Raul Parolari \<raulparolari@gmail.com\>:
> 
> > Robert Klemme wrote:
> > 
> > \> I would not do that because it is not really an Array operation.
> > 
> > You are right; it does not belong in class Array.
> > 
> > \> Having a top level method (aka function) is better IMHO.
> > 
> > Here I use the occasion to share a perplexity I have, when I look at  
> > Ruby programs, and I sometimes see a myriad of top-level methods. If I  
> > ask the reason, the answer is a vague "they do not really fit well  
> > anywhere", which leaves me a bit disconcerted (especially when the  
> > script is part of a project that may grow, not something written to be  
> > used once).
> > 
> > In the case discussed, I would place this method in a module (that would  
> > just need to be required):
> > 
> > module MyUtils  
> > &nbsp;&nbsp;def self.mv\_files\_to\_subdir(files, subdir)  
> > &nbsp;&nbsp;&nbsp;&nbsp;files.each do |curr\_file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;..  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > or, as this is in fact a file operation, I would consider to reopen  
> > FileUtils and add the method:
> > 
> > #--  
> > module FileUtils  
> > &nbsp;&nbsp;def self.mv\_files\_to\_subdir(files, subdir)  
> > &nbsp;&nbsp;&nbsp;&nbsp;files.each do |curr\_file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_file = "./#{subdir}/#{curr\_file}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileUtils.mv(curr\_file, new\_file) unless File.exists?(new\_file)  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > fglob= %r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x  
> > all\_files = Dir.glob("\*")  
> > my\_files = all\_files.grep(fglob)
> > 
> > FileUtils.mv\_files\_to\_subdir(my\_files, 'sent')  
> > #-------
> > 
> > It is a small point (some may say 'but what's the difference with a  
> > top-level method?'), but let me know your opinion, if you have a chance
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [27 November 2007 07:42 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/13 "2007-11-27T07:42:13Z")

</div>

> Robert Klemme wrote:
> 
> \> What about this one?  
> \>  
> \> def get\_file\_names\_3  
> \> Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]  
> \> end
> 
> Robert
> 
> &nbsp;&nbsp;I had not tested case 1 in the benchmark (I dropped digit '1' in the  
> call); it turns out that it is the Find, as you had thought, to be the  
> one that causes most inefficiency; without repeating all the code, these  
> are the results, including also your last suggestion.
> 
> # uses Find, and selects files manually  
> def get\_file\_names  
> &nbsp;&nbsp;fn=  
> &nbsp;&nbsp;..  
> &nbsp;&nbsp;fn  
> end
> 
> # 1) uses Dir.glob and builds array with a loop  
> def get\_file\_names1  
> &nbsp;&nbsp;fn=  
> &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> &nbsp;&nbsp;..  
> &nbsp;&nbsp;fn  
> end
> 
> # 2) uses Dir.glob and grep  
> def get\_file\_names2  
> &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> &nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
> end

We completely forgot about Dir.entries()... That might be a tad  
faster because supposedly no globbing is going on.

> # 3) variation of solution 2  
> def get\_file\_names3  
> &nbsp;&nbsp;&nbsp;Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]  
> end
> 
> # with 30 files  
> Benchmark.bm(5) do |timer|  
> &nbsp;&nbsp;timer.report('get\_file\_names') {10\_000.times {get\_file\_names} }  
> &nbsp;&nbsp;timer.report('get\_file\_names1') {10\_000.times {get\_file\_names1} }  
> &nbsp;&nbsp;timer.report('get\_file\_names2') {10\_000.times {get\_file\_names2} }  
> &nbsp;&nbsp;timer.report('get\_file\_names3') {10\_000.times {get\_file\_names3} }  
> end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> get\_file\_names 14.640000 9.080000 23.720000 ( 23.778029)  
> get\_file\_names1 1.690000 1.200000 2.890000 ( 2.903737)  
> get\_file\_names2 1.370000 1.210000 2.580000 ( 2.581539)  
> get\_file\_names3 1.430000 3.530000 4.960000 ( 4.968951)
> 
> Solution 2) is, as we saw before, the winner: 10 times faster than the  
> original solution. But the grep only improves things by 10%; 90% of the  
> contribution comes from removing Find (as Robert had guessed)!
> 
> Regarding the last one (called solution 3) from you:  
> \> Dir["{CAL,NCPH,GOH}[0-9][0-9][0-9][0-9][0-9][0-9].xls"]
> 
> This turned out to be 2 times slower than solution 2. Checking if  
> something is between 0-9 is apparently quite slower than checking for  
> 'digit'.
> 
> In conclusion:  
> Solution 2 is the fastest; but the reason is not the grep (as I had  
> theorized), which accounts only for 10% of the improvement; the other  
> 90% comes from removing Find, as Robert had guessed.

It's an old truth: access to external memory is always an order of  
magnitude slower than access to main memory. Ergo, if you can reduce  
the number of accesses to that memory you typically achieve big  
improvements. 🙂

> Last consideration: increasing the number of files, the weight of 'grep'  
> in the improvement increases (but, enough of benchmarks for to-day :-).

Thanks for taking the time to go through this.

🙂

Kind regards

robert

> **···**
>
> 2007/11/27, Raul Parolari \<raulparolari@gmail.com\>:
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Raul\_Parolari](https://avatars.discourse-cdn.com/v4/letter/r/49beb7/32.png) [@Raul\_Parolari](https://rubytalk.org/u/Raul_Parolari)\
**Post date:** [27 November 2007 08:39 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/14 "2007-11-27T08:39:05Z")

</div>

Robert Klemme wrote:

> > &nbsp;&nbsp;all\_files = Dir.glob("\*")  
> > &nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
> > end
> 
> We completely forgot about Dir.entries()... That might be a tad  
> faster because supposedly no globbing is going on.

The solution with Dir.entries is a bit faster:

def get\_file\_names5  
&nbsp;&nbsp;&nbsp;all\_files = Dir.entries(".")  
&nbsp;&nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
end

# 30 target files present  
Benchmark.bm(5) do |timer|  
&nbsp;&nbsp;timer.report('get\_file\_names2') {10\_000.times {get\_file\_names2} }  
&nbsp;&nbsp;timer.report('get\_file\_names5') {10\_000.times {get\_file\_names5} }  
end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
get\_file\_names2 1.410000 1.260000 2.670000 ( 2.675148)  
get\_file\_names5 1.190000 1.260000 2.450000 ( 2.450649)

A 10% gain; not much, but the final winner of the marathon is: Nr 5!  
🙂

Raul

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)\
**Post date:** [27 November 2007 08:40 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/15 "2007-11-27T08:40:02Z")

</div>

Since all the matches are xsl files, the function can be faster using  
a glob for "\*.xsl". Dir.entries is slower.

$ cat test.rb && ./test.rb  
#!/usr/bin/env ruby

require 'find'  
require 'fileutils'  
require 'benchmark'

def get\_file\_names  
&nbsp;&nbsp;Dir.glob("\*.xsl").select { | e |  
&nbsp;&nbsp;&nbsp;&nbsp;e =~ /^(CAL|NCPH|GOH)\d{6}\.xsl$/  
&nbsp;&nbsp;}  
end

def get\_file\_names1  
&nbsp;&nbsp;Dir.glob("\*.xsl").grep(/^(CAL|NCPH|GOH)\d{6}\.xsl$/)  
end

def get\_file\_names2  
&nbsp;&nbsp;Dir.entries('.').select { | e |  
&nbsp;&nbsp;&nbsp;&nbsp;e =~ /^(CAL|NCPH|GOH)\d{6}\.xsl$/  
&nbsp;&nbsp;}  
end

n = 10000  
[Benchmark.bm](http://Benchmark.bm)(5) { |x|  
&nbsp;&nbsp;x.report('get\_file\_names ') { n.times { get\_file\_names } }  
&nbsp;&nbsp;x.report('get\_file\_names1') { n.times { get\_file\_names1 } }  
&nbsp;&nbsp;x.report('get\_file\_names1') { n.times { get\_file\_names2 } }  
}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
get\_file\_names 0.210000 0.330000 0.540000 ( 0.566366)  
get\_file\_names1 0.270000 0.310000 0.580000 ( 0.602565)  
get\_file\_names1 0.680000 0.310000 0.990000 ( 1.114583)

Regards,  
Jordan

---

<div class="post-metadata">

**Author:** ![Mark9](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark9](https://rubytalk.org/u/Mark9)\
**Post date:** [27 November 2007 09:55 UTC](https://rubytalk.org/t/moving-files-matching-regex/42484/16 "2007-11-27T09:55:03Z")

</div>

Excellent stuff everyone,  
Gives me much to look into!

thanks,

> **···**
>
> On Tue, 27 Nov 2007 03:39:05 -0500 Raul Parolari \<raulparolari@gmail.com\> wrote:
> 
> > Robert Klemme wrote:
> > 
> > \>\> all\_files = Dir.glob("\*")  
> > \>\> my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
> > \>\> end  
> > \>  
> > \> We completely forgot about Dir.entries()... That might be a tad  
> > \> faster because supposedly no globbing is going on.  
> > \>
> > 
> > The solution with Dir.entries is a bit faster:
> > 
> > def get\_file\_names5  
> > &nbsp;&nbsp;&nbsp;all\_files = Dir.entries(".")  
> > &nbsp;&nbsp;&nbsp;my\_files = all\_files.grep(%r{^ (CAL|NCPH|GOH) \d{6} \.xls $}x)  
> > end
> > 
> > # 30 target files present  
> > Benchmark.bm(5) do |timer|  
> > &nbsp;&nbsp;timer.report('get\_file\_names2') {10\_000.times {get\_file\_names2} }  
> > &nbsp;&nbsp;timer.report('get\_file\_names5') {10\_000.times {get\_file\_names5} }  
> > end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user system total real  
> > get\_file\_names2 1.410000 1.260000 2.670000 ( 2.675148)  
> > get\_file\_names5 1.190000 1.260000 2.450000 ( 2.450649)
> > 
> > A 10% gain; not much, but the final winner of the marathon is: Nr 5!  
> > 🙂
> > 
> > Raul
> 
> --  
> Mark
