Why does my attempt to use Perl's stat function on Windows fail? -
i not figure out how reopen previous question. here reference. using perl stat function on windows
i used second suggestion , worked files no spaces in path-file-name. many of ones working have spaces included. searching indicates string should within double quotes resolve problem. have made few attempts various outcomes.
this code reads file contains list of path-file-name entries. read each 1 in , try run stat on full entry. there duplicate entries different size , date attributes. due entries being on separate drives. want output path-file-name, size, date attributes file. use sort function put out list sorted full path date , size allow deletion of unneeded entries after evaluating list.
this code reading path-file-name entries , trying perform stat function.
my $record = 0; $time = 0; $size = 0; $quote = '"'; # single quote, double quote, single quote print "quote = $quote \n"; # read lines listing file open (all_, "<", $filename); # open file list reading while (defined($_ = <all_>)) # while(my $line = <$ { chomp; # remove newline "/n" print " normal value = $_\n"; # status output $record = $_; $record =~ s/\\/\//g; # change \ / $record = "\"$record\""; # add single double quote start , end of record # $record = $quote . $record . $quote; print " = $record \n"; # use lib '/home/foobar/code'; ?? add dbl quotes # use my::module; # using file::stat, replaces stat function friendlier interface: use file::stat qw( stat ); use posix qw( strftime ); # $qfn = $quote . $_ . $quote; # adds double quote $qfn = $record; $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)), ); }
this gets
normal value = n:\dir1\dir2\dir3\file1 = "n:/dir1/dir2/dir3/file1" can't stat ""n:\dir1\dir2\dir3\file1"": no such file or directory
quits because of or die(can't stat \"qfn\": $!\n");
if comment out line
$record = "\"$record\"";
to leave off double quotes get:
normal value = n:\dir1\dir2\dir3file1 = 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 = c:/dir01/dir 02/dir03/file 2 can't stat "c:/dir1/dir 02/dir03/file2": no such file or directory
quits because of or die(can't stat \"qfn\": $!\n");
is there different stat handles double quotes differently such still sees spaces?
if can executing correctly: there way allow or die() provide message indicates failure , goes on processing 1 file somehow got deleted during processing?
none of saying seems consistent know paths etc on windows. here sample program stats bunch of files spaces in names:
#!/usr/bin/env perl use strict; use warnings; use file::spec::functions qw(catdir catpath rootdir); use file::find; %number_of = ( files_with_spaces_for_which_stat_was_successful => 0, files_with_spaces_seen => 0, ); $top = catpath('c:', catdir(rootdir(), 'program files', 'internet explorer')); print "calling stat 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;
output:
c:\> perl t.pl ... --- files_with_spaces_for_which_stat_was_successful: 171 files_with_spaces_seen: 171
Comments
Post a Comment