The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,680 other followers

Windows: programmatically setting date/time stamps of files

Posted by jpluimers on 2014/07/01

For DOS programs, date and time stamps were used to mark versions of files. For instance, Turbo Pascal 6.0, had a 06:00 time stamp on every file.

You can still do this in Windows, but need to watch for a couple of things:

  • daylight saving time
  • more than one time stamp per file

There are various ways to do it. Besides a graphical Attribute Changer at www.petges.lu (thanks User Randolf Richardson), these are console approaches via How can I change the timestamp on a file?:

In PowerShell, thanks to users Learning josmh and it depends:

use Powershell and these commands:

$(Get-Item ).creationtime=$(Get-Date "mm/dd/yyyy hh:mm am/pm")
$(Get-Item ).lastaccesstime=$(Get-Date "mm/dd/yyyy hh:mm am/pm")
$(Get-Item ).lastwritetime=$(Get-Date "mm/dd/yyyy hh:mm am/pm")

A slight variation that is recursive and a little shorter, though less readable: ”
gci -rec | %{ $_.lastWriteTime = ($_.lastAccessTime = ($_.creationTime = (get-date "2011-09-14T07:10:00"))) }

Using the good old *nix touch command (thanks josmh) for which I totally forgot it had a -t option (this works on many platforms including a Mac with OS X):

Using Cygwin, to set the timestamp of test.txt to January 31, 2000, at 00:01.00:

touch -t 200001310001.00 test.txt

Using Nirsoft (thanks boot13):

Nirsoft to the rescue: try the freeware tool nircmd. It’s a bunch of useful tools in one small command line program. One of the commands allows you to specify either or both of created time and modified time, like this:

nircmd.exe setfiletime "c:\temp\myfile.txt" "24-06-2003 17:57:11" "22-11-2005 10:21:56"

–jeroen

2 Responses to “Windows: programmatically setting date/time stamps of files”

  1. paulbraren said

    Nice article, thank you! Big fan of trying to preserve date/time stamps when moving files across a network share, always annoying when the get scrogged. Here’s a robocopy example:
    copy D:\shares\Install to x:\ on an external USB 3.0 1TB drive
    took 11 hours for 425GB of data:

    D:\>ROBOCOPY D:\shares\Install x:\ /MIR /COPY:DAT /DCOPY:T /LOG:D:\ROBOCOPYLOG.TXT /TEE

    For plain old touch, kind of forgot about nircmd, and nice find on that graphical file changer (I’ve used others, like this one better)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: