Generate unique file name with timestamp in batch script

前端 未结 7 1191
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 13:07

In my .bat file I want to generate a unique name for files/directories based on date-time.

e.g.

Build-2009-10-29-10-59-00

The probl

7条回答
  •  时光取名叫无心
    2020-12-13 13:41

    Following up on @Joey's and @Kees' answers to make them instantly usable.

    On the command line:

    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:

    2014-10-21_16-28-52
    

提交回复
热议问题