Xmind 应用程序使用文件缓存来恢复 Xmind 文件的先前版本。但是,随着您多次保存,这些缓存文件的存储空间会显著增长(>1~100GB)。此行为会导致冗余文件版本的积累,使早期版本与当前版本越来越不相关。
为了解决这个问题,我实现了以下 python 代码来删除不必要的旧版本文件,只保留最新的 5 个版本。
请记得在python文件添加路径(ie.你的xmind cache目录和日志文件)
请通过 Windows PowerShell 执行并输入以下语句:python <您的 py 文件路径>
delete_file_xmind.py
code:
import os
import send2trash
from datetime import datetime
# 设置要处理的目录和日志文件
target_dir = r"<xmind file cache path >" #your xmind path is usually : C:\Users\< userName >\AppData\Roaming\Xmind\Electron v3\vana\file-cache
log_file = r"<your log file path with name deleted_files_log_xmind >" #your log file path, make sure you have create the txt file(eg. deleted_files_log_xmind.txt)
# 清空日志文件
with open(log_file, 'w') as f:
f.write("")
# 总删除计数
total_deleted = 0
# 遍历所有子文件夹
for root, dirs, files in os.walk(target_dir):
if len(files) > 5:
# 记录当前文件夹
folder_path = root
# 获取文件的完整路径和修改时间
files_with_time = [
(os.path.join(root, file), os.path.getmtime(os.path.join(root, file)))
for file in files
]
# 按修改时间排序(最新的在前)
files_with_time.sort(key=lambda x: x[1], reverse=True)
# 计算需要删除的文件数量
files_to_delete = files_with_time[5:]
counter = 0
for file_path, _ in files_to_delete:
try:
# 移动文件到回收站
send2trash.send2trash(file_path)
# 获取当前时间
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(log_file, 'a') as f:
f.write(f"{now} - Deleted: {file_path}\n")
counter += 1
total_deleted += 1
except Exception as e:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(log_file, 'a') as f:
f.write(f"{now} - Failed to delete {file_path}: {str(e)}\n")
# 输出已删除文件的数量
with open(log_file, 'a') as f:
f.write(f"Deleted {counter} files from {folder_path}\n")
# 记录总删除数量
with open(log_file, 'a') as f:
f.write(f"Total deleted files: {total_deleted}\n")