【问题标题】:Why is a directory listing not working on my Windows system?为什么目录列表在我的 Windows 系统上不起作用?
【发布时间】:2014-01-07 09:40:26
【问题描述】:

为什么下面的代码不起作用?

-d-f 在我的 Windows 机器上不起作用。

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";

【问题讨论】:

标签: perl


【解决方案1】:

检查带有路径的文件,

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

【讨论】:

    【解决方案2】:

    您应该在检查它是文件还是目录之前将 $path 添加到 $filename,因为 readdir 只返回没有路径的文件名。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-09
      • 2019-12-14
      • 2017-09-12
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多