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

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Edoctoor
  • 183
  • 1
  • 3
  • 11
  • This is an exceptionally bad idea. Changing the date and time will affect every process running. It will have strange effects on scheduled tasks. Weird things will happen and they will be *very* difficult to diagnose because everything in the system makes the assumption that time progresses monotonically. Since the real goal is to make a generated file match an input, you need a utility like [touch](http://gnuwin32.sourceforge.net/packages/coreutils.htm) to just update the date of the single, generated file. – RBerteig Jun 11 '12 at 22:22

2 Answers2

5

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
  • 18,264
  • 2
  • 56
  • 81
  • 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 Sep 08 '10 at 22:05
  • 1
    This is correct answer to the problem posed by @Edoctoor, although not the answer to the exact question asked. I'd say it is the answer to the question that should have been asked. – RBerteig Jun 11 '12 at 22:19
4

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
  • 131,090
  • 20
  • 188
  • 351
  • 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 Sep 08 '10 at 22:25
  • 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 Sep 08 '10 at 23:21
  • Wow, I didn't know about for-loop tokenisation. `cmd.exe` never ceases to amaze. – Jack Kelly Sep 09 '10 at 00:00
  • 2
    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 Sep 09 '10 at 19:30
  • 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 Sep 10 '10 at 02:34
  • 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 Sep 10 '10 at 03:10
  • Do you know why the test.txt file doesn't get created when run WITH administrator privileges? – Edoctoor Sep 10 '10 at 16:47