如何使用“变量”批量重命名文件?

我用xmbc查看我的电视节目。 在我开始使用xmbc之前,我批量下载了“Bleach”的前几季。 我能说什么,我是一个动漫迷;-)。 这些被命名为:“bleachxx.mp4”,其中xx是关于整个系列的剧集编号,而不是季节。 所以“bleach21.mp4”是第二季第一集和第21集。 但是,文件本身分为各自的季节文件夹。

我知道您可以使用“重命名”命令批量重命名文件。 在使用本指南进行了大量的修补之后: http : //tips.webdesign10.com/how-to-bulk-rename-files-in-linux-in-the-terminal我得到了这个命令:

rename -n 's/.*(\d{2}).*$/Bleach S0XE$1/' * 

问题是该命令会将所有文件重命名为“Bleach S0XExx”,但因为文件具有整体文件编号,bleach52.mp4 – >’Bleach S03E52.mp4’,而第3季显然没有52集。

我想知道,如果有任何方法将数学运算应用于文件重命名。 这将解决问题,因为我可以减去前几季的剧集数量,并且基本上得到季节数。

示例:如果第1季有20集,则25-20 = 5,因此第25集是第2季的第5集,重命名将正常进行。

那么,无论如何通过应用数学运算来改变重命名的值?

PS我很抱歉这个冗长的解释,但我不知道如果没有它我的问题是否清楚。

如果你足够专注于修改正则表达式(在你提供的示例中),那么我建议只需更进一步并编写一个小脚本 – 比如说,用Python编写。 这样,您就可以对文件名执行绝对的任何转换。

我估计python脚本不会超过15-20行,所以这绝对不是一项艰巨的任务。 正如您所尝试的那样,使用正则表达式会受到更多限制。

这是我对这样的脚本的看法:

 #!/usr/bin/python import os,re files = os.listdir('.') SEASONS = ( (1, 1, 3), # the format is - season number, first episode, last episode (2, 50,52), (3, 53,55), (4, 56,99), ) for f in files: # skip all files which are not .mp4 if not f.endswith(".mp4"): continue # find the first number in the filename matches = re.findall("\d+", f) if not len(matches): print "skipping", f num = int(matches[0]) for season in SEASONS: if num <= season[2]: season_num = season[0] ep_num = num - season[1] + 1 new_file_name = "BleachS%02dE%02d.mp4" % (season_num, ep_num) # This is for testing print "%s ==> %s" % (f, new_file_name) # Uncomment the following when you're satisfied with the test runs # os.rename(f, new_file_name) break print "Done" 

看起来我低估了脚本大小(它是36行atm),虽然我确定你是否使用这段代码进入stackoverflow,你会得到很多更优雅的建议

仅仅因为我说它可以在15行中完成……以下是20行,其中5行是配置:P

 #!/usr/bin/python import os, re, glob SEASONS = ( {'num':1, 'first':1, 'last':3}, # the format is - season number, first episode, last episode {'num':2, 'first':50, 'last':52}, {'num':3, 'first':53, 'last':55}, {'num':4, 'first':56, 'last':99}, ) files = glob.glob('bleach*.mp4') for f in files: num = int(re.findall("\d+", f)[0]) # find the first number in the filename for season in SEASONS: if num > season['last']: continue new_file_name = "BleachS%02dE%02d.mp4" % (season['num'], num - season['first'] + 1) print "%s ==> %s" % (f, new_file_name) # This is for testing # os.rename(f, new_file_name) # Uncomment this when you're satisfied with the test runs break 

我给你写了一个python脚本

 import sys import os import glob import re def rename(): episode_counts = (0, 20, 41, 63) episode_pairs = [] for index, num in enumerate(episode_counts): if index < len(episode_counts) - 1: episode_pairs.append((num, episode_counts[index+1])) episodes = glob.glob(os.path.join(sys.argv[1], '*')) for episode in episodes: match = re.search('.*(\d{2}).*$', os.path.basename(episode)) episode_num = match.group(1) for season, pair in enumerate(episode_pairs): if int(episode_num) in range(pair[0]+1, pair[1]+1): dirname = os.path.dirname(episode) path = os.path.join(dirname, 'Bleach S{0}E{1}'.format(season+1, episode_num)) os.rename(episode, path) if __name__ == "__main__": rename() 

我是python的新手(这也是为什么我写这个,为了练习),所以它可能不是世界上最好的脚本。 但我尝试了几个测试文件,它似乎工作。

只需将顶部episode_counts = ...附近的行编辑为一个季节的最后一集编号。 我从这个页面输入了前三个。

将代码保存为episode_renamer.py并将其与python episode_renamer.py /path/to/episodes/

存储库中有一个名为Thunar的文件管理器,它具有批量重命名选项。

有关更多信息,请访问此站点: http : //thunar.xfce.org/pwiki/documentation/bulk_renamer

您可以在软件中心或命令行中安装thunar:

 sudo apt-get install thunar 

还有其他方法可以在bash中执行此操作,或者通过编写一些代码,但是图形工具可能会更快地为您提供所需的内容。

我正在开发一个跨平台的桌面应用程序,它允许您将一组文件从指定的模式重命名为另一个。 用户可以指定两种模式。

您可以从此链接下载它(称为: file-renamer-swt )。 这是项目主站​​点 。

继续鞭打这匹马…如果这是一次性重命名,我会做一些像一次性的东西:

 cd /path/to/stuff ls bleach*-lq.mp4 >x paste xx >y sed 's-^-mv -' z vim z # or use your favorite editor # to rename the files manually bash -xz rm xyz 

如果您将不止一次使用它,并且不止一种情节…那么python就是要走的路,上面的例子就好了。