Get current date and time within a loop
Asked Answered
G

2

6

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?

Gamages answered 28/6, 2015 at 12:30 Comment(6)
... another one. See the delayed expansion trapRhizogenic
Thanks very much. But its not clear to me, in the line above, what to change from %% or % to !.Gamages
%%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 hereRhizogenic
Be careful because this will probably not work with Windows in a different locale (where date and time are formatted differently)Sharpset
I have had success using dbenham's getTimeStamp.bat script. #21034854Mancino
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?Cosmetic
O
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
Oat answered 15/12, 2020 at 3:1 Comment(5)
Remove " | " and you wish!Oat
I do test my code But I see what I missedOat
it doesn't. Remove the | or escape it: ^|Rhizogenic
what i mean is just do this echo date:%Date% Time:%time%Oat
then edit your answer accordingly. Non-working answers tend to be downvoted and closed.Rhizogenic
S
0

Trick is using delayedExpansion and !time! instead of %time%. Practical example that lists files in current folder, one per second showing timestamp:

@echo off
setlocal enableextensions enabledelayedexpansion & REM read vars at exec with !varNm!
rem List of files sorted by name
for /f "usebackq delims=" %%x in (`dir "*.*" /b /on /a-d`) do (
  rem Replace trailing space before 10AM hours
  set dateTime=!date!/!time: =0!
  rem Delims ., will separate seconds from its hundreds
  for /f "tokens=1,2,3,4,5,6,7 usebackq delims=:/-,. " %%a in ('!dateTime!') do (
    rem Local date format DD/MM/YYYY, change next line for other cases
    rem Show date-time filename
    echo %%c%%b%%a-%%d%%e%%f  %%x
  )
  rem wait 1 second aprox
  ping 10.0.0.201 -n 1 -w 1000 >nul
)

Better approach local independent using Stephan's answer: Batch command date and time in file name

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "usebackq delims=" %%x in (`dir "*.*" /b /on /a-d`) do (
  for /f "tokens=2 delims==.," %%I in ('wmic os get localdatetime /format:list') do set dateTime=%%I
  echo !datetime:~2,6!-!datetime:~8,6!  %%x
  ping 10.0.0.201 -n 1 -w 1000 >nul
)
Saki answered 1/9 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.