win10拷贝文件时实现自定义的重命名

 我的需求是将《我的需求.txt》在按住ctrl键通过鼠标进行复制时,自动改为《我的需求2021年2月21日20点35分41秒.txt》,即原本的文件名+复制时的时间,实现备份的目的

经过查阅资料,发现可以在修改HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\NamingTemplates下的NamingTemplates来实现,但是网上并没有我符合我需求的教程。

后来我决定用c#来写,后来又写了现在这个python版本的程序

我的方法是通过程序每秒钟修改NamingTemplates的值来实现文件的重命名,如果大佬们有更好的方法建议给我留言

另外我也不知道频繁写入注册表会不会出什么问题>--<

#################RenameWithDate.py##############
#自动安装缺少的第三方库开始
requirements = ["win32api", "win32con"]
def check_requirement(package):
    try:
        exec("import {0}".format(package))
    except ModuleNotFoundError:
        inquiry = input("This script requires {0}. Do you want to install {0}? [y/n]".format(package))
        while (inquiry != "y") and (inquiry != "n"):
            inquiry = input("This script requires {0}. Do you want to install {0}? [y/n]".format(package))
        if inquiry == "y":
            import os
            print("Execute commands: pip install {0}".format(package))
            os.system("pip install {0}".format(package))
        else:
            print("{0} is missing, so the program exits!".format(package))
            exit(-1)
for requirement in requirements:
    check_requirement(requirement)
#自动安装缺少的第三方库结束
   
import win32api
import win32con
import time
import threading
import atexit

rename_file_regpath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\NamingTemplates"
rename_file_regpath_0 = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer"

#定时器,循环修改CopyNameTemplate注册表,来实现文件备份时改名的目的
def fun_timer():
    mytime = time.strftime("%Y年%m月%d日%H时%M分%S秒", time.localtime()) 
    print(mytime)
    win32api.RegSetValueEx(key,"CopyNameTemplate",0,win32con.REG_SZ,"%s"+mytime)
    global timer
    timer = threading.Timer(1, fun_timer)
    timer.start()

#退出函数,在程序退出时,恢复为原来的注册表
@atexit.register  
def exit_fun():  
    key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,rename_file_regpath_0,0,win32con.KEY_WRITE)
    win32api.RegDeleteKey(key,"NamingTemplates")
    win32api.RegCloseKey(key)
    print ('exit RenameWithDate')

#容错判断
try:
    key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,rename_file_regpath,0,win32con.KEY_WRITE)
except:
    key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,rename_file_regpath_0,0,win32con.KEY_WRITE)
    win32api.RegCreateKey(key,"NamingTemplates")

finally:
    key = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER,rename_file_regpath,0,win32con.KEY_WRITE)
    fun_timer()

 

其他

更换pip源

在当前用户文件夹下创建pip文件夹,在pip文件夹下创建pip.ini,写入以下内容

[global]

index-url = https://mirrors.aliyun.com/pypi/simple/

 

将python打包为可执行文件并且不显示命令提示符界面

pip install pyinstaller

在要打包的py文件夹下面通过cmd界面输入以下内容

pyinstaller -F RenameWithDate.py --noconsole

在dist文件夹下面只有一个可执行文件,这个单文件就可以发布了,可以运行在你正在使用的操作系统类似的系统的下面。

需要注意的是,PyInstaller打包的执行文件,只能在和打包机器系统同样的环境下。也就是说,不具备可移植性,若需要在不同系统上运行,就必须针对该平台进行打包。

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值