临时文件清理
免费在线统计并清理用户与系统临时目录(默认只删超过 7 天的文件)。
Windows清理临时文件
🧹Windows 本地工具 · 使用介绍
使用介绍
用途:统计并清理 `%TEMP%` 等临时目录,释放磁盘空间。
使用步骤:
1.下载脚本,双击 `run.bat` 运行
2.先显示临时目录占用大小
3.设置清理天数(默认 7 天以上),确认后清理
安全说明:只删除超过 N 天且未被占用的临时文件,占用中的自动跳过;删除的是临时缓存,不影响文档/程序。建议先关闭浏览器、Office 等占用临时文件的程序再运行。
脚本为纯文本(PowerShell),可先用记事本打开查看源码再运行;仅在本机执行,不会上传任何数据。 首次运行如被系统拦截,右键脚本 → 属性 → 解除锁定,或用启动器(已带 ExecutionPolicy Bypass)运行。
脚本源码预览(可复制)
# ==================================================
# 临时文件清理(NMHUB 牛马工具)
# 统计并清理:用户临时目录、Windows 临时目录(超过 7 天的文件)
# 安全:只删除超过 7 天且未被占用的临时文件;占用中的文件自动跳过
# ==================================================
$ErrorActionPreference = "Continue"
Write-Host ""
Write-Host "========== 临时文件清理 ==========" -ForegroundColor Cyan
$targets = @(
@{ Name = "用户临时目录"; Path = $env:TEMP },
@{ Name = "Windows 临时目录"; Path = "$env:SystemRoot\Temp" }
)
$total = 0
foreach ($t in $targets) {
if (-not (Test-Path -LiteralPath $t.Path)) { continue }
$size = (Get-ChildItem -LiteralPath $t.Path -Recurse -File -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
$mb = [Math]::Round($size / 1MB, 1)
$total += $mb
Write-Host ("{0}: {1} MB" -f $t.Name, $mb)
}
Write-Host "合计约 $total MB。" -ForegroundColor Green
$days = Read-Host "只清理超过多少天的文件?(默认 7 天)"
if (-not $days) { $days = 7 }
$cutoff = (Get-Date).AddDays(-[int]$days)
$confirm = Read-Host "确认清理(仅删除超过 $days 天的临时文件)?(y/n)"
if ($confirm -ne "y" -and $confirm -ne "Y") {
Write-Host "已取消。" -ForegroundColor Yellow
Read-Host "按回车退出"; exit
}
$deleted = 0
$failed = 0
foreach ($t in $targets) {
if (-not (Test-Path -LiteralPath $t.Path)) { continue }
Get-ChildItem -LiteralPath $t.Path -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt $cutoff } |
ForEach-Object {
try {
Remove-Item -LiteralPath $_.FullName -Force -ErrorAction Stop
$deleted++
} catch { $failed++ }
}
}
Write-Host ""
Write-Host "完成:删除 $deleted 个文件,跳过 $failed 个(占用中)。" -ForegroundColor Green
Write-Host "提示:建议先关闭占用大量临时文件的程序(如浏览器/Office)再运行。" -ForegroundColor Yellow
Read-Host "按回车退出"