Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
FileUtils.cp_r - works with regex?
Edouard Dantes
2/9/2009 7:50:00 AM
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
--
Posted via
http://www.ruby-...
.
2 Answers
7stud --
2/9/2009 10:18:00 AM
0
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.
--
Posted via
http://www.ruby-...
.
7stud --
2/9/2009 10:51:00 AM
0
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
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
FileUtils.cp_r - works with regex?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password