|
本帖最后由 BestMiniPE 于 2025-2-21 00:36 编辑
思路不错,改了一下,可以编译,功能暂时没测试。
import os
import re
import datetime
import hashlib
import time
import tkinter as tk
from tkinter import filedialog
from PIL import Image
#from tkPDFViewer2 import tkPDFViewer2 as pdf
#from cadviewer import CADViewer
#from videoplayer import VideoPlayer
#import py7zlib
import zipfile
import rarfile
#import isomounter
#import search
#import unlock
import clean
import zlib # 导入 zlib 模块用于计算 CRC32 值
import shutil # 导入 shutil 模块用于文件操作
def get_md5_value(file_path):
try:
with open(file_path, 'rb') as f: # 以二进制模式打开文件
md5 = hashlib.md5(f.read()).hexdigest()
return md5
except Exception as e:
print(f"计算 MD5 值时出错: {e}")
return None
def get_sha256_value(file_path):
try:
with open(file_path, 'rb') as f: # 以二进制模式打开文件
sha256 = hashlib.sha256(f.read()).hexdigest()
return sha256
except Exception as e:
print(f"计算 SHA256 值时出错: {e}")
return None
def get_crc32_value(file_path):
try:
with open(file_path, 'rb') as f: # 以二进制模式打开文件
crc32 = zlib.crc32(f.read()) # 使用 zlib.crc32 计算 CRC32 值
return crc32
except Exception as e:
print(f"计算 CRC32 值时出错: {e}")
return None
def rename_files_with_gui(directory, pattern, replacement, date_format, new_suffix, include_size, include_original_date, include_md5, include_sha256, include_crc32, change_original_time):
files = os.listdir(directory)
current_date = datetime.datetime.now().strftime(date_format)
for filename in files:
file_path = os.path.join(directory, filename)
if re.match(pattern, filename):
new_filename = re.sub(pattern, replacement, filename)
if date_format:
new_filename = new_filename + "_" + current_date
if include_size:
file_size = os.path.getsize(file_path)
new_filename = new_filename + "_Size_" + str(file_size)
if include_original_date:
file_date = datetime.datetime.fromtimestamp(os.path.getctime(file_path)).strftime("%Y-%m-%d")
new_filename = new_filename + "_OrigDate_" + file_date
if include_md5:
md5_value = get_md5_value(file_path)
new_filename = new_filename + "_MD5_" + md5_value
if include_sha256:
sha256_value = get_sha256_value(file_path)
new_filename = new_filename + "_SHA256_" + sha256_value
if include_crc32:
crc32_value = get_crc32_value(file_path)
new_filename = new_filename + "_CRC32_" + str(crc32_value)
if new_suffix:
old_suffix = os.path.splitext(filename)[1]
new_filename = os.path.splitext(new_filename)[0] + new_suffix
new_file_path = os.path.join(directory, new_filename)
os.rename(file_path, new_file_path)
if change_original_time:
new_time = time.time()
os.utime(new_file_path, (new_time, new_time))
print(f"重命名: {filename} -> {new_filename}")
else:
print(f"跳过: {filename}(不符合模式)")
def compare_files(file1, file2):
file1_info = {
"文件名": os.path.basename(file1),
"创建时间": datetime.datetime.fromtimestamp(os.path.getctime(file1)).strftime("%Y-%m-%d %H:%M:%S"), # 补充键名
"修改时间": datetime.datetime.fromtimestamp(os.path.getmtime(file1)).strftime("%Y-%m-%d %H:%M:%S"),
"MD5值": get_md5_value(file1),
"SHA256值": get_sha256_value(file1),
"CRC32值": get_crc32_value(file1),
}
file2_info = {
"文件名": os.path.basename(file2),
"创建时间": datetime.datetime.fromtimestamp(os.path.getctime(file2)).strftime("%Y-%m-%d %H:%M:%S"),
"修改时间": datetime.datetime.fromtimestamp(os.path.getmtime(file2)).strftime("%Y-%m-%d %H:%M:%S"),
"MD5值": get_md5_value(file2),
"SHA256值": get_sha256_value(file2),
"CRC32值": get_crc32_value(file2),
}
differences = {}
for key in file1_info:
if file1_info[key] != file2_info[key]:
differences[key] = (file1_info[key], file2_info[key])
return differences
def show_file_type(event):
# 这里需要根据实际情况修改,event.widget 并没有 get_selected_file_path 和 show_tooltip 方法
print("需要根据实际情况实现获取文件路径和显示提示信息的功能")
def preview_file_content(file_path):
file_type = os.path.splitext(file_path)[1]
if file_type in [".jpg", ".png", ".bmp"]:
Image.open(file_path).show()
#elif file_type == ".pdf":
#pdf.open(file_path)
#elif file_type in [".dwg", ".dxf"]:
#CADViewer.open(file_path)
#elif file_type in [".mp4", ".avi", ".mov"]:
#VideoPlayer.open(file_path)
elif file_type in [".doc", ".xls", ".ppt"]:
os.startfile(file_path)
else:
print(f"不支持预览的文件类型: {file_type}")
def copy_file(file_path, target_path):
shutil.copy2(file_path, target_path) # 使用 shutil.copy2 进行文件复制
print(f"复制: {file_path} -> {target_path}")
def cut_file(file_path, target_path):
shutil.move(file_path, target_path) # 使用 shutil.move 进行文件移动(剪切)
print(f"剪切: {file_path} -> {target_path}")
def delete_file(file_path):
os.remove(file_path) # 使用 os.remove 进行文件删除
print(f"删除: {file_path}")
def move_file(file_path, target_path):
shutil.move(file_path, target_path)
print(f"移动: {file_path} -> {target_path}")
#def compress_to_7z(file_path, output_path, password=None):
#py7zlib.compress(file_path, output_path, compression_level="max", password=password)
#print(f"压缩: {file_path} -> {output_path}")
#def #decompress_7z(file_path, output_path, password=None):
#py7zlib.decompress(file_path, output_path, password=password)
#print(f"解压: {file_path} -> {output_path}")
#def mount_iso(iso_path):
#isomounter.mount(iso_path)
#print(f"挂载: {iso_path}")
#def search_files(directory, keyword):
#results = search.search(directory, keyword)
#for result in results:
#print(f"搜索结果: {result}")
#def unlock_file(file_path):
#unlock.unlock(file_path)
#print(f"解锁: {file_path}")
def clean_system():
clean.clean_memory()
clean.clean_system_garbage()
clean.clean_cache()
print("系统清理完成")
def optimize_software():
clean.optimize()
print("软件优化完成")
def main():
root = tk.Tk()
root.title("钢筋翻样徐林林 文件重命名")
contact_label = tk.Label(root, text="微信QQ:9632xxx 手机:1583561xxxx")
contact_label.pack()
directory_label = tk.Label(root, text="文件夹路径:")
directory_label.pack()
directory_entry = tk.Entry(root)
directory_entry.pack()
directory_button = tk.Button(root, text="选择文件夹", command=lambda: directory_entry.insert(0, filedialog.askdirectory()))
directory_button.pack()
pattern_label = tk.Label(root, text="正则表达式模式:")
pattern_label.pack()
pattern_entry = tk.Entry(root)
pattern_entry.pack()
replacement_label = tk.Label(root, text="替换规则:")
replacement_label.pack()
replacement_entry = tk.Entry(root)
replacement_entry.pack()
date_format_label = tk.Label(root, text="日期格式:")
date_format_label.pack()
date_format_entry = tk.Entry(root)
date_format_entry.pack()
new_suffix_label = tk.Label(root, text="新后缀:")
new_suffix_label.pack()
new_suffix_entry = tk.Entry(root)
new_suffix_entry.pack()
include_size_var = tk.IntVar() # 创建 IntVar 变量用于存储复选框状态
include_size_checkbox = tk.Checkbutton(root, text="包含文件大小", variable=include_size_var)
include_size_checkbox.pack()
include_original_date_var = tk.IntVar()
include_original_date_checkbox = tk.Checkbutton(root, text="包含原始日期", variable=include_original_date_var)
include_original_date_checkbox.pack()
include_md5_var = tk.IntVar()
include_md5_checkbox = tk.Checkbutton(root, text="包含MD5值", variable=include_md5_var)
include_md5_checkbox.pack()
include_sha256_var = tk.IntVar()
include_sha256_checkbox = tk.Checkbutton(root, text="包含SHA256值", variable=include_sha256_var)
include_sha256_checkbox.pack()
include_crc32_var = tk.IntVar()
include_crc32_checkbox = tk.Checkbutton(root, text="包含CRC32值", variable=include_crc32_var)
include_crc32_checkbox.pack()
change_original_time_var = tk.IntVar()
change_original_time_checkbox = tk.Checkbutton(root, text="改变文件原始时间", variable=change_original_time_var)
change_original_time_checkbox.pack()
rename_button = tk.Button(root, text="重命名", command=lambda: rename_files_with_gui(
directory_entry.get(),
pattern_entry.get(),
replacement_entry.get(),
date_format_entry.get(),
new_suffix_entry.get(),
include_size_var.get(),
include_original_date_var.get(),
include_md5_var.get(),
include_sha256_var.get(),
include_crc32_var.get(),
change_original_time_var.get()
))
rename_button.pack()
file_listbox = tk.Listbox(root)
file_listbox.pack()
file_listbox.bind("<Button-1>", show_file_type) # 使用具体的鼠标左键单击事件名称
compare_button = tk.Button(root, text="文件对比", command=lambda: compare_files(filedialog.askfile(), filedialog.askfile()))
compare_button.pack()
preview_button = tk.Button(root, text="预览文件", command=lambda: preview_file_content(filedialog.askfile()))
preview_button.pack()
copy_button = tk.Button(root, text="复制", command=lambda: copy_file(filedialog.askfile(), filedialog.askdirectory()))
copy_button.pack()
cut_button = tk.Button(root, text="剪切", command=lambda: cut_file(filedialog.askfile(), filedialog.askdirectory()))
cut_button.pack()
delete_button = tk.Button(root, text="删除", command=lambda: delete_file(filedialog.askfile()))
delete_button.pack()
move_button = tk.Button(root, text="移动", command=lambda: move_file(filedialog.askfile(), filedialog.askdirectory()))
move_button.pack()
compress_button = tk.Button(root, text="压缩", command=lambda: compress_to_7z(filedialog.askfile(), filedialog.asksavefilename()))
compress_button.pack()
decompress_button = tk.Button(root, text="解压", command=lambda: decompress_7z(filedialog.askfile(), filedialog.askdirectory()))
decompress_button.pack()
mount_button = tk.Button(root, text="挂载镜像", command=lambda: mount_iso(filedialog.askfile()))
mount_button.pack()
keyword_label = tk.Label(root, text="搜索关键字:")
keyword_label.pack()
keyword_entry = tk.Entry(root)
keyword_entry.pack()
search_button = tk.Button(root, text="搜索", command=lambda: search_files(directory_entry.get(), keyword_entry.get()))
search_button.pack()
unlock_button = tk.Button(root, text="解锁文件", command=lambda: unlock_file(filedialog.askfile()))
unlock_button.pack()
clean_button = tk.Button(root, text="系统清理", command=lambda: clean_system())
clean_button.pack()
optimize_button = tk.Button(root, text="优化软件", command=lambda: optimize_software())
optimize_button.pack()
root.mainloop()
if __name__ == "__main__":
main()
|
|