Search code examples
windowsbatch-filesystemtimefile-attributesfiletime

Can a batch file change the system date; save file with attributes; change date back to current date?


Can a batch file change the system date; save file with attributes; change date back to current date?

Goal to save MYFILE.TXT with the date of 01-01-2010

using Batch commands.

I have tried to set date=01-01-2010

and then save the file, but it didn't work.

Is this impossible?

@echo off
rem to Run this Batch file as administrator

date 01-01-2010
echo %date%
pause
echo Hello World > test.txt

date 09-08-2010

echo %date%
pause

goto :eof

Note: If we didn't "Run as Administrator" It creates an error message of "A required privilege is not held by the client."


Solution

  • Yes, a batch file can do it, but I wouldn't recommend it. To set the date in Windows, you use the DATE command. To set the date to 01/01/2010, you would execute this command at the command prompt:

    date 01-01-2010
    

    However, you will need administrator privileges in order for that to work.

    To change the time, the command is TIME.

    You can look up both commands by using the HELP facility. i.e.

    help date
    help time
    

    It's also possible to get the current date using a batch file so that you can re-set the date after making your change. That's somewhat more complicated. Here's one way to do it.

    All that said, I agree with Jack Kelly: get a touch utility.