Umm, from "I have a group of files in the same directory", I think you've misled some of the monks here with your use of the word recursive and see no reason why you can't just do: foreach $f (<*>) {
do stuff with $f;
}
This does assume that you are in the correct directory already.
setantae@eidosnet.co.uk|setantae|http://www.setantae.uklinux.net | [reply] [d/l] |
| [reply] |
The easiest way I can see to do it (assuming that you're
running under a Unix variant) is to write a perl program
that opens up $ARGV[0] as a file and does
appropriate processing on it, then from your command line,
find /path-to-files -exec /path-to-program {} \;
This lets the find program take care of providing all the
recursive paths. Call me a blasphemer if you want, but
I think the find command is much easier to use than
File::Find, and will be much easier to change in the future
should you need to add flags to the find. Of course, then
there's the issue of the reduced efficiency of launching
multiple copies of the perl interpreter, which if you have
many files could be an issue. It probably doesn't make much
of a difference either way, I just thought I'd provide an
additional viewpoint. | [reply] [d/l] [select] |
I just wrote a program that toes exactly what you want to do. First I opendir() the directory then I readdir() the directory into an array. Finally I used foreach $file (@files) to process each file at a time
The file I created scans a directory for mp3's and extracts their running time using MPEG::MP3Info
It doesn't recurse subdirectories but that shouldn't be too hard to impliment by checking file attributes for the directory value. (at least in Win32)
<code>
#!c:\perl\bin\perl.exe
use MPEG::MP3Info;
my $file;
my $out = '>>playlist.txt';
open (OUTFILE, $out);
# replace with the directory containing the fines you want
# does not recurse subdirectries
$directory = "D:\\";
opendir (DIRECTORY, $directory);
#read the file names into an array
@files = readdir(DIRECTORY);
#extract data from each file
foreach $file (@files)
{
#is the file an mp3
if ($file =~ /.mp3/)
{
#extract info from mp3 using MPEG::MP3Info
my $info = get_mp3info("$directory$file");
#get minutes and seconds
my $minutes = $info->{MM};
my $seconds = $info->{SS};
#if seconds = 0 to 9
if (($seconds =~ tr/0-9/0-9/) == 1)
{
#set seconds = 00 to 09 so they print correctly
$seconds = join ("", "0", $seconds);
}
#remove .mp3 extention from file name and print along with time
| [reply] [d/l] [select] |
Don't mind if I chip in myself. (plaid's approach doesn't take into account the possibility of a non-UNIX platform...)
sub process_file {
open F, shift;
# ...do stuff with F...
close F;
}
sub recurse_dir {
opendir D, shift;
while (readdir D) {
process_file ($_) if -f;
recurse_dir ($_) if -d;
}
closedir D;
}
recurse_dir ($ARGV[0]);
Two comments: first, I'm not sure if that's what the OP wanted at all. It's a rather useful idiom nonetheless. Second, this would have been a much more elegant solution had I made recurse_dir receive a subref and return a closure that recursively applied this subref to each file (as one would do in Scheme, e.g.). Unfortunately, this causes serious scope issues, especially if one chooses to use strict; | [reply] [d/l] [select] |
recurse_dir ($_) if -d;
This runs into problems when you see the directories "."
and "..", so you need to do something more like:
while (readdir D) {
if (-d) {
recurse_dir($_) unless ($_ eq '.' or $_ eq '..')
}
else {
process_file($_) if -f
}
}
| [reply] [d/l] [select] |
One minor problem with the above code.. The opendir command
is done relative to the current directory. If you were
to opendir('dir1'), you would be looking in pwd/dir1. If
you then found a directory inside of dir1 named dir2, an
opendir('dir2') would look for pwd/dir2, which is not the
desired result. To get the recursion right, the
recurse_dir would have to look something like
sub recurse_dir {
my $dir = shift();
opendir D, $dir;
while (readdir D) {
process_file ($dir/$_) if -f $dir/$_;
recurse_dir ($dir/$_) if -d _;
}
closedir D;
}
Either that, or the recurse_dir function could chdir in to
each directory and chdir .. after the while loop. | [reply] [d/l] |