import os
import shutil
import time
import sys
from datetime import datetime, timedelta
try:
pass
except ImportError:
pass
def get_folder_size(folder_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(folder_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
def list_wechat_folders(base_path):
wechat_folders = {}
for folder in os.listdir(base_path):
if len(folder) == 16 and folder.isdigit():
folder_path = os.path.join(base_path, folder)
folder_size = get_folder_size(folder_path)
wechat_folders[folder] = folder_size
return wechat_folders
def delete_old_cache_files(cache_path, days_to_keep):
current_time = datetime.now()
cutoff_time = current_time - timedelta(days=days_to_keep)
subfolders_to_clean = ['File', 'Image', 'Video', 'Voice']
total_files = 0
deleted_files_count = 0
freed_space = 0
for root, dirs, files in os.walk(cache_path):
for dir_name in dirs:
if any(subfolder in root for subfolder in subfolders_to_clean) and '-' in dir_name:
dir_path = os.path.join(root, dir_name)
total_files += len([f for f in os.listdir(dir_path) if os.path.isfile(os.path.join(dir_path, f))])
for root, dirs, files in os.walk(cache_path, topdown=False):
for dir_name in dirs:
if any(subfolder in root for subfolder in subfolders_to_clean) and '-' in dir_name:
dir_path = os.path.join(root, dir_name)
for file_name in os.listdir(dir_path):
file_path = os.path.join(dir_path, file_name)
file_time = datetime.fromtimestamp(os.path.getmtime(file_path))
if file_time < cutoff_time:
try:
file_size = os.path.getsize(file_path)
os.remove(file_path)
deleted_files_count += 1
freed_space += file_size
print(f"已删除文件: {file_path}")
except Exception as e:
print(f"无法删除文件 {file_path}: {e}")
if not os.listdir(dir_path):
try:
os.rmdir(dir_path)
except Exception as e:
print(f"无法删除空文件夹 {dir_path}: {e}")
print(f"共删除 {deleted_files_count} 个文件,释放了 {freed_space} 字节的空间。")
def main():
input("欢迎使用Clean My WXWork v1.0--企业微信清理缓存工具,按回车键扫描企业微信缓存文件。")
try:
default_base_path = "D:\\Users\\Administrator\\Documents\\WXWork"
base_path = default_base_path
wechat_folders = list_wechat_folders(base_path)
if not wechat_folders or all(size < 100 * 1024 * 1024 for size in wechat_folders.values()):
base_path = input(
"默认路径下未找到合适的企业微信号文件夹,请手动输入企业微信缓存文件目录,例如:D:\\Users\\Administrator\\Documents\\WXWork: ")
wechat_folders = list_wechat_folders(base_path)
else:
print("企业微信缓存文件为默认路径,现在开始扫描对应企业微信号文件夹")
print("\n\n")
sorted_wechat_folders = sorted(wechat_folders.items(), key=lambda x: x[1], reverse=True)
table_data = [[i, folder, size / (1024 * 1024)] for i, (folder, size) in
enumerate(sorted_wechat_folders, start=1)]
print(f"{'序号':<10}{'企微号':<25}{'文件大小(MB)':<20}")
print("=" * 55)
for index, folder, size_mb in table_data:
print(f"{index:<10}{folder:<25}{size_mb:<20.2f}")
while True:
try:
selected_index = int(input("请输入要删除缓存的序号,通常选择文件大的企微号:")) - 1
if 0 <= selected_index < len(sorted_wechat_folders):
selected_folder, _ = sorted_wechat_folders[selected_index]
cache_path = os.path.join(base_path, selected_folder, 'Cache')
if os.path.exists(cache_path):
days_to_keep = int(input("想要保留几天之内的缓存文件:"))
delete_old_cache_files(cache_path, days_to_keep)
print("缓存文件删除成功!")
else:
print("未找到Cache文件夹。")
break
else:
print("输入的序号无效,请重新输入。")
except ValueError:
print("请输入一个有效的数字序号。")
except Exception as e:
print(f"程序运行时发生错误: {e}")
finally:
print("程序已退出。")
input("按任意键继续...")
sys.exit(0)
if __name__ == "__main__":
main()