依照文件的最新批改日期工夫进行删除
@REM @Author: chasi@REM @Date:   2020-11-09 14:49:16@REM @Last Modified by:   chasi@REM Modified time: 2020-11-10 09:14:16@echo offtitle 清理Tomcat日志文件set log_dir="D:\apache-tomcat-8.5.27\logs"set bak_dat=10forfiles /p %log_dir% /S /M *.log /D -%bak_dat% /C "cmd /c echo 正在删除@relpath 文件… & echo. & del @file"forfiles /p %log_dir% /S /M *.txt /D -%bak_dat% /C "cmd /c echo 正在删除@relpath 文件… & echo. & del @file"
依照文件中蕴含的日期格局间隔以后工夫的距离天数进行删除
@REM @Author: chasi@REM @Date:   2020-11-09 15:39:53@REM @Last Modified by:   chasi@REM Modified time: 2020-11-25 16:56:35@echo offrem 应用绝对路径,Tomcat目录下寄存日志的具体门路(以理论装置门路为准)set SrcDir="D:\apache-tomcat-8.5.27\logs"rem 指定日志保留天数set DaysAgo=10::计算距离天数>"%temp%/DstDate.vbs" echo LastDate=date()-%DaysAgo%>>"%temp%/DstDate.vbs" echo FmtDate=right(year(LastDate),4) ^& right("0" ^& month(LastDate),2) ^& right("0" ^& day(LastDate),2)>>"%temp%/DstDate.vbs" echo wscript.echo FmtDatefor /f %%a in ('cscript /nologo "%temp%/DstDate.vbs"') do (  set "DstDate=%%a")::设置日期格局set DstDate=%DstDate:~0,4%-%DstDate:~4,2%-%DstDate:~6,2%::开启变量提早setlocal enabledelayedexpansionset indexofstr=0::查找含有指定日期格局字符串的文件for /r "%SrcDir%" %%a in (*.*) do (    set "FileDate=%%~na"    ::echo indexofstr:!indexofstr!    call:IndexOfStrFunc "%%~na",indexofstr    ::echo indexofstr:!indexofstr!    set  indexofstr=!indexofstr!    ::echo %indexofstr%    call set FileDate=%%FileDate:~!indexofstr!,10%%    if "!FileDate!" leq "%DstDate%" (        if exist "%%a" (            ::echo del /f /q "%%a"            del /f /q "%%a"        )    ))::敞开变量提早endlocal::echo 输入结束,按任意键退出&&pause>nul&&exit:IndexOfStrFunc::IndexOfStrFunc函数:查找字符串内第一个字符为2-3的索引地位set str1=%~1set str=%str1%set num=0:nextset ch1=2if not "%str%"=="" (set /a num+=1::循环局部:loopif not "%ch1%" == "3" (    if "!str:~0,1!"=="%ch1%" goto last    set /a ch1=ch1+1    goto loop)set "str=%str:~1%"goto next)set /a num=0:last::echo 字符'%ch1%'在字符串"%str1%"中的首次呈现地位为%num%set next=1set /a indexofstr=%num%-%next%goto:eof
PS:windows端增加定时工作脚本,运行cmd,输出compmgmt.msc后依照定时工作的增加步骤要求进行填写。