Batch command date and time in file name

前端 未结 14 2380
闹比i
闹比i 2020-11-22 01:25

I am compressing files using WinZip on the command line. Since we archive on a daily basis, I am trying to add date and time to these files so that a new one is auto generat

14条回答
  •  我寻月下人不归
    2020-11-22 01:46

    Another solution:

    for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
    

    It will give you (independent of locale settings!):

      20130802203023.304000+120
    ( YYYYMMDDhhmmss.+/-  )
    

    From here, it is easy:

    set datetime=%datetime:~0,8%-%datetime:~8,6%
    20130802-203023
    

    For Logan's request for the same outputformat for the "date-time modified" of a file:

    for %%F in (test.txt) do set file=%%~fF
    for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetime=%%I
    echo %datetime%
    

    It is a bit more complicated, because it works only with full paths, wmic expects the backslashes to be doubled and the = has to be escaped (the first one. The second one is protected by surrounding quotes).

提交回复
热议问题