网站运行生成日志的目录为:
D:\sihuiyun\webapi\logs\2022-08\2022-08-01\01
压缩2天前的日志,注意文件路径问题。
DaysAgo可以设置要保留的日志天数
@echo off set DaysAgo=2 call :DateToDays %date:~0,4% %date:~5,2% %date:~8,2% PassDays set /a PassDays-=%DaysAgo% call :DaysToDate %PassDays% DstYear DstMonth DstDay set DstDate=%DstYear%-%DstMonth%-%DstDay% echo %DaysAgo%ddd%DstDate% rar a -r -df -m3 D:\SiHuiYun\WebAPI\logs\%DstYear%-%DstMonth%\%DstDate%.rar D:\SiHuiYun\WebAPI\logs\%DstYear%-%DstMonth%\%DstDate% goto :eof :DateToDays %yy% %mm% %dd% days setlocal ENABLEEXTENSIONS set yy=%1&set mm=%2&set dd=%3 if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%) set /a dd=100%dd%%%100,mm=100%mm%%%100 set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2 set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633 endlocal&set %4=%j%&goto :EOF :DaysToDate %days% yy mm dd setlocal ENABLEEXTENSIONS set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5 set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10 (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%) endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%& goto :EOF
这里用到了压缩软件rar的exe单文件,可以参考:
https://www.cnblogs.com/micr067/p/12269738.html
删除指定目录的文件,保留最近两天的文件:
forfiles /p "D:\sihuiyun\webapi\temp" /s /m *.* /d -2 /c "cmd /c del @path /q /f"
这个删除只能删除文件,不能删除空的文件夹,需要单独的命令来删除空文件夹
for /f "tokens=*" %%i in ('dir /s /b /ad D:\SiHuiYun\webapi\temp\^|sort /r') do rd "%%i"