Batch file to delete files older than N days

前端 未结 24 2500
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 11:11

I am looking for a way to delete all files older than 7 days in a batch file. I\'ve searched around the web, and found some examples with hundreds of lines of code, and oth

24条回答
  •  醉梦人生
    2020-11-21 11:43

    How about this modification on 7daysclean.cmd to take a leap year into account?

    It can be done in less than 10 lines of coding!

    set /a Leap=0
    if (Month GEQ 2 and ((Years%4 EQL 0 and Years%100 NEQ 0) or Years%400 EQL 0)) set /a Leap=day
    set /a Months=!_months!+Leap
    

    Edit by Mofi:

    The condition above contributed by J.R. evaluates always to false because of invalid syntax.

    And Month GEQ 2 is also wrong because adding 86400 seconds for one more day must be done in a leap year only for the months March to December, but not for February.

    A working code to take leap day into account - in current year only - in batch file 7daysclean.cmd posted by Jay would be:

    set "LeapDaySecs=0"
    if %Month% LEQ 2 goto CalcMonths
    set /a "LeapRule=Years%%4"
    if %LeapRule% NEQ 0 goto CalcMonths
    rem The other 2 rules can be ignored up to year 2100.
    set /A "LeapDaySecs=day"
    :CalcMonths
    set /a Months=!_months!+LeapDaySecs
    

提交回复
热议问题