Windows Command Line - Changing a file modified timestamp to an earlier datetime

10,178

Solution 1

In Powershell, this works:

(gci)[1].LastWriteTimeUtc = (New-Object datetime 2013,12,31)

Solution 2

In the end i used the windows port for Gnu:

http://gnuwin32.sourceforge.net/

This example changes the timestamp for all files in a folder:

for /f "tokens=*" %i in ('dir "C:\Data\*" /s /b') do ("GnuWin32\Touch.exe" -t 1003141400 "%i")

or in a batch script:

for /f "tokens=*" %%i in ('dir "C:\Data\*" /s /b') do ("GnuWin32\Touch.exe" -t 1003141400 "%%i")
Share:
10,178
Dan Hall
Author by

Dan Hall

Updated on December 04, 2022

Comments

  • Dan Hall
    Dan Hall over 1 year

    in windows command line i can use the following to change the timestamp of a file to the current datetime:

    copy /b filename.ext +,,
    

    what i need is a way to set the timestamp to a previous datetime (-1 day, -1 week etc)

    is this possible?

    What i typically do in linux is this:

    touch -d "$(date -R -r filename) - 2 hours" filename
    

    But i need this for windows command line, not powershell or any other alternative.

    • Anders
      Anders about 6 years
      You could do it programmatically but I don't think there are any in-box tools to do it.
    • slugster
      slugster about 6 years
      Set the time on the machine, then execute the same command.