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

utime of dirs on win32

by belg4mit (Prior)
on Feb 05, 2007 at 10:40 UTC ( [id://598263]=perlquestion: print w/replies, xml ) Need Help??

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

Does anyone know how I can change the mtime of a folder from within perl on win32? I'd really prefer to not have to use an external binary (I have lots of files to process), and utime doesn't work on folders (with or without -U) but files are fine.

The platform supports it:

G:\>mkdir foo

G:\>stat foo
  File: `foo'
  Size: 0               Blocks: 0          IO Block: 1024   directory
Device: 435c336ch/1130115948d   Inode: 12243914462851298397  Links: 2
Access: (0755/drwxr-xr-x)  Uid: (  400/ jpierce)   Gid: (  401/mkpasswd)
Access: 2007-02-05 00:00:00.000000000 -0500
Modify: 2007-02-05 05:29:16.000000000 -0500
Change: 2007-02-05 05:29:16.000000000 -0500

G:\>touch --date "July 14, 2000" foo

G:\>stat foo
  File: `foo'
  Size: 0               Blocks: 0          IO Block: 1024   directory
Device: 435c336ch/1130115948d   Inode: 12243914462851298397  Links: 2
Access: (0755/drwxr-xr-x)  Uid: (  400/ jpierce)   Gid: (  401/mkpasswd)
Access: 2000-07-13 01:00:00.000000000 -0400
Modify: 2000-07-14 00:00:00.000000000 -0400
Change: 2000-07-14 00:00:00.000000000 -0400
But no joy:
G:\>perl -e "utime(undef, 1156817846, 'foo') #Mon Aug 28 22:17:26 2006"

G:\>stat foo
  File: `foo'
  Size: 0               Blocks: 0          IO Block: 1024   directory
Device: 435c336ch/1130115948d   Inode: 12243914462851298397  Links: 2
Access: (0755/drwxr-xr-x)  Uid: (  400/ jpierce)   Gid: (  401/mkpasswd)
Access: 2007-02-05 00:00:00.000000000 -0500
Modify: 2000-07-14 00:00:00.000000000 -0400
Change: 2000-07-14 00:00:00.000000000 -0400
This appears to be Siemens perl 5.8.0.

--
In Bob We Trust, All Others Bring Data.

Replies are listed 'Best First'.
Re: utime of dirs on win32
by ferreira (Chaplain) on Feb 05, 2007 at 10:55 UTC

    You have been biten by the last paragraph of utime documentation:

    Note that only passing one of the first two elements as "undef" will be equivalent of passing it as 0 and will not have the same effect as described when they are both "undef". This case will also trigger an uninitialized warning.

    and you may check this is so by using -w:

    G:\>perl -w -e "utime(undef, 1156817846, 'foo') # Mon Aug 28 22:17:26 2006" Use of uninitialized value in utime at -e line 1.

    And the following seems to do something more similar to what you want:

    G:\>perl -w -e "utime(-A 'foo', 1156817846, 'foo') #Mon Aug 28 22:17:2 +6 2006"

    Updates:

    1. as noted by belg4mit, utime docs were updated at 5.8.1 (as can be seen here: 5.8.1 utime vs 5.8.0 utime).
    2. Windows has weird ideas about tracking access and modification times.

      Interesting, must be newer dox. Mine just say:
      Changes the access and modification times on each file of a list of files. The first two elements of the list must be the NUMERICAL access and modification times, in that order. Returns the number of files successfully changed. The inode change time of each file is set to the current time. This code has the same effect as the "touch" command if the files already exist:
      #!/usr/bin/perl $now = time; utime $now, $now, @ARGV;
      If the first two elements of the list are "undef", then the utime(2) function in the C library will be called with a null second argument. On most systems, this will set the file's access and modification times to the current time. (i.e. equivalent to the example above.)
      utime undef, undef, @ARGV;
      In any event, this did not address the problem:
      G:\>perl -we "utime(1, 1156817846, 'foo')"
      
      G:\>stat foo
        File: `foo'
        Size: 0               Blocks: 0          IO Block: 1024   directory
      Device: 435c336ch/1130115948d   Inode: 12243914462851298397  Links: 2
      Access: (0755/drwxr-xr-x)  Uid: (  400/ jpierce)   Gid: (  401/mkpasswd)
      Access: 2007-02-05 00:00:00.000000000 -0500
      Modify: 2000-07-14 00:00:00.000000000 -0400
      Change: 2000-07-14 00:00:00.000000000 -0400
      Did you test on a directory, which was what the question was specifically about?

      UPDATE: Sorry, I stat'ed the wrong file when checking the new code; it doesn't help that windows only seems to track the date for access time. I suppose this is another argument for remembering to check warnings. Time for bed now,

      --
      In Bob We Trust, All Others Bring Data.

Re: utime of dirs on win32
by bingos (Vicar) on Feb 05, 2007 at 13:24 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found