@echo off
setlocal enabledelayedexpansion
set "days=7"
set "tempFile=%temp%\del_list_%RANDOM%.txt"
echo 正在扫描%days%天前的文件...
forfiles /P "." /D -%days% /C "cmd /c if @isdir==FALSE echo @relpath" > "%tempFile%" 2>nul
if %errorlevel% neq 0 (
echo 错误:无法读取文件列表
goto cleanup
)
set /a fileCount=0
for /f "delims=" %%a in ('type "%tempFile%"') do set /a fileCount+=1
if %fileCount% equ 0 (
echo 未找到超过%days%天的文件
goto cleanup
)
echo 找到以下待删除文件(共%fileCount%个):
type "%tempFile%"
echo.
:confirm
set "input="
set /p "input=确认删除以上文件?(y/n) "
if /i "!input!"=="y" (
echo 开始删除...
for /f "delims=" %%F in (%tempFile%) do (
echo 删除: %%~F
del "%%~F" 2>nul || echo 删除失败: %%~F
)
echo 操作完成
) else (
echo 操作已取消
)
:cleanup
if exist "%tempFile%" del "%tempFile%" >nul 2>&1
echo 按任意键退出...
pause >nul