Batch command date and time in file name

前端 未结 14 2082
闹比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条回答
  •  梦毁少年i
    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).

提交回复
热议问题