Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Getting modified date for a file (Win32)

by ciryon (Sexton)
on Aug 18, 2004 at 09:26 UTC ( [id://383902]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I'm trying to get a script to work in win32 environment and need some kind of equivalent for:

return (stat($file))[9];

which returns the modified date for a file but doesn't seem to work here since all dates look like "1970-01-01 00:00:00".

Grateful for help.

Update:

The stat function seems to work, at least when I try it with Active Perl. I forgot to mention that I'm using mod_perl. I also tried Corion's suggestion of enabling warnings, but nothing shows up.

Update II: I think I found the problem. It reports correct dates on files, but not on directories.

Replies are listed 'Best First'.
Re: Getting modified date for a file (Win32)
by maa (Pilgrim) on Aug 18, 2004 at 09:35 UTC

    I use that on NT4 all the time... are you using UNC filenames or a mapped drive? (UNC works). Try a one-liner...

    C:\rexec>echo > file.txt
    C:\rexec>perl -e "print scalar localtime((stat('file.txt'))[9])"
    Wed Aug 18 10:33:50 2004
    
Re: Getting modified date for a file (Win32)
by Corion (Patriarch) on Aug 18, 2004 at 10:30 UTC

    Most likely, you are running without warnings and are not error-checking your results. My guess is, as you don't show how $file gets filled, that a file with the name in $file does not exist.

    You will need to modify your code to track down this error, preferrably as follows:

    use strict; use warnings; use Carp qw( carp ); sub get_mtime { my ($file) = @_; if (! -e $file) { carp "There exists no such file as '$file'"; return undef; }; if (! -f $file) { carp "There exists '$file' but it isn't a file."; return undef; }; my $result = (stat $file)[9]; carp $! if $!; $result; }; my @files = qw( c:\\winnt\\cmd.exe c:\\winnt\\system32\\cmd.exe c:\\winnt ); for my $f (@files) { printf "'%s' has the mtime %s\n", $f, (get_mtime($f) || 'n/a'); };

    While that code is overly verbose, it should help you much with diagnosing the real cause of your error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (None)
    As of 2024-04-25 01:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found