0

well, I got a set of folders each containing a *.nfo file from XBMC (beside other files such as videos and pictures etc).

I want to rename the folder by strings inside the *.nfo file. The relevant content of such a *.nfo file:

...
    <title>Filmtitle</title>
...
    <year>2011</year>
...
    <director>Werner Herzog</director>
...

UPDATE: here is an unmodified, original .nfo file from XBMC

movie.nfo

I tried a lot with find exec and grep, but I did not really get anythin usable...

In the example above the folder should have the name "Filmtitel [2011, Werner Herzog]"

Maybe someone can help me out!

1 Answers1

1

Try the following script. It extracts the title, year and director from each file and then renames the directory:

find . -type f -name "*.nfo" -print0 | while IFS= read -r -d $'\0' file
do
    title="$(sed 's|.*<title>\(.*\)</title>.*|\1|g' $file)"
    year="$(sed 's|.*<year>\(.*\)</year>.*|\1|g' $file)"
    director="$(sed 's|.*<director>\(.*\)</director>.*|\1|g' $file)"

    dirName="${file%/*}"
    newDirName="${dirName%/*}/$title [$year, $director]"
    # mv "$dirName" "$newDirName"
    echo mv "$dirName" "$newDirName"
done

(Simply uncomment the mv command if you are happy that the commands being printed out are correct.)

dogbane
  • 266,786
  • 75
  • 396
  • 414
  • I tried, but sed writes too much to the variable, also all lines before and after the line with the search term... I tried to understand the sed command, but i failed.... – user3114634 Dec 18 '13 at 14:11
  • Anyone knows how to prevent that `title="$(sed 's|.*\(.*\).*|\1|g' $file)"` don't add everything above and below the line containing the title? – user3114634 Dec 19 '13 at 11:39
  • It won't add everything above and below the line. It should only add what is between the title tags. Is your file formatted exactly as shown in the question? – dogbane Dec 19 '13 at 11:45
  • I shortend the file in my question. Maybe something is wrong with BR and LF? I just added a link to an original file, – user3114634 Dec 19 '13 at 21:00