@echo off
setlocal EnableExtensions
chcp 65001 >nul
title C盘安全清理

rem Relaunch with administrator rights when needed.
net session >nul 2>&1
if not "%errorlevel%"=="0" (
    echo 正在申请管理员权限...
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

echo ============================================================
echo                    C盘安全清理工具
echo ============================================================
echo.
echo 将清理：
echo   1. Windows 和当前用户的临时文件
echo   2. Chrome/Edge 缓存及代码缓存
echo   3. Windows Update 下载缓存、错误报告
echo   4. calibre 缓存和 Conda 包缓存（如存在）
echo   5. C盘全部系统还原点（可选，下面单独询问）
echo.
echo 不会清理 Chrome/Edge 的历史记录、Cookie、密码、书签、
echo 登录资料、扩展数据，也不会删除个人文档。
echo.
echo 建议先关掉 Chrome 和 Edge，正在用的缓存文件删不掉。
echo.
choice /C YN /N /M "确认执行吗？输入 Y 继续，N 取消："
if errorlevel 2 (
    echo.
    echo 已取消，没有进行清理。
    pause
    exit /b
)

echo.
echo ------------------------------------------------------------
echo 是否同时删除 C盘全部系统还原点？
echo 通常能多腾出几个到几十个 GB，但删除后无法撤销，
echo 系统出问题时将无法回滚到之前的还原点。
echo ------------------------------------------------------------
set "DEL_SHADOWS=0"
choice /C YN /N /M "删除系统还原点吗？输入 Y 删除，N 保留（只做其它清理）："
if not errorlevel 2 set "DEL_SHADOWS=1"

for /f %%A in ('powershell.exe -NoProfile -Command "[int64](Get-PSDrive -Name C).Free"') do set "FREE_BEFORE=%%A"

echo.
echo [1/7] 清理 Windows 临时文件...
call :ClearDirectory "%SystemRoot%\SystemTemp"
call :ClearDirectory "%SystemRoot%\Temp"

echo [2/7] 清理当前用户临时文件...
call :ClearDirectory "%TEMP%"

echo [3/7] 清理 Chrome 和 Edge 缓存...
for /d %%P in ("%LOCALAPPDATA%\Google\Chrome\User Data\*") do (
    call :ClearDirectory "%%~fP\Cache"
    call :ClearDirectory "%%~fP\Code Cache"
)
for /d %%P in ("%LOCALAPPDATA%\Microsoft\Edge\User Data\*") do (
    call :ClearDirectory "%%~fP\Cache"
    call :ClearDirectory "%%~fP\Code Cache"
)

echo [4/7] 清理应用缓存和错误报告...
call :ClearDirectory "%LOCALAPPDATA%\calibre-cache"
call :ClearDirectory "%ProgramData%\Microsoft\Windows\WER"

echo [5/7] 清理 Windows Update 下载缓存...
net stop wuauserv /y >nul 2>&1
net stop bits /y >nul 2>&1
call :ClearDirectory "%SystemRoot%\SoftwareDistribution\Download"
net start bits >nul 2>&1
net start wuauserv >nul 2>&1

echo [6/7] 使用 Conda 自带命令清理包缓存...
call :CleanConda "%USERPROFILE%\Miniconda3\Scripts\conda.exe"
call :CleanConda "%USERPROFILE%\Anaconda3\Scripts\conda.exe"

if "%DEL_SHADOWS%"=="1" (
    echo [7/7] 删除 C盘全部系统还原点...
    vssadmin.exe delete shadows /for=C: /all /quiet >nul 2>&1
) else (
    echo [7/7] 已按你的选择保留系统还原点，跳过。
)

rem Ensure update-related services are not left stopped.
net start bits >nul 2>&1
net start wuauserv >nul 2>&1

for /f %%A in ('powershell.exe -NoProfile -Command "[int64](Get-PSDrive -Name C).Free"') do set "FREE_AFTER=%%A"

echo.
echo ============================================================
echo 清理完成。
powershell.exe -NoProfile -Command "$before=[int64]'%FREE_BEFORE%'; $after=[int64]'%FREE_AFTER%'; Write-Host ('清理前剩余空间：{0:N2} GB' -f ($before/1GB)); Write-Host ('清理后剩余空间：{0:N2} GB' -f ($after/1GB)); Write-Host ('本次实际释放：  {0:N2} GB' -f (($after-$before)/1GB))"
echo.
echo 提示：正在运行的程序可能锁定少量缓存文件，这些文件会被跳过。
echo Chrome/Edge 历史记录、Cookie、密码和书签未被删除。
echo ============================================================
pause
exit /b

:ClearDirectory
set "CLEAN_TARGET=%~1"
if not defined CLEAN_TARGET exit /b
if not exist "%CLEAN_TARGET%" exit /b
rem 路径太短（比如 C:\）或指到用户目录、系统目录本身时跳过，防止环境变量异常时误删。
if "%CLEAN_TARGET:~3%"=="" exit /b
if /i "%CLEAN_TARGET%"=="%USERPROFILE%" exit /b
if /i "%CLEAN_TARGET%"=="%SystemRoot%" exit /b
del /f /s /q "%CLEAN_TARGET%\*" >nul 2>&1
for /d %%D in ("%CLEAN_TARGET%\*") do rd /s /q "%%~fD" >nul 2>&1
exit /b

:CleanConda
if not exist "%~1" exit /b
"%~1" clean --all -y >nul 2>&1
exit /b
