joken-前端工程师

  博客园 :: 首页 :: 新随笔 :: :: :: 管理 ::

window bat 深度清理垃圾文件代码,一键运行

@echo off
setlocal enabledelayedexpansion
title Windows Deep Disk Cleanup Tool (Automated One-Click)
color 0A
cd /d "%~dp0"

REM ========================================================
REM 1. Check Admin Privileges & Auto-Elevate
REM ========================================================
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo [INFO] Requesting Administrator Privileges...
    echo Please click YES on the UAC prompt window.
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

REM ========================================================
REM 2. Start Automated Cleanup
REM ========================================================
cls
echo ========================================================
echo       Windows Deep Disk Cleanup Tool (One-Click)
echo ========================================================
echo.

echo Analyzing current disk space...
set "space_gb_before=0"
set "space_mb_before=0"
for /f "usebackq tokens=*" %%A in (`powershell -NoProfile -Command "[math]::Round((Get-PSDrive $env:SystemDrive[0]).Free / 1GB, 2)"`) do set "space_gb_before=%%A"
for /f "usebackq tokens=*" %%A in (`powershell -NoProfile -Command "[int64]((Get-PSDrive $env:SystemDrive[0]).Free / 1MB)"`) do set "space_mb_before=%%A"

echo System Drive (%SystemDrive%) Free Space Before: %space_gb_before% GB (%space_mb_before% MB)
echo.
echo Starting deep cleanup...
echo --------------------------------------------------------

REM --------------------------------------------------------
REM [0] Close Browsers and Target Apps to Release File Locks
REM --------------------------------------------------------
echo Closing running browsers, WeChat, and Douyin to release locked caches...
taskkill /f /im chrome.exe >nul 2>&1
taskkill /f /im msedge.exe >nul 2>&1
taskkill /f /im brave.exe >nul 2>&1
taskkill /f /im firefox.exe >nul 2>&1
taskkill /f /im 360chrome.exe >nul 2>&1
taskkill /f /im 360se.exe >nul 2>&1
taskkill /f /im qqbrowser.exe >nul 2>&1
taskkill /f /im opera.exe >nul 2>&1
taskkill /f /im vivaldi.exe >nul 2>&1
taskkill /f /im WeChat.exe >nul 2>&1
taskkill /f /im WeChatAppEx.exe >nul 2>&1
taskkill /f /im WeChatPlayer.exe >nul 2>&1
taskkill /f /im Douyin.exe >nul 2>&1
timeout /t 1 /nobreak >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [1] Temp Files
REM --------------------------------------------------------
echo [1/12] Cleaning User and System Temp files...
del /f /s /q "%TEMP%\*.*" >nul 2>&1
for /d %%i in ("%TEMP%\*") do rd /s /q "%%i" >nul 2>&1

del /f /s /q "%WINDIR%\Temp\*.*" >nul 2>&1
for /d %%i in ("%WINDIR%\Temp\*") do rd /s /q "%%i" >nul 2>&1

del /f /q "%SystemDrive%\*.tmp" >nul 2>&1
del /f /q "%SystemDrive%\*._mp" >nul 2>&1
del /f /q "%SystemDrive%\*.log" >nul 2>&1
del /f /q "%SystemDrive%\*.gid" >nul 2>&1
del /f /q "%SystemDrive%\*.chk" >nul 2>&1
del /f /q "%SystemDrive%\*.old" >nul 2>&1
del /f /q "%SystemDrive%\*.bak" >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [2] Browser, WeChat, and Douyin Caches
REM --------------------------------------------------------
echo [2/12] Cleaning Browser, WeChat, and Douyin Caches...
if exist "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache" (
    rd /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Code Cache" (
    rd /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\Code Cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Google\Chrome\User Data\Default\GPUCache" (
    rd /s /q "%LOCALAPPDATA%\Google\Chrome\User Data\Default\GPUCache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache" (
    rd /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Code Cache" (
    rd /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\Code Cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\GPUCache" (
    rd /s /q "%LOCALAPPDATA%\Microsoft\Edge\User Data\Default\GPUCache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\BraveSoftware\Brave-Browser\User Data\Default\Cache" (
    rd /s /q "%LOCALAPPDATA%\BraveSoftware\Brave-Browser\User Data\Default\Cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Douyin\User Data\Default\Cache" (
    rd /s /q "%LOCALAPPDATA%\Douyin\User Data\Default\Cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\Douyin\cache" (
    rd /s /q "%LOCALAPPDATA%\Douyin\cache" >nul 2>&1
)
if exist "%USERPROFILE%\AppData\Local\Tencent\WeChat\XPlugin" (
    rd /s /q "%USERPROFILE%\AppData\Local\Tencent\WeChat\XPlugin" >nul 2>&1
)
if exist "%USERPROFILE%\AppData\Local\Microsoft\Windows\INetCache" (
    rd /s /q "%USERPROFILE%\AppData\Local\Microsoft\Windows\INetCache" >nul 2>&1
)
if exist "%USERPROFILE%\AppData\Local\Microsoft\Windows\INetCookies" (
    del /f /q "%USERPROFILE%\AppData\Local\Microsoft\Windows\INetCookies\*.*" >nul 2>&1
)
if exist "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Recent" (
    del /f /q "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Recent\*.*" >nul 2>&1
    rd /s /q "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations" >nul 2>&1
    rd /s /q "%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Recent\CustomDestinations" >nul 2>&1
)
echo        Done.

REM --------------------------------------------------------
REM [3] GPU Driver Installer Leftovers (NVIDIA, AMD, Intel)
REM --------------------------------------------------------
echo [3/12] Cleaning GPU Driver Installer Leftovers (NVIDIA, AMD, Intel)...
if exist "%SystemDrive%\NVIDIA\DisplayDriver" (
    rd /s /q "%SystemDrive%\NVIDIA\DisplayDriver" >nul 2>&1
)
if exist "%ProgramData%\NVIDIA Corporation\Downloader" (
    rd /s /q "%ProgramData%\NVIDIA Corporation\Downloader" >nul 2>&1
)
if exist "%SystemDrive%\AMD" (
    rd /s /q "%SystemDrive%\AMD" >nul 2>&1
)
if exist "%SystemDrive%\Intel" (
    rd /s /q "%SystemDrive%\Intel" >nul 2>&1
)
echo        Done.

REM --------------------------------------------------------
REM [4] DirectX and GPU Shader Caches
REM --------------------------------------------------------
echo [4/12] Cleaning DirectX and GPU Shader Caches...
if exist "%LOCALAPPDATA%\D3DSCache" (
    del /f /s /q "%LOCALAPPDATA%\D3DSCache\*.*" >nul 2>&1
    for /d %%i in ("%LOCALAPPDATA%\D3DSCache\*") do rd /s /q "%%i" >nul 2>&1
)
if exist "%LOCALAPPDATA%\NVIDIA\DXCache" (
    del /f /s /q "%LOCALAPPDATA%\NVIDIA\DXCache\*.*" >nul 2>&1
)
if exist "%LOCALAPPDATA%\NVIDIA\GLCache" (
    del /f /s /q "%LOCALAPPDATA%\NVIDIA\GLCache\*.*" >nul 2>&1
)
if exist "%LOCALAPPDATA%\AMD\DxCache" (
    del /f /s /q "%LOCALAPPDATA%\AMD\DxCache\*.*" >nul 2>&1
)
echo        Done.

REM --------------------------------------------------------
REM [5] Empty Recycle Bin
REM --------------------------------------------------------
echo [5/12] Emptying all Recycle Bins...
powershell -NoProfile -Command "Clear-RecycleBin -Force -ErrorAction SilentlyContinue" >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [6] Flush DNS Cache
REM --------------------------------------------------------
echo [6/12] Flushing DNS Cache...
ipconfig /flushdns >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [7] Windows Update and Delivery Optimization Cache
REM --------------------------------------------------------
echo [7/12] Cleaning Windows Update and Delivery Optimization Cache...
net stop wuauserv >nul 2>&1
net stop bits >nul 2>&1
net stop dosvc >nul 2>&1

if exist "%WINDIR%\SoftwareDistribution\Download" (
    del /f /s /q "%WINDIR%\SoftwareDistribution\Download\*.*" >nul 2>&1
    for /d %%i in ("%WINDIR%\SoftwareDistribution\Download\*") do rd /s /q "%%i" >nul 2>&1
)
if exist "%WINDIR%\SoftwareDistribution\DeliveryOptimization" (
    del /f /s /q "%WINDIR%\SoftwareDistribution\DeliveryOptimization\*.*" >nul 2>&1
    for /d %%i in ("%WINDIR%\SoftwareDistribution\DeliveryOptimization\*") do rd /s /q "%%i" >nul 2>&1
)
if exist "%SystemRoot%\ServiceProfiles\LocalService\AppData\Local\Microsoft\DeliveryOptimization\Cache" (
    rd /s /q "%SystemRoot%\ServiceProfiles\LocalService\AppData\Local\Microsoft\DeliveryOptimization\Cache" >nul 2>&1
)

net start dosvc >nul 2>&1
net start bits >nul 2>&1
net start wuauserv >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [8] System Logs, WER, Crash Dumps
REM --------------------------------------------------------
echo [8/12] Cleaning System Logs (CBS/DISM/Events) and Error Reports...
del /f /q "%WINDIR%\Logs\CBS\CbsPersist_*.log" >nul 2>&1
del /f /q "%WINDIR%\Logs\CBS\CbsPersist_*.cab" >nul 2>&1
del /f /q "%WINDIR%\Logs\DISM\dism.log" >nul 2>&1
for %%a in (Application Security System Setup) do (
    wevtutil cl "%%a" >nul 2>&1
)

if exist "%ProgramData%\Microsoft\Windows\WER" (
    del /f /s /q "%ProgramData%\Microsoft\Windows\WER\ReportArchive\*.*" >nul 2>&1
    del /f /s /q "%ProgramData%\Microsoft\Windows\WER\ReportQueue\*.*" >nul 2>&1
    for /d %%i in ("%ProgramData%\Microsoft\Windows\WER\ReportArchive\*") do rd /s /q "%%i" >nul 2>&1
    for /d %%i in ("%ProgramData%\Microsoft\Windows\WER\ReportQueue\*") do rd /s /q "%%i" >nul 2>&1
)
if exist "%LOCALAPPDATA%\CrashDumps" (
    del /f /s /q "%LOCALAPPDATA%\CrashDumps\*.*" >nul 2>&1
    for /d %%i in ("%LOCALAPPDATA%\CrashDumps\*") do rd /s /q "%%i" >nul 2>&1
)
del /f /q "%WINDIR%\memory.dmp" >nul 2>&1
del /f /q "%WINDIR%\Minidump\*.dmp" >nul 2>&1
del /f /q "%WINDIR%\System32\FNTCACHE.DAT" >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [9] Windows.old Leftovers (if present)
REM --------------------------------------------------------
echo [9/12] Checking and Cleaning Windows.old Residual Backups...
if exist "%SystemDrive%\Windows.old" (
    rd /s /q "%SystemDrive%\Windows.old" >nul 2>&1
)
echo        Done.

REM --------------------------------------------------------
REM [10] Developer and Tool Caches (pip, npm, VS Code)
REM --------------------------------------------------------
echo [10/12] Cleaning Developer Caches (pip, npm, VS Code)...
if exist "%LOCALAPPDATA%\pip\cache" (
    rd /s /q "%LOCALAPPDATA%\pip\cache" >nul 2>&1
)
if exist "%LOCALAPPDATA%\npm-cache" (
    rd /s /q "%LOCALAPPDATA%\npm-cache" >nul 2>&1
)
if exist "%APPDATA%\Code\Cache" (
    rd /s /q "%APPDATA%\Code\Cache" >nul 2>&1
)
if exist "%APPDATA%\Code\CachedData" (
    rd /s /q "%APPDATA%\Code\CachedData" >nul 2>&1
)
if exist "%APPDATA%\Code\CachedExtensions" (
    rd /s /q "%APPDATA%\Code\CachedExtensions" >nul 2>&1
)
echo        Done.

REM --------------------------------------------------------
REM [11] Downloads Directory (> 30 days old)
REM --------------------------------------------------------
echo [11/12] Cleaning Downloads files older than 30 days...
set "TARGET_DOWNLOADS="
for /f "usebackq delims=" %%A in (`powershell -NoProfile -Command "$p = (Get-ItemPropertyValue -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' -Name '{374DE290-123F-4565-9164-39C4925E467B}' -ErrorAction SilentlyContinue); if (!$p) { $p = \"$env:USERPROFILE\Downloads\" }; [System.Environment]::ExpandEnvironmentVariables($p)"`) do (
    set "TARGET_DOWNLOADS=%%A"
)
if not defined TARGET_DOWNLOADS set "TARGET_DOWNLOADS=%USERPROFILE%\Downloads"
if not exist "%TARGET_DOWNLOADS%" set "TARGET_DOWNLOADS=%USERPROFILE%\Downloads"

powershell -NoProfile -Command "$t = '%TARGET_DOWNLOADS%'; Get-ChildItem -LiteralPath $t -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Force -ErrorAction SilentlyContinue; Get-ChildItem -LiteralPath $t -Recurse -Directory -ErrorAction SilentlyContinue | Where-Object { (Get-ChildItem -LiteralPath $_.FullName -Recurse -File -ErrorAction SilentlyContinue).Count -eq 0 -and $_.LastWriteTime -lt (Get-Date).AddDays(-30) } | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue" >nul 2>&1
echo        Done.

REM --------------------------------------------------------
REM [12] Reset Explorer Cache
REM --------------------------------------------------------
echo [12/12] Rebuilding Explorer thumbnail and icon cache...
taskkill /f /im explorer.exe >nul 2>&1
timeout /t 1 /nobreak >nul 2>&1

del /f /q "%LOCALAPPDATA%\Microsoft\Windows\Explorer\thumbcache_*.db" >nul 2>&1
del /f /q "%LOCALAPPDATA%\Microsoft\Windows\Explorer\iconcache_*.db" >nul 2>&1
del /f /q "%LOCALAPPDATA%\IconCache.db" >nul 2>&1

start explorer.exe >nul 2>&1
echo        Done.

REM ========================================================
REM 3. Calculate Results & Summary
REM ========================================================
echo.
echo --------------------------------------------------------
echo Calculating cleanup results...
set "space_gb_after=0"
set "space_mb_after=0"
for /f "usebackq tokens=*" %%A in (`powershell -NoProfile -Command "[math]::Round((Get-PSDrive $env:SystemDrive[0]).Free / 1GB, 2)"`) do set "space_gb_after=%%A"
for /f "usebackq tokens=*" %%A in (`powershell -NoProfile -Command "[int64]((Get-PSDrive $env:SystemDrive[0]).Free / 1MB)"`) do set "space_mb_after=%%A"

set /a freed_mb=!space_mb_after!-!space_mb_before!
if !freed_mb! lss 0 set "freed_mb=0"

echo.
echo ========================================================
echo                  CLEANUP COMPLETED!
echo ========================================================
echo  Free Space Before : %space_gb_before% GB (%space_mb_before% MB)
echo  Free Space After  : %space_gb_after% GB (%space_mb_after% MB)
echo  ------------------------------------------------------
echo  * TOTAL SPACE FREED: !freed_mb! MB
echo ========================================================
echo.
echo Summary of Cleaned Items:
echo  [+] User and System Temp Directories (%%TEMP%%, Windows\Temp)
echo  [+] Webpage, Browser, WeChat, and Douyin Media Caches
echo  [+] GPU Driver Installer Residuals (NVIDIA, AMD, Intel)
echo  [+] DirectX and GPU Shader Caches (D3DSCache, DXCache)
echo  [+] Full Emptying of All Drive Recycle Bins
echo  [+] Flushed DNS Client Resolver Cache
echo  [+] Windows Update and Delivery Optimization Download Cache
echo  [+] Windows CBS/DISM Residual Update Logs
echo  [+] Windows Event Logs (Application, Security, System, Setup)
echo  [+] Windows Error Reports (WER) and System Crash Dumps
echo  [+] Windows.old Residual Upgrade Backup (if present)
echo  [+] Developer Package and Editor Caches (pip, npm, VS Code)
echo  [+] Downloads Directory Files Older Than 30 Days
echo  [+] Explorer Thumbnail and Icon Cache Databases
echo.
echo Note: Files currently in use were safely skipped.
echo.
echo ========================================================
echo Press any key to close this window...
pause >nul
exit /b 0

posted on 2026-09-03 18:17  joken1310  阅读(115)  评论(0)    收藏  举报