Creating a file name as a timestamp in a batch job

CP source.log %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%.log

But it’s locale dependent. I’m not sure if %DATE% is localized, or depends on the format specified for the short date in Windows.

Here is a locale-independent way to extract the current date from this answer, but it depends on WMIC and FOR /F:

FOR /F %%A IN ('WMIC OS GET LocalDateTime ^| FINDSTR \.') DO @SET B=%%A
CP source.log %B:~0,4%-%B:~4,2%-%B:~6,2%.log

Leave a Comment