Re: selcting all files in a given directory except......
by moritz (Cardinal) on Dec 06, 2007 at 09:07 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $dir = <STDIN>);
my @files;
if (-d $dir){
opendir (my $DIR, $dir);
@files = grep { m/^(?!\.{1,2}\z)/ } readdir($DIR);
closedir($DIR);
}
foreach my $file (@files) {
print "$file\n";
}
| [reply] [d/l] [select] |
|
names of the files are not been displayed .......
| [reply] |
|
| [reply] |
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: selcting all files in a given directory except......
by cdarke (Prior) on Dec 06, 2007 at 09:10 UTC
|
It would help if you said what the error messages are. Error messages are your friends!
Your first problem is a missing '#' on the first line. That might be a slip when pasting your code, otherwise it might be trying to execute it as a Bourne shell script. Thereafter, you will get strictness errors because you are not pre-declaring all your variables. Try this:
#!/usr/bin/perl
use strict;
use warnings;
chomp(my $dir = <STDIN>);
my @files;
if (-d $dir){
opendir (my $DIR, $dir);
readdir ($DIR); # ignore .
readdir ($DIR); # ignore ..
@files = readdir($DIR);
closedir($DIR);
}
foreach my $file (@files) {
print "$file\n";
}
| [reply] [d/l] |
|
at least for me, the opendir don't return as the first 2 values the '.' and '..'.
readdir ($DIR); # ignore .
readdir ($DIR); # ignore ..
the solution of moritz it's more robust
@files = grep {/^(?!\.{1,2}\z)/} readdir($DIR);
| [reply] [d/l] [select] |
|
@files = grep {$_ ne '.' and $_ ne '..'} readdir($D);
I know one isn't meant to write code that is easy for a monkey to understand, but all the same, I don't think the added complexity of throwing in a couple of more-rarely used regops like \z and (?!...) is worth it in this case.
This way there is no ambiguity with strange directories like "foo\n.". Also, your original solution may fail if a corrupted filesystem is rebuilt, and the parent and current directories pointers are rewritten elsewhere than positions 1 and 2 in the directory list.
If you want to go really cross-platform robust, use File::Spec's curdir() and updir() routines.
• another intruder with the mooring in the heart of the Perl
| [reply] [d/l] |
|
Not all directories have . and .. in them, at least in Windows.
| [reply] [d/l] [select] |
|
names of the files are not been printed on the terminal when script is excecuted
| [reply] |
|
#!/usr/bin/perl
use strict;
use warnings;
my $dir = $ARGV[0] || $ENV{HOME};
my @files;
if (-d $dir){
opendir (my $DIR, $dir);
@files = grep {/^(?!\.{1,2}\z)/} readdir($DIR);
closedir($DIR);
} else {
die("can't open the dir, $dir\n");
}
foreach my $file (@files) {
print "$file\n";
}
save the script as myls.pl,
and run the script like this
$ perl myls.pl /home
ps. the $ENV{HOME} exist in the windows environment?? | [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
When you ran the preceding example,
- What input did you give it?
- What was the expected output?
- What platform/perl are you running?
| [reply] |
|
|
| [reply] |
|
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|
|
Maybe the opendir is failing. Modify that line to read:
opendir (my $DIR, $dir) || die "Unable to open $dir: $!";
then try again. | [reply] [d/l] |
|
Re: selcting all files in a given directory except......
by Dawesy (Initiate) on Dec 06, 2007 at 10:11 UTC
|
I believe the GLOB function will return all files in a given directory, as this is what I use on UNIX / Solaris.
Eg: @<list> = glob(<path & wildcard for files>);
@ftest = glob(/dir1/file*.sh); | [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: selcting all files in a given directory except......
by poolpi (Hermit) on Dec 06, 2007 at 14:27 UTC
|
Beginning with Perl v5.6.0, you can use the standard File::Glob extension :
see http://perldoc.perl.org/File/Glob.html
use File::Glob ':globally';</br>
my $dir = '/my/dir';
my @files = <$dir/*>;
print "$_\n" for @files;
Hope This Helps | [reply] [d/l] |
Re: selcting all files in a given directory except......
by kyle (Abbot) on Dec 06, 2007 at 15:55 UTC
|
Instead of trying to match everything except those two names, match those two names and negate the test. Also, be sure to check for and report errors.
opendir my $dh, $dir or die "Can't opendir '$dir': $!";
my @files = grep { ! m{ \A \. \.? \z }xms } readdir $dh;
closedir $dh or die "Can't closedir? $!";
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in. |