Reputation: 4342
06/24/2012 09:58 AM 0 Backup of deleted Report 20120622.xlk
08/20/2012 06:51 PM 28,160 deleted 9 2 Foundation Installs on March 7th.xls
08/20/2012 06:51 PM 31,232 deleted-Installations March 10.xls
08/20/2012 06:51 PM 37,821 deleted Support Process 1.0.pdf
08/20/2012 06:51 PM 57,344 deleted_Support_Process_3_18_2010 V2.vsd
Hey guy. I am writing a Perl utility for some file system analysis. I am using DIR in Windows to list out certain files. I want to grab the access date (the first column) and the file name (the last column). Many file names have space so I can't easily split on whitespace. Ideally I should split on the column separator.
Upvotes: 0
Views: 349
Reputation: 57656
The split
function has an optional third argument to define the max number of strings you split into:
my ($date, $time, $ampm, $size, $name) = split /\s+/, $input, 5;
Parsing the output of dir
isn't sensible. You can open a directory in Perl with the opendir
function and loop over the entries:
open my $directory, $dirname or die;
while(defined(my $file = readdir $directory)) {
next if -d "$dirname/$file"; # skip directories
my $mtime = (stat "$dirname/$file")[9]; # we use the mtime (last modified)
printf "%10d %s", $mtime, $string;
}
You can consult the documentation for further options of the stat
function including the array indices for access time or file size. The times are given as Unix time (seconds since 1. Jan 1970 00:00) but can be converted with localtime
to a more readable format.
Upvotes: 4
Reputation: 74292
There exists a pure Perl solution. The stat
function can be used to get the access time.
Otherwise, you could split on whitespace:
#!/usr/bin/env perl
use strict;
use warnings;
while (<DATA>) {
chomp;
my ( $date, undef, undef, undef, $name ) = split " ", $_, 5;
print "$date: $name\n";
}
__DATA__
06/24/2012 09:58 AM 0 Backup of deleted Report 20120622.xlk
08/20/2012 06:51 PM 28,160 deleted 9 2 Foundation Installs on March 7th.xls
08/20/2012 06:51 PM 31,232 deleted-Installations March 10.xls
08/20/2012 06:51 PM 37,821 deleted Support Process 1.0.pdf
08/20/2012 06:51 PM 57,344 deleted_Support_Process_3_18_2010 V2.vsd
You could use unpack
to consume data column-wise.
while (<DATA>) {
chomp;
my ( $date, $filename ) = unpack 'A10 x29 A*';
print "$date: $filename\n";
}
Upvotes: 4