Forum: Ruby FileUtils.cp_r - works with regex?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
25ae00f7825b9b6ddc7db1fb7b589f34?d=identicon&s=25 Edouard Dantes (edouard)
on 2009-02-09 08:51
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-09 11:18
Edouard Dantes 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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-09 11:52
7stud -- wrote:
> Edouard Dantes 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
This topic is locked and can not be replied to.