Recursive directory listing

J

Jesper

Hello,


I've made a small script that gets username + password from a form, and
uses them to log into an ftp-account, where it then search for specific
files (*.mp3, for example).

I read somewhere (online, not in the Pickaxe which is out of reach at
the moment), that "**/*.mp3" would search through all levels, looking
for *.mp3 files... but that doesn't seem to be the case. My (pig-ugly)
code at the moment looks something like:

lines = ftp.nlst("*.mp3 **/*.mp3 ***/*.mp3 ****/*.mp3")

It gets very bloated when I want to find many different extensions many
(all) levels down, surely there is some better way to do this? Any hints
of references would be appreciated.


Regards,

/ J
 
V

vasudevram

Hello,

I've made a small script that gets username + password from a form, and
uses them to log into an ftp-account, where it then search for specific
files (*.mp3, for example).

I read somewhere (online, not in the Pickaxe which is out of reach at
the moment), that "**/*.mp3" would search through all levels, looking
for *.mp3 files... but that doesn't seem to be the case. My (pig-ugly)
code at the moment looks something like:

lines = ftp.nlst("*.mp3 **/*.mp3 ***/*.mp3 ****/*.mp3")

It gets very bloated when I want to find many different extensions many
(all) levels down, surely there is some better way to do this? Any hints
of references would be appreciated.

Regards,

/ J

You could try to implement the file-tree walk function which is
available in C libraries that come with the free C compilers like GCC;
if I remember right, its called ftw(). Or an easier option could be to
look at the source for the similar function/method of Python (I think
there is one, since I was reading this in a Python book lately) and
port it to Ruby. (I think the Python version even supports the prune
option as in the UNIX find command). I don't think it should be too
difficult and, though doing so would be an additional investment of
time, its likely to stand you (and others, if you release the code for
it) in good stead later as well, as walking a file tree is a common
and generic need. If you're doing it, try to let it support blocks, so
as to be more Rubyish (and useful) :)

Maybe I'll try to do it myself when I get some free time ...

My 2c ...

Vasudev Ram
Dancing Bison Enterprises
www.dancingbison.com
 
V

vasudevram

You could try to implement the file-tree walk function which is
available in C libraries that come with the free C compilers like GCC;
if I remember right, its called ftw(). Or an easier option could be to
look at the source for the similar function/method of Python (I think
there is one, since I was reading this in a Python book lately) and
port it to Ruby. (I think the Python version even supports the prune
option as in the UNIX find command). I don't think it should be too
difficult and, though doing so would be an additional investment of
time, its likely to stand you (and others, if you release the code for
it) in good stead later as well, as walking a file tree is a common
and generic need. If you're doing it, try to let it support blocks, so
as to be more Rubyish (and useful) :)

Maybe I'll try to do it myself when I get some free time ...

My 2c ...

Vasudev Ram
Dancing Bison Enterpriseswww.dancingbison.com- Hide quoted text -

- Show quoted text -

P.S.: Forgot to mention that what I've suggested is the same as what
Peter Seebach said in his first reply to your post - a file tree walk
does just what he said.

The C ftw() function, if I remember, supports the ability to pass a
function pointer to it, via which the function pointed to, gets called
for each entry in the tree (recursively). Providing the ability to
pass a block that can do the same (in your Ruby version, if you do
write one), might be a good idea ...

- Vasudev
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top