Batch command date and time in file name

前端 未结 14 2042
闹比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:41

    Extract the hour, look for a leading space, if found replace with a zero;

    set hr=%time:~0,2%
    if "%hr:~0,1%" equ " " set hr=0%hr:~1,1%
    echo Archive_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%.zip
    
    0 讨论(0)
  • 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.<milliseconds><always 000>+/-<minutes difference to UTC>  )
    

    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).

    0 讨论(0)
  • 2020-11-22 01:46

    Your question seems to be solved, but ...

    I'm not sure if you take the right solution for your problem.
    I suppose you try to compress each day the actual project code.

    It's possible with ZIP and 1980 this was a good solution, but today you should use a repository system, like subversion or git or ..., but not a zip-file.

    Ok, perhaps it could be that I'm wrong.

    0 讨论(0)
  • 2020-11-22 01:50

    I found the best solution for me, after reading all your answers:

    set t=%date%_%time%
    set d=%t:~10,4%%t:~7,2%%t:~4,2%_%t:~15,2%%t:~18,2%%t:~21,2%
    echo hello>"Archive_%d%"
    

    If AM I get 20160915_ 150101 (with a leading space and time).

    If PM I get 20160915_2150101.

    0 讨论(0)
  • 2020-11-22 01:50

    You can add leading zeroes to a variable (value up to 99) like this in batch: IF 1%Var% LSS 100 SET Var=0%Var%

    So you'd need to parse your date and time components out into separate variables, treat them all like this, then concatenate them back together to create the file name.

    However, your underlying method for parsing date and time is dependent on system locale settings. If you're happy for your code not to be portable to other machines, that's probably fine, but if you expect it to work in different international contexts then you'll need a different approach, for example by reading out the registry settings:

    HKEY_CURRENT_USER\Control Panel\International\iDate
    HKEY_CURRENT_USER\Control Panel\International\iTime
    HKEY_CURRENT_USER\Control Panel\International\iTLZero
    

    (That last one controls whether there is a leading zero on times, but not dates as far as I know).

    0 讨论(0)
  • 2020-11-22 01:55

    You should search; you can simply replace all spaces with zero set hr=%hr: =0%jeb Oct 11 '11 at 14:16

    So I did:

    set hr=%time:~0,2%
    set hr=%hr: =0%
    

    Then use %hr% inside whatever string you are formatting to always get a two-digit hour.

    (Jeb's comment under the most popular answer worked the best for me and is the simplest. I repost it here to make it more obvious for future users.)

    0 讨论(0)
提交回复
热议问题