wei/sh/log_del.bat
2026-06-26 10:10:40 +08:00

37 lines
1.0 KiB
Batchfile
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@echo off
setlocal enabledelayedexpansion
rem 设置保留天数
set days_to_keep=3
rem 计算截止日期(当前日期减去保留天数)
for /f "tokens=1-3 delims=/- " %%a in ('date /t') do (
set current_day=%%a
set current_month=%%b
set current_year=%%c
)
rem 使用PowerShell计算截止日期更准确的方法
for /f %%d in ('powershell -command "(Get-Date).AddDays(-%days_to_keep%).ToString('yyyy-MM-dd')"') do (
set cutoff_date=%%d
)
echo 正在删除 %cutoff_date% 之前的日志文件...
echo.
for /D %%A in ("D:\*") do (
if exist "%%A\userdata\log\*.log" (
echo 正在检查目录: %%A\userdata\log
rem 使用forfiles命令删除3天前的文件
forfiles /p "%%A\userdata\log" /m "*.log" /d -%days_to_keep% /c "cmd /c echo 正在删除 @path && del /q @path"
rem 显示剩余文件
echo 剩余文件:
dir "%%A\userdata\log\*.log" /b 2>nul || echo 目录中没有日志文件
echo.
)
)
echo 清理完成!
pause