in reply to getting files names
Use glob or opendir and readdir to get a list of filenames, then loop through the list.
For example,
my $dir = '.' my @list = glob($dir . '/*'); # <- amended per merlyn's response
will return a list of files in the current working directory. My preference is to use glob for this sort of thing. Usually, the number of files in a directory will be small (no more than a few thousand), so storing the names in an array should not be a problem. You may want to filter @list, so that it contains only the type of file you want, but this is easy using grep. Glob can process wildcards, so @list could be restricted by something likemy $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; @list = readdir($dh); closedir($dh);
Alternatively, you could call readdir in a scalar context, and process the files one at a time. The code for this would look something like:@list = glob($dir .'/A*');
my $dir = '.'; opendir(my $dh, $dir) or die "Could not open $dir because $!\n; while(readdir($dh)){ # do stuff to $_ } closedir($dh);
As I said, since the number of files in a folder tends to be fairly small, I prefer to use glob, possibly in conjunction with grep to get a list of file names, and then loop through that list.
emc
Insisting on perfect safety is for people who don't have the balls to live in the real world.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: getting files names
by merlyn (Sage) on Apr 30, 2007 at 14:47 UTC |