使用python定时复制同种命名格式文件

  1. 脚本介绍
    将数据库每天生成的数据库备份文件拷贝到指定网络文件夹
    (1)需要构造filename文件格式–同时获取当天的日期进行文件名替换
    (2)构造可视化操作界面
    (3)设置执行时间,每天执行一次。
    2.操作界面
    在这里插入图片描述

3.实现代码

import os
import shutil
import time
from datetime import datetime, timedelta, date
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import schedule
import threading

def copy_files_to_network_share(source_dir, target_dir):
    today = date.today()
    today_str = today.strftime("%Y-%m-%d")

    for filename in os.listdir(source_dir):
        if filename.startswith("HNJC_" + today_str):
            source_file = os.path.join(source_dir, filename)
            target_file = os.path.join(target_dir, filename)
            shutil.copy(source_file, target_file)
            print(f"文件 {filename} 已成功复制到目标目录!")

def select_source_directory():
    global source_directory
    source_directory = filedialog.askdirectory()
    source_dir_label.config(text=source_directory)

def select_target_directory():
    global target_directory
    target_directory = filedialog.askdirectory()
    target_dir_label.config(text=target_directory)

def start_copy():
    if source_directory and target_directory:
        copy_files_to_network_share(source_directory, target_directory)
        status_label.config(text="复制完成!")
    else:
        status_label.config(text="请选择源目录和目标目录!")

def set_execution_time():
    global execution_time
    execution_time = time_combobox.get()
    schedule.clear()
    schedule.every().day.at(execution_time).do(start_copy)
    status_label.config(text="成功设置定时任务!")

def run_scheduled_job():
    def job_thread():
        while True:
            schedule.run_pending()
            time.sleep(1)

    # 创建并启动定时任务线程
    thread = threading.Thread(target=job_thread)
    thread.daemon = True
    thread.start()

# 创建主窗口
window = tk.Tk()
window.title("文件复制工具")

# 创建选择源目录按钮和标签
source_dir_button = tk.Button(window, text="选择源目录", command=select_source_directory)
source_dir_button.pack()
source_dir_label = tk.Label(window, text="请选择源目录")
source_dir_label.pack()

# 创建选择目标目录按钮和标签
target_dir_button = tk.Button(window, text="选择目标目录", command=select_target_directory)
target_dir_button.pack()
target_dir_label = tk.Label(window, text="请选择目标目录")
target_dir_label.pack()

# 创建执行时间选择框
time_combobox = ttk.Combobox(window, values=["00:00", "01:00", "02:00", "03:00", "04:00"])
time_combobox.current(0)
time_combobox.pack()

# 创建设置执行时间按钮
set_time_button = tk.Button(window, text="设置执行时间", command=set_execution_time)
set_time_button.pack()

# 创建开始复制按钮
copy_button = tk.Button(window, text="开始复制", command=start_copy)
copy_button.pack()

# 创建定时执行按钮
schedule_button = tk.Button(window, text="定时执行", command=run_scheduled_job)
schedule_button.pack()

# 创建状态标签
status_label = tk.Label(window, text="")
status_label.pack()

# 运行主循环
window.mainloop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MD5校验码:f4f9ea3f7bcc3375192be61dc110cb58 1、本软件是定时自动备份软件。 2、备份任务自动拷贝文件文件大小或修改时间变化的文件 3、MyCopy.exe是配置界面,该文件生成配置文件mycopy.ini,并能显示系统运行状态。 4、MyCopyTask.exe是执行拷贝任务的程序,它根据mycopy.ini配置的信息定时进行拷贝,在休眠状态下每5秒钟检查一次是否到达任务指定的拷贝时间。拷贝过程中出现错误,记录log.ini文件,但不会终止拷贝进程。 5、MyCopy.exe配置界面里有“启动”按钮把运行状态改为“运行”,并启动MyCopyTask.exe程序;“终止”按钮把运行状态改为“终止”,MyCopyTask.exe检测到系统状态时为“终止”则自动退出。“退出”按钮退出配置界面,但不会终止MyCopyTask.exe程序。“暂停”也会推出MyCopyTask.exe程序,但下次启动时,从上次暂停的任务开始继续拷贝。 6、MyCopyTask.exe运行时托盘上会显示图标。 7、要实现拷贝任务的自动启动,可以把MyCopyTask.exe加入到windows系统的“启动”菜单中,但配置文件中的运行状态一定是“启动”,否则程序会自动退出。 8、程序拷贝文件时意外终止,可以坚持mycopy.ini文件中的[系统状态]是否有“半个文件”,如果有说明“运行信息”中包含的文件没有拷贝完整。 9、如果因为某种原因错过了任务执行时机的话,拷贝程序会在启动后补回错过的拷贝任务。 10、任务名和文件路径中不能出现","(半角逗号) 11、标准版只记录log.ini文件,健康提示版当任务执行完之后会弹出提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值