perl file size calculation not working

前端 未结 2 1839
不知归路
不知归路 2021-01-07 05:06

I am trying to write a simple perl script that will iterate through the regular files in a directory and calculate the total size of all the files put together. However, I a

相关标签:
2条回答
  • 2021-01-07 05:34

    If it isn't just your typo -s _ instead of the correct -s $_ then please remember that readdir returns file names relative to the directory you've opened with opendir. The proper way would be something like

    my $base_dir = '/path/to/somewhere';
    opendir DH, $base_dir or die;
    while ($_ = readdir DH) {
      print "size of $_: " . (-s "$base_dir/$_") . "\n";
    }
    closedir DH;
    

    You could also take a look at the core module IO::Dir which offers a tie way of accessing both the file names and the attributes in a simpler manner.

    0 讨论(0)
  • 2021-01-07 05:38

    You have a typo:

    $cursize = -s _;
    

    Should be:

    $cursize = -s $_;
    
    0 讨论(0)
提交回复
热议问题