按照文件的最新修改日期时间进行删除
@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 off
title 清理Tomcat日志文件
set log_dir="D:\apache-tomcat-8.5.27\logs"
set bak_dat=10
forfiles /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 off
rem 使用绝对路径,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 FmtDate
for /f %%a in ('cscript /nologo "%temp%/DstDate.vbs"') do (
set "DstDate=%%a"
)
::设置日期格式
set DstDate=%DstDate:~0,4%-%DstDate:~4,2%-%DstDate:~6,2%
::开启变量延迟
setlocal enabledelayedexpansion
set 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=%~1
set str=%str1%
set num=0
:next
set ch1=2
if not "%str%"=="" (
set /a num+=1
::循环部分
:loop
if 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=1
set /a indexofstr=%num%-%next%
goto:eof
PS:windows端添加定时任务脚本,运行cmd,输入compmgmt.msc后按照定时任务的添加步骤要求进行填写。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。