如何用「變數」批次重命名檔案?

如何用「變數」批次重命名檔案?

我使用 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”,而第三季顯然沒有 52 集。

那麼,我想知道是否有任何方法可以將數學運算應用於文件重命名。這可以解決問題,因為我可以用總數減去前幾季的劇集數,基本上就得到了季數。

範例:如果第 1 季有 20 集,則 25-20=5,因此第 25 集是第 2 季的第 5 集,重命名將照常進行。

那麼,是否有辦法透過應用數學運算來更改重命名的值?

PS 我很抱歉這麼長的解釋,但我不確定如何解釋,或者如果沒有它我的問題是否會清楚。

答案1

如果您足夠專注於修改正規表示式(在您提供的範例中),那麼我建議您更進一步,編寫一個小腳本 - 例如,用 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"

看起來我低估了腳本的大小(atm 是 36 行),不過我確信如果你用這段程式碼去 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

答案2

我為你寫了一個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/.

答案3

儲存庫中有一個名為 Thunar 的檔案管理器,它具有批次重命名選項。

請參閱此網站以了解更多資訊: http://thunar.xfce.org/pwiki/documentation/bulk_renamer

您可以在軟體中心或命令列上安裝 thunar:

sudo apt-get install thunar

在 bash 中還有其他方法可以做到這一點,或者透過編寫一些程式碼,但圖形工具可能會更快地滿足您的需求。

答案4

為了繼續鞭打這匹馬......如果這是一次性重命名,我會做一些一次性的事情,例如:

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

如果您將多次使用它,並且不止一種情節……那麼 python 就是您的最佳選擇,上面的示例就很好了。

相關內容