如何写一个脚本定时清理图标缓存

wen 实用脚本 1

本文目录导读:

如何写一个脚本定时清理图标缓存

  1. Windows 系统图标缓存清理
  2. macOS 系统图标缓存清理
  3. Linux 系统图标缓存清理
  4. 定时任务配置
  5. 使用注意事项
  6. 使用方法

Windows 系统图标缓存清理

PowerShell 脚本 (Clean-IconCache.ps1)

# 图标缓存清理脚本
$ErrorActionPreference = "Stop"
# 停止资源管理器进程
Write-Host "正在停止资源管理器..." -ForegroundColor Yellow
Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 2
# 删除图标缓存文件
$cachePaths = @(
    "$env:LOCALAPPDATA\IconCache.db",
    "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\iconcache*",
    "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\thumbcache_*.db",
    "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\thumbcache_idx.db"
)
Write-Host "正在删除图标缓存文件..." -ForegroundColor Yellow
foreach ($path in $cachePaths) {
    if (Test-Path $path) {
        Remove-Item -Path $path -Force -Recurse -ErrorAction SilentlyContinue
        Write-Host "已删除: $path" -ForegroundColor Green
    }
}
# 重建图标缓存
Write-Host "正在重建图标缓存..." -ForegroundColor Yellow
ie4uinit.exe -show
# 重启资源管理器
Write-Host "正在重启资源管理器..." -ForegroundColor Yellow
Start-Process explorer.exe
Write-Host "图标缓存清理完成!" -ForegroundColor Green

macOS 系统图标缓存清理

Shell 脚本 (clean_icon_cache.sh)

#!/bin/bash
echo "开始清理 macOS 图标缓存..."
# 清除 Finder 图标缓存
echo "清除 Finder 图标缓存..."
sudo find /private/var/folders/ -name "com.apple.dock.iconcache" -exec rm -f {} \; 2>/dev/null
sudo find /private/var/folders/ -name "com.apple.iconservices" -exec rm -rf {} \; 2>/dev/null
# 清除系统图标缓存
echo "清除系统图标缓存..."
sudo rm -rf /Library/Caches/com.apple.iconservices.store
sudo rm -rf /System/Library/Caches/com.apple.iconservices.store
# 重置 Dock 和 Finder
echo "重置 Dock 和 Finder..."
killall Dock
killall Finder
echo "图标缓存清理完成!"
echo "建议重启电脑以完全生效。"

Linux 系统图标缓存清理

Shell 脚本 (clean_linux_icon_cache.sh)

#!/bin/bash
echo "开始清理 Linux 图标缓存..."
# 清理 GTK 图标缓存
echo "清理 GTK 图标缓存..."
sudo gtk-update-icon-cache -f /usr/share/icons/hicolor
sudo gtk-update-icon-cache -f /usr/share/icons/Adwaita
# 清理用户级图标缓存
echo "清理用户级图标缓存..."
rm -rf ~/.cache/icon-cache.kcache
rm -rf ~/.thumbnails/*
rm -rf ~/.cache/thumbnails/*
rm -rf ~/.cache/mozilla/firefox/*.default/thumbnails/*
# 清理 freedesktop 图标缓存
echo "清理 freedesktop 图标缓存..."
rm -rf ~/.cache/icon-theme.cache
# 清理应用特定缓存
echo "清理应用缓存..."
rm -rf ~/.cache/unity/icons/*
rm -rf ~/.cache/gnome-software/icons/*
echo "图标缓存清理完成!"

定时任务配置

Windows 任务计划程序设置

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <Triggers>
    <Weekly>
      <DaysOfWeek>
        <Sunday/>
      </DaysOfWeek>
      <WeeksInterval>1</WeeksInterval>
    </Weekly>
  </Triggers>
  <Actions>
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>-ExecutionPolicy Bypass -File "C:\Scripts\Clean-IconCache.ps1"</Arguments>
    </Exec>
  </Actions>
</Task>

Linux Crontab 配置

# 每周日凌晨3点清理
0 3 * * 0 /path/to/clean_linux_icon_cache.sh
# 或者使用 systemd timer
# /etc/systemd/system/clean-icon-cache.service
[Unit]
Description=Clean Icon Cache
[Service]
Type=oneshot
ExecStart=/path/to/clean_linux_icon_cache.sh
# /etc/systemd/system/clean-icon-cache.timer
[Unit]
Description=Weekly icon cache cleanup
[Timer]
OnCalendar=weekly
Persistent=true
[Install]
WantedBy=timers.target

使用注意事项

权限设置

# Linux/Mac 设置执行权限
chmod +x clean_icon_cache.sh
# Windows PowerShell 执行策略
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

日志记录(可选)

# PowerShell 添加日志
$logFile = "$env:TEMP\IconCacheClean.log"
Start-Transcript -Path $logFile -Append
# ... 脚本内容 ...
Stop-Transcript

错误处理

# 添加错误处理
if [ $? -ne 0 ]; then
    echo "错误:图标缓存清理失败" >&2
    exit 1
fi

使用方法

  1. Windows: 以管理员身份运行 PowerShell 脚本
  2. macOS/Linux: 给予执行权限后运行
  3. 定时任务: 根据系统配置相应的定时任务

这些脚本会帮你自动清理系统图标缓存,解决图标显示异常或损坏的问题,建议根据你的具体需求选择合适的版本。

抱歉,评论功能暂时关闭!