首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重命名文件名,使文件以指定的排序顺序出现。

重命名文件名,使文件以指定的排序顺序出现。
EN

Stack Overflow用户
提问于 2021-08-18 19:18:29
回答 3查看 36关注 0票数 0

我有一份文件清单。它们目前按以下顺序出现:

代码语言:javascript
复制
Advance Topics in Graphs _ Lec 11 _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Basics of Combinatorics _ Lec 12 _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Basics of Graph Theory _ Lec 6 _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm
Basics of Relations - 2 _ Lec 20 _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
Basics of Relations _ Lec 19 _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Basics of Sets _ Lec 18 _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm

我想重命名这些文件,以便文件名具有以下模式(使用regex表示清楚),Lec\s0-90-9出现在文件名的中间,出现在文件名的开头。(这是排序顺序)。

最终结果如下:

代码语言:javascript
复制
Lec 6 Basics of Graph Theory _  _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm
Lec 11 Advance Topics in Graphs _  _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Lec 12 Basics of Combinatorics _  _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Lec 18 Basics of Sets _  _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm
Lec 19 Basics of Relations _  _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Lec 20 Basics of Relations - 2 _  _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-08-18 19:45:04

当我遇到这样的问题时,我倾向于用Python编写一个一次性的脚本来完成这项工作,尽管您可以用所选择的任何语言来代替它(即。( bash+awk,.)

这主要是为了确保您在真正重命名之前知道结果是什么,这可能是一个破坏性操作(抛出标识符,将所有输入文件重命名为同名,以便只有最后一个保存下来)!

代码语言:javascript
复制
import os
import re

def mapper(s):
    # adjust to taste, this just swaps the middle to the front
    prefix, index, postfix = re.match(r"^(.*) (?:Lec) (\d+) (.*)$", s).groups()
    return f"Lec {int(index):02} {prefix} {postfix}"  # pad index 2->02, etc.

mapping = {}  # start a new dictionary to store src->dest maps
for name_file in os.listdir():
    try:
        mapping[name_file] = mapper(name_file)
    except AttributeError:  # failed to match
        print(f"failed to match for {name_file}")

# display the change on separate lines
for src, dest in mapping.items():
    print(f"{src} -> {dest}")

# [return] or ^C to quit
input("continue?..")

# perform renames
for src, dest in mapping.items():
    os.rename(src, dest)
票数 1
EN

Stack Overflow用户

发布于 2021-08-18 19:36:44

注意:这需要gawk,因为gensub

代码语言:javascript
复制
ls | awk '{a=gensub(/^(.*) (Lec [0-9]+) (.*)$/, "\\2 \\1\\3","1",$0);printf "mv \"%s\" \"%s\"\n",$0, a}' |  bash
票数 1
EN

Stack Overflow用户

发布于 2021-08-18 20:57:29

给予:

代码语言:javascript
复制
ls -1 *.webm
Advance Topics in Graphs _ Lec 11 _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Basics of Combinatorics _ Lec 12 _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Basics of Graph Theory _ Lec 6 _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm
Basics of Relations - 2 _ Lec 20 _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
Basics of Relations _ Lec 19 _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Basics of Sets _ Lec 18 _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm 

我只需要在Ruby中做一个小小的直线:

代码语言:javascript
复制
ruby -e 'Dir.glob("*.webm"){
        |fn| File.rename(fn, fn.sub(/^([^_]*)_ ([^_]+? )_/,"\\2\\1_ _"))}'

然后:

代码语言:javascript
复制
ls -1 *.webm
Lec 11 Advance Topics in Graphs _ _ Discrete Mathematics _ Exam-lAN6B0Hvy7E.webm
Lec 12 Basics of Combinatorics _ _ Discrete Mathematics _ Exam-sBg13JNUQQ8.webm
Lec 18 Basics of Sets _ _ Discrete Mathematics _ Exam-LE-Gt_PUdws.webm
Lec 19 Basics of Relations _ _ Discrete Mathematics _ Exam-4KTaggHRd54.webm
Lec 20 Basics of Relations - 2 _ _ Discrete Mathematics _ Exam-CTo9pvl__5M.webm
Lec 6 Basics of Graph Theory _ _ Discrete Mathematics _ Exam-JqgdPH-PEIM.webm

但是,请注意,如果新名称与以前生成的名称相同,则此(以及这里的所有其他解决方案)将覆盖一个文件.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68838194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档