traisjames
traisjames

Reputation: 211

Perl's retrieval of file create time incorrect

I am attempting to use perl to rename files based on the folder they are in and the time created. Files GOPR1521.MP4 and GOPR7754.MP4 were created on two different cameras at the same time and date, and I want to be able their names to indicate that. For example .../GoProTravisL/GOPR1521.mp4 created at 12:32:38 should become 123238L_GOPR1520.mp4, and GOPR7754.MP4 becomes 123239R_GOPR7754.MP4. Right now the only problem is the time stamps. I would think its a problem with being wrong timezone or hour offset, but the minutes are off too. Is there something in perl I am missing when getting time stamps? Below is the perl code, what it outputs for times for each file, and what Finder on OS X says the creation times are.

Code:

#!/usr/bin/perl


use Time::Piece;
use File::stat;
use File::Find;
use File::Basename;
use File::Spec;

@files = <$ARGV[0]/>;

find({ wanted => \&process_file, no_chdir => 1 }, @files);

sub process_file {
    my($filename, $dirs, $suffix) = fileparse($_,qr/\.[^.]*/);
    if ((-f $_) && ($filename ne "" )) {
        #print "\n\nThis is a file: $_";
        #print "\nFile: $filename";
        #print "\nDIR: $dirs";
        my(@parsedirs) = File::Spec->splitdir($dirs);

        my @strippeddirs;

        foreach my $element ( @parsedirs ) {
            push @strippeddirs, $element if defined $element and $element ne '';
        }
        $pardir = pop(@strippeddirs);
        #print "\nParse DIR: ", $pardir;
        #print "\nFile creation time: ";
        $timestamp = localtime(stat($_)->ctime)->strftime("%H%M%S"); #gives time stamp

        print $timestamp;
        $newname = $timestamp . substr($pardir,-1) ."_". $filename . $suffix;

        print "\nRename: $dirs$filename$suffix to $dirs$newname\n";
        #rename ($dirs . $filename . $suffix,$dirs . $newname) || die ( "Error in renaming: " . $! );
    } else {
        print "\n\nThis is not file: $_\n";
    }
}

Output of time stamps for each file:

/Volumes/Scratch/Raw/2016-03-21/GoProTravisL/

File: GOPR1520
File creation time: 05-55-21
File: GOPR1521
File creation time: 05-56-18
File: GOPR1522
File creation time: 05-57-44
File: GOPR1523
File creation time: 05-58-49
File: GP011520
File creation time: 05-59-53

/Volumes/Scratch/Raw/2016-03-21/GoProTravisR

File: GOPR7754
File creation time: 06-02-48
File: GOPR7755
File creation time: 06-04-19
File: GOPR7756
File creation time: 06-06-27
File: GOPR7757
File creation time: 00-06-16
File: GP017754
File creation time: 00-19-30
File: GP027754
File creation time: 00-22-20

Actual file times using ls:

MacTravis:2016-03-21 travis$ ls -lR /Volumes/Scratch/Raw/2016-03-21
total 0
drwxr-xr-x  8 travis  admin  272 Apr  9 21:25 GoProTravisL
drwxr-xr-x  9 travis  admin  306 Apr  9 21:25 GoProTravisR

/Volumes/Scratch/Raw/2016-03-21/GoProTravisL:
total 21347376
-rw-------  1 travis  admin  4001240088 Mar 21 12:04 GOPR1520.MP4
-rw-------  1 travis  admin  1447364149 Mar 21 12:31 GOPR1521.MP4
-rw-------  1 travis  admin  2140532053 Mar 21 12:45 GOPR1522.MP4
-rw-------  1 travis  admin  1649133454 Mar 21 13:00 GOPR1523.MP4
-rw-------  1 travis  admin  1691562945 Mar 21 12:21 GP011520.MP4

/Volumes/Scratch/Raw/2016-03-21/GoProTravisR:
total 31941008
-rw-------  1 travis  admin  4001129586 Mar 21 12:04 GOPR7754.MP4
-rw-------  1 travis  admin  2166255754 Mar 21 12:31 GOPR7755.MP4
-rw-------  1 travis  admin  3202301883 Mar 21 12:45 GOPR7756.MP4
-rw-------  1 travis  admin  2466803806 Mar 21 12:08 GOPR7757.MP4
-rw-------  1 travis  admin  4001257192 Mar 21 11:27 GP017754.MP4
-rw-------  1 travis  admin   516025454 Mar 21 11:29 GP027754.MP4

Upvotes: 0

Views: 101

Answers (1)

ikegami
ikegami

Reputation: 386541

ctime is the "time of last status change", which I believe is the time the inode was last modified. It is NOT the file's creation time[1]. ls lists the file modification time, so simply change from using ctime to using mtime.


  1. Historically, the time at which a file was created wasn't tracked by file systems used on unix file systems. Some newer file systems track it, but I am unsure how to access it (nor is it needed here).

Upvotes: 3

Related Questions