递归创建.nfo文件

时间:2014-06-11 19:31:57

标签: find directory

我有一系列音乐会媒体(音频和视频)。他们都使用相同的模式进行组织:

./ARTISTNAME/[YYYY-MM-DD] VENUE, CITY

基本上我想编写一个遍历文件夹的脚本,查找[YYYY-MM-DD]文件夹并获取有关艺术家(上面一个文件夹级别),日期,地点和位置的信息(使用名称)它刚刚找到的文件夹)并将信息写入.nfo文件,并保存到找到的文件夹中。

我已经对这个主题做了很多研究,发现了一个类似的脚本,但我被卡住了,因为它搜索文件而不是文件夹:

#!/bin/bash

find . -path "*[????-??-??]*" | while read folder; do # the script is supposed to look for the concert-folders
      ->  band_name=`echo $file_name | sed 's/ *-.*$//'` # Get rid of song name
      ->  band_name=`echo $band_name | sed 's/^.*\///'`  # Get rid of file path
      ->  song_name=`echo $file_name | sed 's/^.*- *//'` # Get rid of band name
      ->  song_name=`echo $song_name | sed 's/.avi//'` # Get rid of file extension
      ->  new_file_name=`echo $file_name | sed 's/.avi/.nfo/'` # Make new filename
      ->  echo "Making $new_file_name..."
        echo -en "<musicvideo>\n<venue>$venue_name</venue>\n<city>$city_name</city><date>$date>\date>\n<artist>$band_name</artist>\n</musicvideo>\n" > "$new_file_name"
done

更改脚本的第一部分后(使用“[YYYY-MM-DD]”查找文件夹)我明白脚本的第二部分会分配“标签”(例如艺术家,日期,位置等)。但我不知道如何让脚本从文件夹名称中获取标签。基本上在“ - &gt;”之后需要帮助。

在脚本的最后一部分,它应该将此文件夹的收集信息写入.nfo文件(例如FOLDERNAME.nfo)。

1 个答案:

答案 0 :(得分:0)

这是一个简单的Python示例,可帮助您入门。如果你想将它移植到shell,你可以。

首先,我的设置:

test
test/BOB
test/BOB/[3011-01-01] Lollapalooza 213, Saturn Base 5
test/THE WHO
test/THE WHO/[1969-08-17] Woodstock, Woodstock

代码:

#!/usr/bin/env python

import os
import os.path
import re
import sys


def handle_concert(dirname, artist, date, venue, city):
    """Create a NFO file in the directory."""

    # the {} are replaced by each argument in turn, like printf()
    # Using a triple quote is a lot like a here document in shell, i.e.
    # cat <EOF
    # foo
    # EOF
    nfo_data = """<musicvideo>
<venue>{}</venue>
<city>{}</city>
<date>{}</date>
<artist>{}</artist>
</musicvideo>
""".format(venue, city, date, artist)

    nfo_file = "[{}] {}, {}.nfo".format(date, venue, city)

    # when the with statement is done the file is closed and fully
    # written.
    with open(os.path.join(dirname, nfo_file), "w") as fp:
        fp.write(nfo_data)


# This is a regular expression which matches:
# */FOO/[YYYY-MM-DD] VENUE, CITY
# Where possible, white space is left intact
concert_re = re.compile(r'.*/(?P<artist>.+)/\[(?P<date>\d{4}-\d{2}-\d{2})\]\s+(?P<venue>.+),\s+(?P<city>.+)')

def handle_artist(dirname):
    """Found an ARTIST directory. Look for concerts.
    If a subdirectory is found, see if it matches a concert.
    When a concert is found, handle it.
    """
    for i in os.listdir(dirname):
        subdir = os.path.join(dirname, i)
        m = concert_re.match(subdir)
        if m:
            print subdir  # to watch the progress
            handle_concert(subdir, m.group("artist"), m.group("date"),
                           m.group("venue"), m.group("city"))


def walk_collection(start_dir):
    """Examine contents of start_dir.
    If a directory is found, assume it is an ARTIST.
    """
    for i in os.listdir(start_dir):
        # os.path.join ensures paths are right regardless of OS
        dirname = os.path.join(start_dir, i)
        if os.path.isdir(dirname):
            print dirname  # to watch the progress
            handle_artist(dirname)

if __name__ == "__main__":
    collection_dir = sys.argv[1]  # equiv of $1

    walk_collection(collection_dir)

如何运行它:

$ nfo-creator.py /path/to/your/collection

结果:

<musicvideo>
<venue>Woodstock</venue>
<city>Woodstock</city>
<date>1969-08-17</date>
<artist>THE WHO</artist>
</musicvideo>

<musicvideo>
<venue>Lollapalooza 213</venue>
<city>Saturn Base 5</city>
<date>3011-01-01</date>
<artist>BOB</artist>
</musicvideo>

您可以添加更多handle_个函数来处理不同的格式。只需为它定义一个新的正则表达式和处理程序。我通过大量面向shell的评论来保持这个非常简单的学习目的。

这完全可以在shell中完成。但是编写这个Python代码更容易,并且可以在未来实现更多增长。

享受,随时提问。