Re: Scanning static directory tree for files
by davorg (Chancellor) on Jun 21, 2000 at 16:50 UTC
|
| [reply] |
|
|
unless cross-platform issues are not and issue, and speed is in which case you CAN do it faster by doing it yourself. I don't remember what method turned out to be the fastest when I benchmarked them, but I do remember it wasn't File::Find, and since the requestor is asking for a script for his personal use in learning to program perl he should probably look at all the various methods, and consider their benefits and detriments for his own particular situation.
Not to rant, but I hate it when people say "If you're doing such-and-such, you should do it this way." as if there weren't a million and a half equally good ways of doing it.
I admit that I personally don't re-invent the wheel unless I have to. If there's a perfectly good module out there to do the job, I'll generally use it... but I digress.
| [reply] |
|
|
| [reply] |
|
|
|
|
OK, this is a stupid question but.....
I looked on CPAN for this File::Find module. All I see
under File is a bunch of other modules none of which are
Find. Am I just brain-challanged, or what?
Roy Alan
"I quit; I concede. Tanj on your silly game!" -- Louis Wu
| [reply] |
|
|
| [reply] |
|
|
Re: Scanning static directory tree for files
by raflach (Pilgrim) on Jun 21, 2000 at 16:48 UTC
|
To a certain extent it depends on the size and number of the directories. You could use glob to grab all of the entries in a directory. i.e.
@files = glob("*.mp3");
That would get you a list of mp3s in the current directory. To transverse directories, you would want something like...
while (glob("*")) {
if ( ! ( $_ =~ /^\./ ) ) {
if ( -f $_ ) {
#process files in this directory.
} elsif ( -d $_ ) {
#process a directory possibly with a recursive call
}
}
}
By putting the above in a function and calling it recursively, you could actually scan down the entire tree.
This of course can also be done with opendir etc. which is what you are doing I assume.
Most of the various possibilities for this scenario were fairly well fleshed out in another thread on perlmonks... Can't seem to find it at the moment though, so someone else will have to give you the link. | [reply] [d/l] [select] |
(jcwren) Re: Scanning static directory tree for files
by jcwren (Prior) on Jun 21, 2000 at 16:49 UTC
|
Shandor,
This code fragment that I had previously posted to a note about searching directories may help you. It was, in fact, for searching for MP3s in a directory tree.
--Chris | [reply] |
Re: Scanning static directory tree for files
by jjhorner (Hermit) on Jun 21, 2000 at 16:52 UTC
|
Be very careful that merlyn doesn't see this.
He gets very, very touchy about recursive searches and
possible infinite loops.
Look into File::Find. It's amazing.
J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/
| [reply] |
|
|
um... Could you explain why I should care if merlyn gets touchy? Again, not to rant but.
Just because he's virtually a perl God?....
I don't buy that as a reason. It's a good reason to listen to him when he rants, but not a good reason for avoiding the exploration of ideas.
Recursion is sometimes a useful programming tool. If we never use recursion just because of the danger of infinite loops and merlyn's touchiness, we will never learn how to effectively program recursions that aviod infinite loops.
Finally merlyn has not appeared as touchy to me as he has to you. Full of advice, and willing to warn someone of the dangers of a particular approach, yes... touchy no.
Quit to tout the benefits of prebuilt cross-platform-friendly modules, yes... Touchy no.
So, how about a little room to flex our brains and try out that recursive loop every once in a while... We know where our CTRL+ALT+Delete keys and our power buttons are.
| [reply] |
|
|
J. J. Horner
Linux, Perl, Apache, Stronghold, Unix
jhorner@knoxlug.org http://www.knoxlug.org/
| [reply] |
|
|
RE: Scanning static directory tree for files
by jettero (Monsignor) on Jul 25, 2000 at 21:03 UTC
|
At first I thought your question was exactly what I wanted.
... Nobody actually adressed the "random" in the first line.
I know it wasn't exactly part of the actual question, but...
I wrote this:
bash$ echo install File::KGlob | sudo perl -MCPAN -e shell
bash$ perl -e 'use File::KGlob qw /kglob/; \
> @files = kglob("/mp3/*/*"); \
> while(@files) { $i = int(rand @files); \
> `mpg123 $files[$i]`; \
> @files = (@files[0..$i-1], @files[$i+1..$#files]); }';
'course, your directory structure has artist too... mine
doesn't. So you'd need that kglob to have another '*' in
it. | [reply] [d/l] |