At 10:18 AM 11/29/2006, Dan Jablonsky wrote:
>Hi all,
>I am trying to read only files in a directory; I need
>to jump over the dot files and any subdirectories.
>Seems like a simple thing, however with
>
>opendir(DIR, $dir) || die "can't opendir $dir: $!";
>foreach my $file (readdir DIR)
>{
> next if (/^\./); # skip over dot files
> print "file name is: $file\n";
>}
>
>I get . and .. and all subdirectories.
>
>with
>
>opendir(DIR, $dir) || die "can't opendir $dir: $!";
>foreach my $file (readdir DIR)
>{
> next if -d $file); # skip over directories
> print "file name is: $file\n";
>}
>
>I skip the dot files but I still get the
>subdirectories. Any idea how do I get only the plain
>files?
Problem with case 1, I think, is that "next if (/^\./);" compares to
$_, not $file. Should be:
foreach my $file (readdir(DIR)) {
next if ($file =~ /^\./);
}
Problem with case 2, I think, is that you need to provide a full path
name to -d:
foreach my $file (readdir(DIR)) {
next if (-d "$dir\\$file");
}
Keep in mind that I'm using \ as a separator, which should be / for Unix/Linux.
Also, -d $file might work if you know that the current directory is
$dir, but it seems
safer to me to just give a full path name.
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs