You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yudao-web/clean_old_folders.bat

43 lines
1.4 KiB
Batchfile

@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
:: ======= 配置 =======
set "TARGET_DIR=D:\your\data\path"
set "DRIVE_LETTER=D:"
set "MIN_FREE_GB=200"
set "RETENTION_DAYS=60"
:: ===================
:: 1. 查磁盘剩余空间
for /f "tokens=3" %%a in ('dir %DRIVE_LETTER%\ 2^>nul ^| findstr "可用字节"') do set "FREE=%%a"
if "%FREE%"=="" for /f "tokens=3" %%a in ('dir %DRIVE_LETTER%\ 2^>nul ^| findstr /i "free"') do set "FREE=%%a"
set /a "FREE_GB=%FREE:~0,-3% / 1024 / 1024"
:: 2. 空间够就跳过
if %FREE_GB% GEQ %MIN_FREE_GB% goto :END
:: 3. 目标目录不存在就退出
if not exist "%TARGET_DIR%" goto :END
:: 4. 算30天前的日期
for /f %%i in ('powershell -NoProfile -Command "(Get-Date).AddDays(-%RETENTION_DAYS%).ToString('yyyy-MM-dd')"') do set "CUTOFF=%%i"
:: 5. 遍历删除,每删一个检查空间
for /d %%D in ("%TARGET_DIR%\*") do (
set "FN=%%~nxD"
echo !FN! | find "-" >nul && (
if "!FN!" LSS "%CUTOFF%" (
rd /s /q "%%D" 2>nul
:: 重新查剩余空间
for /f "tokens=3" %%f in ('dir %DRIVE_LETTER%\ 2^>nul ^| findstr "可用字节"') do set "FREE=%%f"
if "!FREE!"=="" for /f "tokens=3" %%f in ('dir %DRIVE_LETTER%\ 2^>nul ^| findstr /i "free"') do set "FREE=%%f"
set /a "FREE_GB=!FREE:~0,-3! / 1024 / 1024"
if !FREE_GB! GEQ %MIN_FREE_GB% goto :END
)
)
)
:END
exit /b 0