为什么我尝试在Windows上使用Perl的stat函数失败?

时间:2015-05-14 22:30:58

标签: perl

我无法弄清楚如何重新打开上一个问题。这里是参考。  Using Perl stat function on Windows

我使用了你的第二个建议,它适用于path-file-name中没有空格的文件。但是我工作的很多人都有空格。 我的搜索表明该字符串应在双引号内以解决此问题。我已经尝试过各种各样的结果。

此代码读取包含路径文件名条目列表的文件。我读了每一个并尝试在完整条目上运行stat。存在具有不同大小和日期属性的重复条目。这是因为条目位于不同的驱动器上。我想将path-file-name,size,date属性输出到另一个文件。然后我会使用一个排序函数来输出一个按日期和大小的完整路径排序的列表,以便在评估列表后删除不需要的条目。

这是用于读取path-file-name条目并尝试执行stat函数的代码。

my $record = 0;
my $time   = 0;
my $size   = 0; 
my $quote   = '"';              # single quote, double quote, single quote

print "quote = $quote \n";
                                # read lines from listing file
open (ALL_, "<", $filename);    # open my file list for reading
while (defined($_ = <ALL_>))    # while(my $line = <$
{   chomp;                      # remove newline "/n" 
    print " Normal value = $_\n"; # status output
    $record = $_;
    $record =~ s/\\/\//g;       # change \ to /
    $record = "\"$record\"";    # add single double quote to start and end of record
#   $record = $quote . $record . $quote;
    print " now = $record \n";

#   use lib '/home/foobar/code';  ?? add dbl quotes
#   use My::Module;

    # Using File::stat, which replaces stat with a function with a friendlier interface:
    use File::stat qw( stat );
    use POSIX      qw( strftime );
#   my $qfn = $quote . $_ . $quote;  # also adds double quote
    my $qfn = $record; 
    my $stat = stat($qfn)   or die("Can't stat \"$qfn\": $!\n");

    printf("File %s: size=%s modified=%s\n",
        $qfn,
        $stat->size,
        strftime("%Y-%m-%d %H:%M:%S", localtime($stat->mtime)), 
    );
}

这是

Normal value = N:\dir1\dir2\dir3\file1
now          = "N:/dir1/dir2/dir3/file1"
Can't stat ""N:\dir1\dir2\dir3\file1"": No such file or directory

因为或死而退出(不能是stat \“qfn \”:$!\ n“);

如果我注释掉行

$record = "\"$record\"";  

取消我得到的双引号:

normal value = N:\dir1\dir2\dir3file1
now = N:/dir1/dir2/dir3/file1
file N:/dir1/dir2/dir3/file1: size=280580 modified=20022-12-13 09:41:28
normal value = C:\dir01\dir 02\dir03\file 2
now = C:/dir01/dir 02/dir03/file 2
Can't stat "C:/dir1/dir 02/dir03/file2": No such file or directory

因为或死而退出(不能是stat \“qfn \”:$!\ n“);

有没有关于stat的不同之处在于它以不同方式处理双引号,以至于它仍然可以看到空格?

如果我能正确执行此操作: 有没有办法允许或die()提供一条指示失败的消息,并继续处理那个在处理过程中被删除的文件?

1 个答案:

答案 0 :(得分:0)

您所说的内容似乎与我在Windows上对路径等所知的任何内容保持一致。这是一个示例程序,用于统计一组名称中带有空格的文件:

#!/usr/bin/env perl

use strict;
use warnings;
use File::Spec::Functions qw(catdir catpath rootdir);

use File::Find;

my %number_of = (
    files_with_spaces_for_which_stat_was_successful => 0,
    files_with_spaces_seen => 0,
);

my $top = catpath('C:', catdir(rootdir(), 'Program Files', 'Internet Explorer'));
print "calling stat for every file under '$top'\n";

find({
    no_chdir => 1,
    wanted => sub {
        next unless /\s/;
        $number_of{files_with_spaces_seen} += 1;
        print "$_: ";
        if (stat) {
            $number_of{files_with_spaces_for_which_stat_was_successful} += 1;
            print "+\n";
        }
        else {
            print "-\n";
        }
    },
}, $top);

use YAML::XS;
print Dump \%number_of;

输出:

C:\> perl t.pl
...
---
files_with_spaces_for_which_stat_was_successful: 171
files_with_spaces_seen: 171