V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
mortal
V2EX  ›  问与答

程序猿们,求程序或脚本实现批量文件重命名功能

  •  
  •   mortal · 2014-11-19 10:25:06 +08:00 · 2133 次点击
    这是一个创建于 3452 天前的主题,其中的信息可能已经有所发展或是发生改变。
    大概要实现的是:

    1、将一个目录下的所有内容(包括子文件夹的)导出为 *.txt,比如里面有「a.doc b.xls ……」;
    2、手动修改 *.txt 内容,保存为新的 txt 文本「1.doc 2.xls ……」;
    3、程序将两个文本比对,将所有文件更改为新的文件名。

    系统环境 Windows,我能想到的就是用 tree 命令,然后用 Python 解析格式、循环完成更名操作。
    我知道如果在类 Unix 系统下 shell 脚本估计就可以很容易搞定了,但是没有这个条件……

    有没有现成的类似的工具,或者有哪位大神帮撸一发代码呢?
    13 条回复    2014-11-20 09:17:45 +08:00
    rrfeng
        1
    rrfeng  
       2014-11-19 11:00:01 +08:00   ❤️ 1
    cygwin

    虽然还是没懂你要干什么。

    先把文件名列出来,然后肉眼判断每个改成什么名字,然后再用程序改过去?
    mimzy
        2
    mimzy  
       2014-11-19 11:06:45 +08:00 via Android   ❤️ 1
    不知道 ReNamer 是不是符合你的需求
    xavierskip
        3
    xavierskip  
       2014-11-19 11:11:02 +08:00
    Total Commander
    aa65535
        4
    aa65535  
       2014-11-19 11:16:12 +08:00   ❤️ 1
    为什么不用 for /r ?

    (for /r .\ %%i in (*) do echo ren %%i %%~nxi)>ren.bat

    然后你修改 ren.bat 就行了,里面是生成好的 ren 命令。
    如果改路径需要改成 move ,不用 ren 。
    mortal
        5
    mortal  
    OP
       2014-11-19 11:27:28 +08:00
    @rrfeng 是这个意思
    mortal
        6
    mortal  
    OP
       2014-11-19 11:28:54 +08:00
    @xavierskip TC 试过了,只能批量修改一个目录下的文件,不能同时包括子文件夹……
    mortal
        7
    mortal  
    OP
       2014-11-19 11:36:01 +08:00
    @mimzy 感谢,这个软件差不多可以满足要求,就是对中文支持不好
    yakczh
        8
    yakczh  
       2014-11-19 11:44:17 +08:00
    os.walk 你值得拥有
    frankzeng
        9
    frankzeng  
       2014-11-19 12:46:39 +08:00   ❤️ 1
    第一个先成生,再运行另一个
    ##q.py
    import os
    path = "C:\Users\john\Desktop\data\sbaidu" #dir
    dlist = os.walk(path)

    outdict = {}
    for d in dlist:
    sdir = d[0]
    for i in d[1:]:
    for file in i:
    p = sdir+"\\"+file
    outdict[p] = 1

    fd = open("qout.txt",'w') #write to file qout.txt
    for key in outdict.keys():
    fd.write(key+'\n')
    fd.close()

    这是另一个文件

    import os

    qfile = "qout.txt" #old file
    mfile = "mout.txt" #the new file

    data = {}
    fd = open(qfile)
    n = 1
    for line in fd:
    data[n] = line[:-1]
    n += 1
    fd.close()

    fd = open(mfile)
    n = 1
    for line in fd:
    file = line[:-1]
    if file!=data[n]:
    print data[n] ,"move to ",file
    os.rename(data[n],file)
    else:
    pass
    n += 1
    asdftu
        10
    asdftu  
       2014-11-19 14:07:27 +08:00
    批处理实现起来会很简单。

    for /r 遍历所有文件,
    echo 文件名
    set /p 键入新文件名
    ren

    搞定
    msg7086
        11
    msg7086  
       2014-11-19 14:12:06 +08:00
    拖把更名器,拖入所有文件,调用文本文件命名,应用,结束。
    mortal
        12
    mortal  
    OP
       2014-11-19 20:44:12 +08:00
    @yakczh 我也是这么想的~
    @frankzeng 非常感谢!我写出来可能要几个小时=。= 大神一下搞定了…
    frankzeng
        13
    frankzeng  
       2014-11-20 09:17:45 +08:00
    @mortal 不客气,需要的话可以加我企鹅106367224
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2262 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:24 · PVG 16:24 · LAX 01:24 · JFK 04:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.