5

The following will print the current data and time within a windows batch file:

for /f "tokens=2,3,4,5,6 usebackq delims=:/ " %%a in ('%date% %time%') do echo %%c-%%a-%%b %%d%%e

I found this here.

However, if placed within a loop, the initial data and time will get continually printed, and will not be updated to reflect the new time.

How can it be modified to reflect the current time and work within a loop?

Thanks

Community
  • 1
  • 1
user1052610
  • 4,440
  • 13
  • 50
  • 101
  • 1
    ... another one. See the [delayed expansion trap](http://stackoverflow.com/a/30284028/2152082) – Stephan Jun 28 '15 at 13:33
  • Thanks very much. But its not clear to me, in the line above, what to change from %% or % to !. – user1052610 Jun 28 '15 at 14:46
  • `%%a`,...`%%e` are your `for`-variables. Don't change them. Use `!time!` instead of `%time%` (and `!date!`instead of `%date%` - just in case, your script runs about midnight ;)). But you will find a better way for Date-Time-string [here](http://stackoverflow.com/a/18024049/2152082) – Stephan Jun 28 '15 at 14:50
  • Be careful because this will probably not work with Windows in a different locale (where date and time are formatted differently) – Marged Jun 28 '15 at 20:04
  • I have had success using dbenham's getTimeStamp.bat script. http://stackoverflow.com/questions/21034354/wmic-failure-with-concurrent-calls-on-xp-and-server-2003 – lit Jun 29 '15 at 02:22
  • never use `%date%` for getting the date. It's unreliable because it depends on the current locale and datetime setting. See [How to get Windows batch date stamp that is locale independent?](https://stackoverflow.com/q/56165593/995714) – phuclv Dec 13 '20 at 04:47

1 Answers1

0

This is to be placed in a batch file or the code will not work.

@echo off

:loop

echo date: %date% | time: %time%

goto loop