首页 > 代码库 > How to generate file name according to datetime in bat command

How to generate file name according to datetime in bat command

On the command line, note: must copy and paste below lines of code and then execute them one by one:

FOR /f %a IN (‘WMIC OS GET LocalDateTime ^| FIND "."‘) DO SET DTS=%a
SET DateTime=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%_%DTS:~8,2%-%DTS:~10,2%-%DTS:~12,2%
echo %DateTime%

In a BAT file:

@echo off
REM See http://stackoverflow.com/q/1642677/1143274
FOR /f %%a IN (‘WMIC OS GET LocalDateTime ^| FIND "."‘) DO SET DTS=%%a
SET DateTime=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%_%DTS:~8,2%-%DTS:~10,2%-%DTS:~12,2%
echo %DateTime%

Example output:

2016-08-26_22-21-15

How to generate file name according to datetime in bat command