Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Getting the correct ctime for a file on Windows 10

by Peppe757 (Initiate)
on Jul 05, 2018 at 18:20 UTC ( [id://1217973]=perlquestion: print w/replies, xml ) Need Help??

Peppe757 has asked for the wisdom of the Perl Monks concerning the following question:

I have a perl script on my Windows 10 machine that renames JPG files with the ctime of the file. I am using Strawberry Perl.

In my script, I use the stat function to get the file stats, but the ctime is not always correct.

For example, for one of my files the script returns the following values (formatted as yyyymmdd_hhmmss):

atime = 20180628_213727

mtime = 20180623_222846

ctime = 20180628_213727

However, when I look at the file information in the Windows 10 window manager, the datetime shown is "5/27/2018 8:33 PM", which I know to be the correct date when the photo was taken.

For the vast majority of my files, the stat function returns the correct ctime, but for a few files, the ctime value has somehow become corrupted; but even in these cases, the window manager still shows the correct date, so it must be available somewhere in the file.

My question is, How can I access from my perl script the file datetime displayed by the window manager?

Also, I thought about using the system function with the DOS "dir /T C" command, but when I checked in a Command Prompt window, it returns the same value as the perl stat function.

Thanks.

  • Comment on Getting the correct ctime for a file on Windows 10

Replies are listed 'Best First'.
Re: Getting the correct ctime for a file on Windows 10
by Corion (Patriarch) on Jul 05, 2018 at 18:30 UTC

    If the timestamp and the information differ, maybe they come from different sources?

    Have you looked at the EXIF information in the image? Image::ExifTool (or maybe Image::EXIF) maybe can give you the "real" time when the photo was taken, while stat and cmd.exe can only know about the time when the file was created.

      Thank you for the quick response. The Image::ExifTool package is what I was looking for. This helps a lot!
Re: Getting the correct ctime for a file on Windows 10
by Veltro (Hermit) on Jul 05, 2018 at 20:24 UTC

    Hello Peppe757,

    I've found another discussion regards this topic here. In this article there is an interesting link to Windows file times.

    So as it seems ctime does not give what you want.

    In the 'Windows file times' link the function GetFileTime is mentioned so looking for that I found the module Win32API::File::Time that has this function. So maybe you can try to use that one instead.

    edit: I found another module that I actually like better. The module is called Win32::UTCFileTime. I have created the following sample program for testing:

    use strict ; use warnings ; use DateTime ; use Win32::UTCFileTime qw(alt_stat $ErrStr) ; my $file = ".\\testFileTime2.pl" ; my @stats = alt_stat( $file ) or die "alt_stat() failed: $ErrStr\n" ; print "ctime = $stats[10]\n" ; my $dt = DateTime->from_epoch( epoch => $stats[10], time_zone => 'UTC' + ) ; print $dt->datetime . "\n" ; __END__ ctime = 1530825924 2018-07-05T21:25:24

    edit 2: Anonymous Monk asked: "But is it any different from built-in stat". The answer can be found in the documentation: Note that the 11th element of the 13-element list returned by stat() is the creation time on Win32, not the inode change time as it is on many other operating systems. Therefore, neither Perl's built-in utime() function nor this replacement function set that value to the current time as would happen on other operating systems.

      But is it any different from built-in stat?
      Thank you for the response. The Image:ExifTool package is what I was looking for. I didn't realize that image files had their own set of metadata, including a timestamp which is different from the file datetime information returned by the stat function.

      The answer can be found in the documentation

      No, the answer can only be found by adding built-in stat usage to your program, and comparing

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217973]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-24 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found