自制文件批量重命名EXE文件

在日常的电脑操作中,文件夹批量重命名常常让人感到无比困扰。想象一下,当您面对数十个甚至上百个需要统一规范命名的文件夹时,传统的逐个重命名方式简直是一场噩梦。不仅操作繁琐,还极易出现错误,耗费大量的时间和精力。

例如,在整理工作项目的文件时,各个阶段的文件夹命名混乱,需要统一改成特定的格式,如 “项目名称 – 阶段 – 日期”。如果没有便捷的批量重命名工具或功能,您只能一个一个地手动修改,这过程中可能会因为粗心导致命名不一致,影响后续的查找和使用。

基于此,我自制了一个自用的文件批量命名软件,适合windows系统,先看使用视频:

1.使用视频

以下是全部代码:

2.入口文件

"""
Author:Liao 
RanDate:2024.8.19
Website:https://www.lrxy.site
"""

# main.py
#引入
# 导入布局文件
from ui import Win as MainWin
# 导入窗口控制器
from Cotroller import Controller as MainUIController

# 将窗口控制器 传递给UI
app = MainWin(MainUIController())

if __name__ == "__main__":
    # 启动
    app.mainloop()

3.ui文件

"""
Author:Liao Ran
Date:2024.8.19
Website:https://www.lrxy.site
"""

from tkinter import *
from tkinter import filedialog
from tkinter.ttk import *
from tkinter import messagebox
from ttkbootstrap import Style


class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_button_choice_file = self.__tk_button_choice_file(self)
        self.tk_label_file_path = self.__tk_label_file_path(self)
        self.tk_input_suffix_name = self.__tk_input_suffix_name(self)
        self.tk_label_lzy43zot = self.__tk_label_lzy43zot(self)
        self.tk_button_start_rename = self.__tk_button_start_rename(self)
        self.tk_label_start_name = self.__tk_label_start_name(self)
        self.tk_input_lzy5i2dh = self.__tk_input_lzy5i2dh(self)
        self.tk_label_end_name=self.__tk_label_end_name(self)
        self.tk_end_name=self.__tk_input_endname(self)

        #样式
        self.style = Style()
        self.style.theme_use('minty')


    def __win(self):
        self.title("文件重命名")
        # 设置窗口大小、居中
        width = 600
        height = 350
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)
        self.iconbitmap('icon/logo.ico')
        self.resizable(width=False, height=False)

    def scrollbar_autohide(self, vbar, hbar, widget):
        """自动隐藏滚动条"""

        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)

        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)

        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())

    def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')

    def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')

    def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)

    def __tk_button_choice_file(self, parent):
        btn = Button(parent, text="选择文件夹", takefocus=False, )
        btn.place(x=41, y=30, width=100, height=30)
        return btn

    def __tk_label_file_path(self, parent):
        label = Label(parent, text="文件夹路径……", anchor="center", )
        label.place(x=170, y=30, width=300, height=30)
        return label

    def __tk_input_suffix_name(self, parent):
        ipt = Entry(parent, )
        ipt.place(x=180, y=80, width=300, height=30)
        return ipt

    def __tk_label_lzy43zot(self, parent):
        label = Label(parent, text="筛选后缀名(eg:jpg)", anchor="center", )
        label.place(x=40, y=80, width=120, height=30)
        return label



    def __tk_label_start_name(self, parent):
        label = Label(parent, text="数字前", anchor="center", )
        label.place(x=40, y=130, width=104, height=30)
        return label

    def __tk_input_lzy5i2dh(self, parent):
        ipt = Entry(parent, )
        ipt.place(x=180, y=130, width=300, height=30)
        return ipt

    def __tk_label_end_name(self, parent):
        label = Label(parent, text="数字后", anchor="center", )
        label.place(x=40, y=180, width=104, height=30)
        return label

    def __tk_input_endname(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=180, y=180, width=300, height=30)
        return ipt

    def __tk_button_start_rename(self, parent):
        btn = Button(parent, text="开始", takefocus=False, )
        btn.place(x=250, y=250, width=50, height=30)
        return btn
    #---------------------------------
    def get_user_path(self):
        user_pth = filedialog.askdirectory()
        self.tk_label_file_path.config(text=f"选择的文件夹路径: {str(user_pth)}")
        return user_pth

    def get_suffix_name(self):
        suffix_name=self.tk_input_suffix_name.get()
        return suffix_name
    def get_start_name(self):
        start_name=self.tk_input_lzy5i2dh.get()
        return start_name
    def get_end_name(self):
        end_name=self.tk_end_name.get()
        return end_name
    def mes_info(self,info):
        messagebox.showinfo('提示',f'{info}')

class Win(WinGUI):
    def __init__(self, controller):
        self.ctl = controller
        super().__init__()
        self.__event_bind()
        self.__style_config()
        self.ctl.init(self)

    def __event_bind(self):
        self.tk_button_choice_file.bind('<Button>', self.ctl.choice_file)
        self.tk_button_start_rename.bind('<Button>', self.ctl.start_rename)
        pass

    def __style_config(self):
        pass

4.逻辑文件

"""
Author:Liao Ran
Date:2024.8.19
Website:https://www.lrxy.site
"""

import os
from ui import Win
from tkinter import filedialog

class Controller:
    # 导入UI类后,替换以下的 object 类型,将获得 IDE 属性提示功能
    ui: Win

    def __init__(self):
        self.file_path = None
        pass

    def init(self, ui):
        """
        得到UI实例,对组件进行初始化配置
        """
        self.ui = ui
        # TODO 组件初始化 赋值操作

    def choice_file(self, evt):
        # print("<Button>事件未处理:",evt)
        user_path = self.ui.get_user_path()
        self.file_path = user_path

    def start_rename(self, evt):
        suffix_name = self.ui.get_suffix_name()
        start_name = self.ui.get_start_name()
        end_name = self.ui.get_end_name()
        if suffix_name and self.file_path:
            self.batch_rename_files(folder_path=self.file_path, target_suffix=suffix_name, start_name=start_name,
                                    end_name=end_name)

        else:
            self.ui.mes_info(info='路径或命名错误,请重试!')
            return

    def batch_rename_files(self, folder_path, target_suffix, start_name, end_name):
        count = 1
        # 遍历文件夹中的所有文件和子文件夹
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                # 获取文件的完整路径
                file_path = os.path.join(root, file)

                # 检查文件后缀是否符合要求
                if file.endswith(target_suffix):
                    # 获取文件名(不包含后缀)
                    file_name_without_suffix = os.path.splitext(file)[0]
                    # 生成新的文件名(按顺序的数字)
                    new_file_name = f"{start_name}{count}{end_name}.{target_suffix}"
                    # 生成新文件的完整路径
                    new_file_path = os.path.join(root, new_file_name)
                    # 重命名文件
                    os.rename(file_path, new_file_path)
                    count += 1
                else:
                    continue

            self.ui.mes_info(info='重命名成功啦!')

5.下载

下载链接:自制文件批量重命名EXE文件(123网盘)

提取码:IIHR

评论 “自制文件批量重命名EXE文件

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注