(tye)Re: Reading specific files from a directory
by tye (Sage) on Mar 12, 2001 at 08:46 UTC
|
First stab:
for my $file ( glob( "$somedir/example*.txt" ) ) {
But if you also have files like "examplebroken.txt", then you'll find too many. So you can refine that. I'll assume you have things like "example3.3.1.2.txt" but not "example-2.45e11.txt":
for my $file (
grep m#/example(\d+\.)+txt$#,
glob( "$somedir/example*.txt" )
) {
But I'd stick with your code if you are on an old version of Perl where glob is the sucking death (that is, where it calls /bin/csh to do its work).
If you are using leading-edge Perl, then you can use File::Spec instead of blithely pasting $somedir and exmample*.txt together with a "/".
-
tye
(but my friends call me "Tye") | [reply] [d/l] [select] |
(sacked) Re: Reading specific files from a directory
by sacked (Hermit) on Mar 12, 2001 at 08:45 UTC
|
You can check the filename with a grep:
opendir(DIR, $somedir) or die "can't open $somedir for read: $!\n";
@files_i_want = grep { /^example\d(?:\.\d)*\.txt$/ } readdir DIR;
closedir(DIR) or die "error closing $somedir: $!\n";
Then use @files_i_want as desired.
--sacked | [reply] [d/l] [select] |
Re: Reading specific files from a directory
by AgentM (Curate) on Mar 12, 2001 at 08:45 UTC
|
| [reply] [d/l] |
|
|
| [reply] |
Re: Reading specific files from a directory
by Tuna (Friar) on Mar 12, 2001 at 08:44 UTC
|
#!/usr/bin/perl -w
use strict;
my $somedir = "/home/me/lala";
my $file;
opendir (DIR, $somedir);
while ($file = readdir DIR) {
print "$file\n";
if ($file =~ /^example\.\d+\.txt$/) {
print " -- FOUND example: $file\n";
}
}
I'm not whether my code is any faster or more optimal than yours; I'll leave that for monks greater than I =) BTW, I'm not sure if it's a typo but:
open (DIR, $somedir);
should be:
opendir (DIR, $somedir);
| [reply] [d/l] [select] |
|
|
Somedays it just doesn't pay to post code (it almost always pays to write code thankfully.) Yeah, the opendir is a typo here that I made while simplifying the problem down for PM. And uh....I have no excuse for why I regenerated my file name. In a hurry combining code and not thinking hard enough. I'm gonna go back to my cave and write HTML now.
-Lexicon
| [reply] |
Re: Reading specific files from a directory
by Anonymous Monk on Mar 13, 2001 at 02:46 UTC
|
File glob'it, and watch newbies cower in AWE!
my @dirc = <*>;
foreach $dir (@dirc) {
print "$dir\n";
print " -- Found $dir\n"
if $dir =~ /example(\d*?)\.txt/;
}
Only one line shorter, but I do think is more efficient. | [reply] |
Re: Reading specific files from a directory
by Anonymous Monk on Mar 13, 2001 at 03:00 UTC
|
If you are on unix, you can findout like this in single line :
ls -l |perl -ne 'print if /example\d+\.txt/
Ashok | [reply] [d/l] |
Re: Reading specific files from a directory
by scain (Curate) on Mar 13, 2001 at 21:13 UTC
|
I just wanted to comment on using glob as some monks have proposed.
Generally, it works fine, but be careful if there are many (>1000)
files in the directory as it can be slow. If there are, it would
probably be better to user readdir and grep.
-Scott | [reply] |
|
|
| [reply] |
|
|
| [reply] |
|
|