Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

retaining TIMESTAMPS

by xlim (Novice)
on Oct 18, 2002 at 04:22 UTC ( [id://206216]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I wrote a script that changes the contents of a file. However, I would like to retain the timestamp for that file.

I have tried the stat() and utime. Assigning $mtime = stat ("$ARGV")[8]and attempting to change $mtime by doing

utime $atime, $mtime, $ARGV; seems to reset the timestamp to the epoch (1969).


Edited: ~Fri Oct 18 21:32:21 2002 (GMT) by footpad: Added <code> and other HTML formatting tags, per Consideration

Replies are listed 'Best First'.
Re: retaining TIMESTAMPS
by submersible_toaster (Chaplain) on Oct 18, 2002 at 07:14 UTC
    Ok , I think you meant to write
    $mtime=(stat ( $ARGV[0] ))[9]; # mtime is 9 , atime is 8!!

    That is to say , stat the file given as the first (index 0) argument to your script, assigning the eighth element in the stat list to the variable $mtime.

    have a go with this....
    #!/usr/bin/perl -w use strict; my $mtime = (stat ( $ARGV[0] ))[9]; print "$mtime\n"; open ( F , '>>' ,$ARGV[0] ); print F 'japh'; close F; utime $mtime, $mtime, $ARGV[0]; $mtime = (stat($ARGV[0]))[9]; ## Update, this is supposed to be a 9 RE +ALLY print "$mtime\n";
    Of course , anything else modifying this file that DOESN'T reset the timestamp is going to throw an irish-screwdriver in the works.
    that snippet seems to work w/ perl5.6.1 on rh7.3
Re: retaining TIMESTAMPS
by tadman (Prior) on Oct 18, 2002 at 06:55 UTC
    I think what you mean is @ARGV not $ARGV, such as this:
    my ($file) = @ARGV; my $mtime = (stat($file))[9]; # Index 9 according to docs utime($mtime,$mtime,$file); # See @ARGV in docs
    The extra quotes around variables like "$ARGV" are mostly meaningless, and can be left out.
Re: retaining TIMESTAMPS
by greenFox (Vicar) on Oct 18, 2002 at 08:26 UTC
    I'm curious :) Why would you want to do that?

    Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth

      The script I am running on my files just do minor cosmetic changes to them. I need to keep track of the major changes made to these files and also when I made them so that I would know which is the latest and greatest. Thanks, Xlim
        Fair enough I suppose but surely a versioning system would be more suitable than relying on the date stamp? There is no reason why your script, which is already modifying content, can't update a minor number on the version at the same time.

        Life is a tale told by an idiot -- full of sound and fury, signifying nothing. William Shakespeare, Macbeth

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-15 19:10 GMT
Find Nodes?
    Voting Booth?

    No recent polls found