简体   繁体   中英

Why is a directory listing not working on my Windows system?

Why is the below code is not working?

-d and -f are not functioning on my Windows machine.

use strict;
use warnings;

use Data::Dump qw/pp/;

my $path="C:/perl/workspace";
opendir ( DIR, $path ) || die "Error in opening dir $path\n";

my (@file,@dir);

while (my $filename=readdir(DIR)) {
    next if ($filename =~ m/^\./);
    if (-f $filename) {
        push(@file,$filename);
    } elsif (-d $filename){
        push(@dir,$filename);
    }
}

#pp \@file,\@dir;
print "@dir";

检查带有路径的文件,

if (-f "$path/$filename")

You should add the $path to the $filename before checking if it's a file or directory as readdir only returns the filenames without the path.

 ...
 if (-f $path."/".$filename) {
    push(@file,$filename);
  } elsif (-d $path."/".$filename){
    push(@dir,$filename);
  }
 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM