本文目录导读:

我来教您编写自动清理最近文档的脚本,根据不同的操作系统,方法会有所不同。
Windows系统
PowerShell脚本
# 清理Windows最近使用的文档
# 保存为 Clean-RecentDocs.ps1
Write-Host "开始清理最近文档..." -ForegroundColor Yellow
# 清理用户最近的文档
$recentPath = "$env:APPDATA\Microsoft\Windows\Recent"
if (Test-Path $recentPath) {
Get-ChildItem -Path $recentPath -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Write-Host "已清理用户最近文档" -ForegroundColor Green
}
# 清理所有用户的文档(需要管理员权限)
$allUsersPath = "C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent"
Get-ChildItem -Path $allUsersPath -Force -ErrorAction SilentlyContinue | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
# 清除跳转列表
$jumpListPaths = @(
"$env:APPDATA\Microsoft\Windows\Recent\AutomaticDestinations",
"$env:APPDATA\Microsoft\Windows\Recent\CustomDestinations"
)
foreach ($path in $jumpListPaths) {
if (Test-Path $path) {
Remove-Item "$path\*" -Force -ErrorAction SilentlyContinue
Write-Host "已清理跳转列表: $path" -ForegroundColor Green
}
}
# 清除开始菜单中的最近文档记录
$startRecentPaths = @(
"$env:APPDATA\Microsoft\Windows\Recent\AutomaticDestinations"
)
foreach ($path in $startRecentPaths) {
if (Test-Path $path) {
Get-ChildItem -Path $path -Include *.automaticDestinations-ms | Remove-Item -Force
}
}
Write-Host "最近文档清理完成!" -ForegroundColor Green
批处理脚本 (CMD)
@echo off清理最近文档
color 0A
echo 正在清理最近文档...
echo.
:: 清理当前用户最近文档
if exist "%APPDATA%\Microsoft\Windows\Recent\*" (
del /f /q "%APPDATA%\Microsoft\Windows\Recent\*" 2>nul
echo ✓ 已清理最近文档
)
:: 清理跳转列表
if exist "%APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations\*" (
del /f /q "%APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations\*" 2>nul
echo ✓ 已清理自动跳转列表
)
if exist "%APPDATA%\Microsoft\Windows\Recent\CustomDestinations\*" (
del /f /q "%APPDATA%\Microsoft\Windows\Recent\CustomDestinations\*" 2>nul
echo ✓ 已清理自定义跳转列表
)
echo.
echo 清理完成!
pause
macOS系统
AppleScript或Shell脚本
#!/bin/bash
# 清理macOS最近使用的文档
# 保存为 clean-recent-docs.sh
echo "开始清理最近文档..."
# 清理Finder最近使用的文件
defaults delete com.apple.finder RecentSearches 2>/dev/null
defaults write com.apple.finder FK_RecentDocuments -dict-add history -string ""
defaults write NSGlobalDomain NSRecentDocumentsLimit 0
# 清理特定应用最近文档记录
killall cfprefsd 2>/dev/null
# 清理Finder的最近文件夹
REALM_PATH="$HOME/Library/Application Support/com.apple.sharedfilelist"
if [ -d "$REALM_PATH" ]; then
find "$REALM_PATH" -name "*.sfl3" -delete 2>/dev/null
find "$REALM_PATH" -name "*.sfl2" -delete 2>/dev/null
echo "✓ 已清理最近文件夹记录"
fi
# 清理最近应用记录
APPS_REALM="$HOME/Library/Application Support/com.apple.recentitems"
if [ -d "$APPS_REALM" ]; then
rm -rf "$APPS_REALM"/* 2>/dev/null
echo "✓ 已清理最近应用记录"
fi
# 重启Finder
killall Finder
echo "清理完成!"
Linux系统
Bash脚本
#!/bin/bash
# 清理Linux最近的文档记录
# 保存为 clean-recent-docs.sh
echo "开始清理最近的文档..."
# 清理主目录下的recently-used.xbel
if [ -f "$HOME/.local/share/recently-used.xbel" ]; then
rm -f "$HOME/.local/share/recently-used.xbel"
echo "✓ 已清理recently-used.xbel"
fi
# 清理gtk最近文档缓存
if [ -d "$HOME/.local/share/Trash" ]; then
find "$HOME/.local/share/Trash" -name "*recent*" -delete 2>/dev/null
fi
# 清理各种文件管理器的最近记录
# Nautilus (GNOME文件)
if [ -f "$HOME/.local/share/gnome-shell/extensions" ]; then
gvfs-trash --empty 2>/dev/null
fi
# Dolphin (KDE文件)
if [ -d "$HOME/.kde4/share/apps/dolphin" ]; then
rm -f "$HOME/.kde4/share/apps/dolphin/recentlyused.xml" 2>/dev/null
fi
# 清理bash历史(可选)
# history -c
echo "清理完成!"
高级功能:定时自动清理
Windows任务计划程序设置
# 设置每天自动清理最近文档 $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-ExecutionPolicy Bypass -File C:\Scripts\Clean-RecentDocs.ps1" $trigger = New-ScheduledTaskTrigger -Daily -At "09:00" $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount $settings = New-ScheduledTaskSettingsSet Register-ScheduledTask -TaskName "清理最近文档" -Action $action -Trigger $trigger -Principal $principal -Settings $settings
Linux Crontab定时任务
# 添加定时任务,每天9点清理 crontab -e # 添加以下行 0 9 * * * /home/user/scripts/clean-recent-docs.sh
使用建议
- 权限要求:某些操作需要管理员/root权限,请确保以合适权限运行
- 备份建议:首次运行前建议备份相关文件夹
- 测试环境:先在测试环境运行,确认无误后再部署到生产环境
- 日志记录:建议在脚本中添加日志功能,记录清理过程
# 启用日志记录的示例 LOGFILE="/var/log/clean-recent.log" echo "$(date) 开始清理" >> $LOGFILE # 清理操作... echo "$(date) 清理完成" >> $LOGFILE
根据您的具体需求,可以选择合适的系统脚本,或结合多种方法进行清理,如果需要更详细的定制,请告诉我您的具体使用场景。