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

O.K., I admit it may be more of an W2K question than perl, but I'm using perl to modify text file contents, and I would like to be able to restore the modification date after I am done. Is there a Windows system command I can summon within perl?
  • Comment on How to change file modification date in Windows

Replies are listed 'Best First'.
Re: How to change file modification date in Windows
by Ido (Hermit) on Nov 12, 2002 at 17:22 UTC
    In order to change the modification date of a file you should use the utime function (utime ACCESSTIME,MODIFICATIONTIME,LIST - Times in numerical format, and list is a list of files). Read more about it in perlfunc -f utime.
Re: How to change file modification date in Windows
by John M. Dlugosz (Monsignor) on Nov 12, 2002 at 17:22 UTC
    Wow, I figured as a late replier the question would have been answered already.

    The Win32 API function is SetFileTimes. Use the Win32::API module to call it. Treat the 3 timestamp parameters as 8-byte buffers and don't worry about how to manipulate them, since you'll just save the results from GetFileTimes.

    —John

(z) Re: How to change file modification date in Windows
by zigdon (Deacon) on Nov 12, 2002 at 16:11 UTC
    Why not just use the stat command?
    my $filename = "Test.txt"; print "$filename was last modified on: ", scalar localtime((stat($file +name))[9]), "\n";

    -- Dan

Re: How to change file modification date in Windows
by Thelonius (Priest) on Nov 12, 2002 at 17:16 UTC
    I'm not aware of any Windows command that does it, but if you load cygwin on your machine, you can do it with the touch command.
      Actually Borlands free compiler tools contain the touch command for win32. I think some of the MKS style tools kits do it as well. Unless you really need a different shell, you can find most GNU/Unix commands ported to the win32 cmd.exe shell without the overhead and hassle of cygwin.

      "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: How to change file modification date in Windows
by Anonymous Monk on Nov 12, 2002 at 18:49 UTC
    Thank you all!
    I can use the  statfunction to grab the time before I modify the file, and then pass that to utime to restore it after my changes. Just what I wanted to do! Thanks again!

    - dp
Re: How to change file modification date in Windows
by robartes (Priest) on Nov 12, 2002 at 16:16 UTC
    If you want to read the modification date after your mods, see zigdon's answer.

    If you want to change the date back to what it was: why do you want to cover your tracks?

    CU
    Robartes-

      These are data files that I recieve from analysts and the mod date gives me a quick read on when they did the analysis. Usually I get updated data files with the same names so my only "version control" is by the date. - dp
        In that case, use zigdon's suggestion: stat. Sorry I misunderstood your question.

        CU
        Robartes-