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
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)
jpluimers said
Robocopy
: the foundation of mymirror.bat
(: