Batch file to delete files older than N days

前端 未结 24 2489
没有蜡笔的小新
没有蜡笔的小新 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:54

    I think e.James's answer is good since it works with unmodified versions of Windows as early as Windows 2000 SP4 (and possibly earlier), but it required writing to an external file. Here is a modified version that does not create an external text file while maintaining the compatibility:

    REM del_old.cmd
    REM usage: del_old MM-DD-YYYY
    setlocal enabledelayedexpansion
    for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do @if exist "%%~nxa" set "excludefiles=!excludefiles!;;%%~nxa;;"
    for /f "tokens=*" %%a IN ('dir /b') do @(@echo "%excludefiles%"|FINDSTR /C:";;%%a;;">nul || if exist "%%~nxa" DEL /F /Q "%%a">nul 2>&1)
    

    To be true to the original question, here it is in a script that does ALL the math for you if you call it with the number of days as the parameter:

    REM del_old_compute.cmd
    REM usage: del_old_compute N
    setlocal enabledelayedexpansion
    set /a days=%1&set cur_y=%DATE:~10,4%&set cur_m=%DATE:~4,2%&set cur_d=%DATE:~7,2%
    for /f "tokens=1 delims==" %%a in ('set cur_') do if "!%%a:~0,1!"=="0" set /a %%a=!%%a:~1,1!+0
    set mo_2=28&set /a leapyear=cur_y*10/4
    if %leapyear:~-1% equ 0 set mo_2=29
    set mo_1=31&set mo_3=31&set mo_4=30&set mo_5=31
    set mo_6=30&set mo_7=31&set mo_8=31&set mo_9=30
    set mo_10=31&set mo_11=30&set mo_12=31
    set /a past_y=(days/365)
    set /a monthdays=days-((past_y*365)+((past_y/4)*1))&&set /a past_y=cur_y-past_y&set months=0
    :setmonth
    set /a minusmonth=(cur_m-1)-months
    if %minusmonth% leq 0 set /a minusmonth+=12
    set /a checkdays=(mo_%minusmonth%)
    if %monthdays% geq %checkdays% set /a months+=1&set /a monthdays-=checkdays&goto :setmonth
    set /a past_m=cur_m-months
    set /a lastmonth=cur_m-1
    if %lastmonth% leq 0 set /a lastmonth+=12
    set /a lastmonth=mo_%lastmonth%
    set /a past_d=cur_d-monthdays&set affffdays=::
    if %past_d% leq 0 (set /a past_m-=1&set affffdays=)
    if %past_m% leq 0 (set /a past_m+=12&set /a past_y-=1)
    set mo_2=28&set /a leapyear=past_y*10/4
    if %leapyear:~-1% equ 0 set mo_2=29
    %affffdays%set /a past_d+=mo_%past_m%
    set d=%past_m%-%past_d%-%past_y%
    for /f "tokens=*" %%a IN ('xcopy *.* /d:%d% /L /I null') do @if exist "%%~nxa" set "excludefiles=!excludefiles!;;%%~nxa;;"
    for /f "tokens=*" %%a IN ('dir /b') do @(@echo "%excludefiles%"|FINDSTR /C:";;%%a;;">nul || if exist "%%~nxa" DEL /F /Q "%%a">nul 2>&1)
    

    NOTE: The code above takes into account leap years, as well as the exact number of days in each month. The only maximum is the total number of days there have been since 0/0/0 (after that it returns negative years).

    NOTE: The math only goes one way; it cannot correctly get future dates from negative input (it will try, but will likely go past the last day of the month).

提交回复
热议问题