FileUtils.cp_r - works with regex?

Hi,

I try to copy files that match a certain pattern to another dir, i write

cp_r //downloads/bsd/i, “~/biblio/bsd/”

if i can use regex to do this what is wrong in my approach?

thanks for comments

Edouard D. wrote:

Hi,

I try to copy files that match a certain pattern to another dir, i write

cp_r //downloads/bsd/i, “~/biblio/bsd/”

if i can use regex to do this

There’s nothing in the standard library docs that suggests you can use a
regex for the src argument. The description of cp_r says:


If src is a directory, this method copies all its contents
recursively…

src can be a list of files.

It doesn’t say src can be a regex and that ruby will search your entire
hard drive for matching directories.

what is wrong in my approach?

I can’t get cp_r to work when providing multiple directories as the
source, for example:

cp_r(%w{./dir1 ./dir2}, “./test_dir”)

produces this error:

/usr/lib/ruby/1.8/fileutils.rb:475:in `mkdir’: No such file or directory

  • /blah/blah/dir1/test_dir/dir1 (Errno::ENOENT)

where dir1 is the current working directory.

A solution would be to use Dir.glob() to search your hard drive for
matching directories, for instance:

arr = Dir.glob("/downloads/bsd", File::FNM_CASEFOLD)

and then step through the array using each(), and then call cp_r() on
each directory.

7stud – wrote:

Edouard D. wrote:

Hi,

I try to copy files that match a certain pattern to another dir, i write

cp_r //downloads/bsd/i, “~/biblio/bsd/”

if i can use regex to do this

I did some more testing and FileUtils seems pretty buggy to me. For
instance, if my ruby program is in this directory:

/Users/me/2testing/dir1

and I execute these two commands:

cp_r ("/Users/me/downloads/bsd", “./test_dir”)
cp_r("/Users/me/2testing/dir1/downloads/bsd", “./test_dir”)

and the respective directories contain these files:

/Users/me/downLOads/bSd/test1.txt
/Users/me/2testing/dir1/DownloadS/BSd/test2.txt

Then I get these results:

…dir1
…test_dir
…test2.txt
…bsd
…test1.txt

According to the FileUtils docs:

If src is a directory, this method copies all its contents recursively.
If dest is a directory, copies src to +dest/src+.

So I would have expected test1.txt to be copied to the directory:

/Users/me/2testing/dir1/test_dir/Users/me/downloads/bsd

And test2.txt to be copied to the directory:

/Users/me/2testing/dir1/test_dir/Users/me/2testing/dir1/downloads/bsd