Batch command date and time in file name

前端 未结 14 2053
闹比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 02:01

    As Vicky already pointed out, %DATE% and %TIME% return the current date and time using the short date and time formats that are fully (endlessly) customizable.

    One user may configure its system to return Fri040811 08.03PM while another user may choose 08/04/2011 20:30.

    It's a complete nightmare for a BAT programmer.

    Changing the format to a firm format may fix the problem, provided you restore back the previous format before leaving the BAT file. But it may be subject to nasty race conditions and complicate recovery in cancelled BAT files.

    Fortunately, there is an alternative.

    You may use WMIC, instead. WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table returns the date and time in a invariable way. Very convenient to directly parse it with a FOR /F command.

    So, putting the pieces together, try this as a starting point...

    SETLOCAL enabledelayedexpansion
    FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
      SET /A FD=%%F*1000000+%%D*100+%%A
      SET /A FT=10000+%%B*100+%%C
      SET FT=!FT:~-4!
      ECHO Archive_!FD!_!FT!.zip
     )
    

提交回复
热议问题