How to append a date in batch files

后端 未结 14 2040
夕颜
夕颜 2020-12-01 03:25

I have the following line in a batch file (that runs on an old Windows 2000 box):

7z a QuickBackup.zip *.backup

How do I append the date to

相关标签:
14条回答
  • 2020-12-01 04:18

    You can also access the date via the variable %DATE%

    When testing my system %DATE% produces ffffd dd/mm/yyyy

    you can use substring operators to produce the format you desire

    ie. running the following on MON 11/12/2018 with US regional settings

    %DATE:~3,3% %DATE:~0,3% %DATE:~7,2%
    

    Will produce an output:

    11 Mon 12
    

    the substring arguments are
    %*variable*:~*startpos*,*numberofchars*%

    0 讨论(0)
  • 2020-12-01 04:18

    I've found two ways that work regardless of the date settings.

    On my pc, date/t returns 2009-05-27

    You can either access the registry and read the regional settings (HKEY_CURRENT_USER\Control Panel\International)

    Or use a vbscript. This is the ugly batch file/vbscript hybrid I created some time ago....

    @Echo Off
    set rnd=%Random%
    set randfilename=x%rnd%.vbs
    
    ::create temp vbscript file
    Echo Dim DayofWeek(7) >  %temp%\%randfilename%
    Echo DayofWeek(1)="Sun" >> %temp%\%randfilename%
    Echo DayofWeek(2)="Mon" >> %temp%\%randfilename%
    Echo DayofWeek(3)="Tue" >> %temp%\%randfilename%
    Echo DayofWeek(4)="Wed" >> %temp%\%randfilename%
    Echo DayofWeek(5)="Thu" >> %temp%\%randfilename%
    Echo DayofWeek(6)="Fri" >> %temp%\%randfilename%
    Echo DayofWeek(7)="Sat" >> %temp%\%randfilename%
    Echo DayofWeek(0)=DayofWeek(Weekday(now)) >> %temp%\%randfilename%
    Echo Mon=Left(MonthName(Month(now),1),3) >> %temp%\%randfilename%
    Echo MonNumeric=right ( "00" ^& Month(now) , 2) >> %temp%\%randfilename%
    Echo wscript.echo ( Year(Now) ^& " " ^& MonNumeric ^& " " ^& Mon ^& " "  _ >> %temp%\%randfilename%
    Echo ^& right("00" ^& Day(now),2) ^& " "^& dayofweek(0) ^& " "^& _ >> %temp%\%randfilename%
    Echo right("00" ^& Hour(now),2)) _ >> %temp%\%randfilename%
    Echo ^&":"^& Right("00" ^& Minute(now),2) ^&":"^& Right("00" ^& Second(Now),2)  >> %temp%\%randfilename%
    
    ::set the output into vars
    if "%1" == "" FOR /f "usebackq tokens=1,2,3,4,5,6" %%A in (`start /wait /b cscript //nologo %temp%\%randfilename%`) do Set Y2KYear=%%A& Set MonthNumeric=%%B& Set Month=%%C& Set Day=%%D& Set DayofWeek=%%E& Set Time=%%F
    set year=%y2kyear:~2,2%
    
    ::cleanup
    del %temp%\%randfilename%
    

    It's not pretty, but it works.

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