python/powershell_批量重命名文件脚本/为文件批量添加前缀序号以便于用终端打开/指定/操作文件中文文件_重命名win10/11的锁屏图

note1

以下程序尚未考虑健壮性
只是以添加后缀为主要目的,按照自然数序来作为新名称

关于以下使用的路径,根据自身情况替换

note2

  • 不可以在新文件名中包含非法字符
  • 基于python的重命名实现:描述文件时,要么使用绝对路径,要么使用os.chdir()切换路径后直接使用名字,否则无法重命名
  • 基于powershell的重命名实现:代码可以更加简介,这是系统shell的优势,但是该语言没有像python那样普及

基于python实现:

以<时间+n>为文件名

import os
import os.path as op
import time
dirName="C:/Users/cxxu_11/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets"
def renameFiles(dirName='.'):
    items=os.listdir(dirName)
    index=0
    # there use os.chdir() to reset the current working directory;of course,you can use absolute path rather than fileName
    os.chdir(dirName)
    print(os.getcwd())
    for item in items:
        index+=1
        # newName=str(index)+".jpg"
        time_string = time.strftime(
        '%Y-%m-%d %H-%M-%S', time.localtime(time.time()))
        time_string=time_string+"  "+str(index)
        newName= time_string+'.jpg'
        if not op.exists(newName):
            os.rename(item,newName)

        print(item)
        
        
if __name__=="__main__":
    
    print("test")
    renameFiles(dirName)

以1,2,3为文件名

import os
import os.path as op
dirName="C:/Users/cxxu_11/AppData/Local/Packages/Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy/LocalState/Assets"
def renameFiles(dirName='.'):
    items=os.listdir(dirName)
    index=1
    # there use os.chdir() to reset the current working directory;of course,you can use absolute path rather than fileName
    os.chdir(dirName)
    for item in items:
        index+=1
        newName=str(index)+".jpg"
        if not op.exists(newName):
            os.rename(item,newName)

        print(item)
        
        
if __name__=="__main__":
    renameFiles(dirName)

基于powershell实现

执行之前:


执行代码以及之后的结果:

$i=1
Get-ChildItem * | ForEach-Object {
    
    

    rename $_.Name ($i.toString() + $_.Name); 
    $i += 1 ;
}

执行之前把rename改为write查看预览效果

$i=1
Get-ChildItem * | ForEach-Object {
    
    

    write ($i.toString() + $_.Name); 
    $i += 1 ;
}

为文件批量添加前缀序号以便于用终端指定/操作文件中文文件

import os
import os.path as op
import time
""" 使用说明,重命名文件是高危操作,为了安全起见,您可以将要被处理的文件做一个备份
其次,您可以复制文件中的一部分来测试您的代码的可行性
最后,如果代码中通过保守的预览代码代替实际的重命名操作也是不错的选择(待预览效果符合预期后执行实际的重命名操作是推荐的做法)

本程序只对文件执行重命名(纳入排序计算),文件夹被可以排除,当然,也可以认为修改使得文件夹也一并纳入计算;当然,您可以将os.isfile()改为os.isdir()来对文件夹进行重命名而干扰文件
"""
if __name__ == "__main__":
    # 测试目录填入一下括号中(字符串)
    os.chdir("D:\\org\\Ecloud\\myMusic\\hitomiShimatani")
    itemList = os.listdir(".")
    is_beginAscii = False
    # 推荐的分割符,如果不喜欢,可以替换(取消的换就将其置为空字符串"")
    separator = "_"
    cleanList = []
    # 给部分去除源文件名的前缀中的ascii码部分(可以改为数字isdigital(这在排错序号重新排序前的清洗操作是横有用的))
    for item in itemList:
        is_continue = item[0].isdigit() or item[0] == separator
        if is_continue:
            new_name = ""
            # 文件名预处理与清理
            for index, chr in enumerate(item):
                if chr.isdigit() or chr == separator:
                    continue
                else:
                    new_name = item[index:]
                    # print(new_name)
                    break
            # 文件判断与重命名
            if op.isfile(item):
                print(new_name)
                # 由于这里还是字符串处理阶段,可以只是预览,不必执行重命名操作
                # os.rename(item, new_name)
                cleanList.append(new_name)

        else:
            ...
    # itemList = cleanList
    # sort with the same format 01~99
    for index, item in enumerate(itemList):
        # 这里使用了字符串的.zfill来前置补零格式化字符串(对数字格式化的话可以用str()将其转为字符类型,然后执行该函数)
        # 通过序数前置补0对其的处理,可以是的排序的时候不会出现错乱,当然,这需要对您的文件数(或者说数量级)做一个估计(如果文件在100个以内,哪个zfill()参数取2较为合适.一次类推)
        # 我还建议在插入序号前缀中的最后一个字符以"_"结尾或者其他合法的字符来分隔
        new_prefix = str(index).zfill(2)
        new_prefix = new_prefix+separator
        new_name = new_prefix+item
        # for index, chr in enumerate(item):
        if op.isfile(item):
            # 在执行重命名操作前但应出来预览一下,符合预期后在编写重命名语句
            print(new_name)
            os.rename(item, new_name)

终端操作效果

终端点歌:

猜你喜欢

转载自blog.csdn.net/xuchaoxin1375/article/details/121587028