.bat file
FOR %%A in (*.css) DO (
@echo %date% %time% >>log.txt
pause
)
writes always the same time to log file. It looks like %time% is evaluated only once.
How to write real time to log file ? Using Windows 10
.bat file
FOR %%A in (*.css) DO (
@echo %date% %time% >>log.txt
pause
)
writes always the same time to log file. It looks like %time% is evaluated only once.
How to write real time to log file ? Using Windows 10
To see why the code is behaving like this, you need to remove the "@" in front of the echo. The code that gets executed is for example:
FOR %A in (*.css) DO (
echo 30.03.2020 19:20:57,99 1>>log.txt
pause
)
because everything in the brackets () is treated as one line, so the variables date and time are replaced by their current values before the loop gets executed.
To get an unique timestamp for every file, you need to delay the expansion of the variables, which can be done in many different ways:
FOR %%A in (*.css) DO (
call echo %%date%% %%time%% >>log.txt
pause
)
the code executed will then be like the one you initially wanted to write:
FOR %A in (*.css) DO (
call echo %date% %time% 1>>log.txt
pause
)
Another, similar possibility is to use call to execute code below a label outside of the loop:
FOR %%A in (*.css) DO (
call :printLogEntry
pause
)
exit
:printLogEntry
echo %date% %time% >>log.txt
exit /b
And of course you can use delayedExpansion:
setlocal enableDelayedExpansion
FOR %%A in (*.css) DO (
echo !date! !time! >>log.txt
pause
)