views:

39

answers:

2

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."

+3  A: 

Your best bet is to probably grab touch from GNUWin32 and use that to change the timestamps. Doing this by changing the system date is like using a sledgehammer to crack a nut.

Jack Kelly
I have a large Batch file; however, the file that it outputs has to have the original date and time as the one that it has inputted. That said; I will check out GNUWin32 and hope that it helps me with other challenges. Thanks for your time Jack Kelly
Edoctoor
+1  A: 

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.

Jim Mischel
Thanks... I already tried set date=01-01-2010 then typed :>Date in another CMD window and the date didn't change. I am able to do all the code but for some reason I just can not set the DATE variable within a batch file. I do appreciate all the feed back, thanks Should I delete this Question?
Edoctoor
There is no DATE variable. You do not use "set date" to set the date. You enter the command "date 01-01-2010" at the command prompt.
Jim Mischel
Wow, I didn't know about for-loop tokenisation. `cmd.exe` never ceases to amaze.
Jack Kelly
Note that the linked variant to parse the date fails with quite a lot of locales as does any naïve approach to date parsing in batch. You should also note that there is no robust way of parsing dates in batch files anyway (at least I'm not aware of any – there are a few approaches that work with a large number of locales but those also fail with some).
Joey
The script above is very strange: When run normally the test.txt file is created and the time doesn't change; however, when run as admin the time changes; Yet, the test.txt file doesn't get created.. Well at least I learned how to detect if the batch is run as admin or not.. Thanks for all your Information.
Edoctoor
As I said in my answer, "you will need administrator privileges in order for that to work." Setting the system date is a privileged operation that only administrators are allowed to perform.
Jim Mischel
Do you know why the test.txt file doesn't get created when run WITH administrator privileges?
Edoctoor