How can I change the timestamp on a file?

80

51

Possible Duplicate:
How to modify timestamp in a dll or exe?
Windows equivalent of the Linux command 'touch'?

How can I set the timestamp for a file via the command-line to a specific date?

My specific situation is Windows 7.

Joseph Hansen

Posted 2011-06-03T18:41:33.067

Reputation: 3 578

Question was closed 2011-06-04T06:11:06.117

Duplicate of Windows equivalent of the Linux command 'touch'?

– Scott – 2015-11-29T01:37:57.350

1You should probably clarify your question and state that you want to choose the new timestamp. The two current answers assume you are looking for a Windows port of the Unix command touch that sets a files's timestamp to the current time. – William Jackson – 2011-06-03T20:44:36.630

I've looked through Sysinternals, and I'm pretty sure they don't have a utility for this. You should try the programs linked to from http://superuser.com/questions/135901/how-to-modify-timestamp-in-a-dll-or-exe

– William Jackson – 2011-06-03T20:50:14.230

@William Jackson Changed, thanks for that. Also, if it truly was a port of the Unix command touch, I could specify the date. http://en.wikipedia.org/wiki/Touch_(Unix). Something is wrong with the auto linking though, make sure you get both parentheses.

– Joseph Hansen – 2011-06-03T20:50:35.040

1I ... can't believe I never bothered to read man touch. You have taught me something new. – William Jackson – 2011-06-03T20:56:49.300

Answers

100

Due to William Jackson's answer, I found a similar question on Stack Overflow.

The accepted answer states to 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")

Edit

Two examples:

(This one is from the comments:) Set the last-access time for a file aaa.csv to the current time:

$(Get-Item aaa.csv).lastwritetime=$(Get-Date)

Set the creation time of a file foo.txt to November 24, 2015, at 6:00am:

$(Get-Item foo.txt).creationtime=$(Get-Date "11/24/2015 06:00 am")

Joseph Hansen

Posted 2011-06-03T18:41:33.067

Reputation: 3 578

1What if I want to do this for every file in a directory? – CodyBugstein – 2016-07-26T01:25:04.377

Search for "Powershell every file". You might find questions like these: http://stackoverflow.com/q/16804265/756329 and http://stackoverflow.com/q/181036/756329

– Joseph Hansen – 2016-07-26T14:03:29.113

4The date and time format is not as per the command, but the system wide one (so, in my system, it was dd/mm/yyy). – Martin Argerami – 2017-12-05T01:31:08.707

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

10For people that don't know much about Powershell, you have to put the filename after Get-Item. You can also omit the string after Get-Date to set the attribute to the current date/time like the default behavior of the touch command. Finally you can pass that code as an argument to the powershell command to have it just execute that from an existing batch file. Example: powershell $(Get-Item aaa.csv).lastwritetime=$(Get-Date) – sjbotha – 2013-09-04T14:03:11.003

41

See the answers to this question.

Specifically, this can be done natively with:

copy /b filename.ext +,,

This will set the timestamp to the current time.

Documentation for the copy command is on TechNet.

The commas indicate the omission of the Destination parameter.

William Jackson

Posted 2011-06-03T18:41:33.067

Reputation: 7 646

1Main problem with this is when the file is not in current directory... it will copy the file to current directory instead of "touching" it in place. – Kasey Speakman – 2017-08-09T19:12:46.407

2@KaseySpeakman - this command relies on being in the current directory. Use pushd FILEDIR, before the comand and popd after – Tydaeus – 2018-06-25T18:32:03.447

@JosephHansen This works by copying the file over itself, resulting in the file counting as having been modified "now" – Tydaeus – 2018-06-25T18:32:58.760

1FYI, "optimizations" in Vista+ means that, depending on context, the timestamp may not get updated as expected. It appears to work fine if this is the only command you run, but if your script does more, the timestamp may be left untouched. – Tydaeus – 2018-06-25T18:34:58.693

2Can you explain how that is working? What is the +,,? How do I know what date it is being set to? – Joseph Hansen – 2011-06-03T20:05:27.570

1@josmh Check the documentation link for details. The timestamp gets set to the current time when you run the command. Are you implying that you want to be able to change the timestamp to an arbritary time of your choosing? – William Jackson – 2011-06-03T20:10:11.567

8I'm not seeing how that helps me choose a new date for the file? – Joseph Hansen – 2011-06-03T20:42:00.420

25

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"

boot13

Posted 2011-06-03T18:41:33.067

Reputation: 5 551

3Also if you want to transfer time from a file to another, clonefiletime filefrom fileto works best! – JasonXA – 2015-06-26T19:42:45.817

Non-commandline, but more convenient for bulk editing: BulkFileChanger by Nirsoft

– SaAtomic – 2017-12-04T12:50:53.637

17

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

touch -t 200001310001.00 test.txt

Joseph Hansen

Posted 2011-06-03T18:41:33.067

Reputation: 3 578

@ziggy make sure you get all the zeros... – Joseph Hansen – 2015-05-27T16:50:53.283

2This worked perfectly for me touch -t 201608221400 filename to set it to 22.08.2016 14:00:00. – SebastianH – 2016-11-24T09:55:35.053

This comes back with invalid date format error. – ziggy – 2013-05-31T19:06:09.920

1

Check out the following webpage: http://www.stevemiller.net/apps/

The Win32 Console Toolbox contains a utility called 'touch' that lets you modify the times on one or more files. I believe it only works with US format times, though.

Justin Pearce

Posted 2011-06-03T18:41:33.067

Reputation: 2 712