Sign in to follow this  
Followers 0
jestermgee

Generate NFO files based on file name

5 posts in this topic

I only create scripts when I have a need to do so and I have not needed to create a script for a while now but am looking into one that I can offer the XBMC community.

The script that I want to look at creating will generate a NFO file (xml text file) with the filename of a video file. In that file would be the basics of the file name.

For example:

This show - 01x01 - Pilot Episode.avi

What I want to do is create the file:

This show - 01x01 - Pilot Episode.nfo

Then in this file I want to extract the bits of the file name to fill the relevent fields:

<episodedetails>
       <title>Pilot Episode</title>
       <season>01</season>
       <episode>01</episode>
</episodedetails>

I am completely stuck.

As a start I know I can create a loop to use FindFileFirst and FindFileNext to read the file names byt I can't figure out how to get this file name and create another file based off it and fill in the details.

My initial thought was to create a temp directory and create a blank text file, then write the lines within the test file then move the file into the video directory and change the extension while moving but it's a bit out of my depth of expertese.

Any help to get me started would be great. There are other things I need to get in this but I am sure I can figure those out.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

maybe this helps you:

Script is based on the fact, that every filename has the same structure like

This show - 01x01 - Pilot Episode.avi

#include <file.au3>
Global $aravifiles, $file, $title, $season, $episode
$avidir = "c:\avi"
$nfodir = "c:\nfo"
If not FileExists ($nfodir) Then DirCreate ($nfodir)
;get avi files from avidir
$aravifiles = _FileListToArray ($avidir, "*.avi", 1)

;Loop over array with filenames
For $i = 1 To UBound ($aravifiles) - 1
    ;Cut 4 char from the right -> get rid of extension and set new extension
    $file = $nfodir & "\" & StringTrimRight ($aravifiles [$i], 4) & ".nfo"
    ;Split Avi Filename by -
    $temp = StringSplit ($aravifiles [$i], "-", 2)
    ;last element is title -> get rid of extension and rid of leading space
    $title = StringTrimLeft (StringTrimRight ($temp [2], 4),1)
    ;title and episode is split of 2 nd element of 1st stringsplit, splitting by x
    $temp1 = StringSplit ($temp [1], "x", 2)
    ;season is last elemnt of split, get rid of spaces
    $season = StringStripWS ($temp1 [1], 8)
    ;episode is 1st elemnt of split, get rid of spaces
    $episode = StringStripWS ($temp1 [0], 8)
    ;write nfo file
    _writenfo ()
Next

Func _writenfo ()
    FileWriteLine ($file, "<episodedetails>")
    ;chr (9) is a TAB
    FileWriteLine ($file, Chr (9) & "<title>" & $title & "</title>")
    FileWriteLine ($file, Chr (9) & "<season>" & $season & "</season>")
    FileWriteLine ($file, Chr (9) & "<episode>" & $episode & "</episode>")
    FileWriteLine ($file, "</episodedetails>")
EndFunc

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

Thanks for the reply

There is a lot of that script I don't fully understand so troubleshooting will be a bit hard for me.

I tried running the script on a test file and received the following error:

C:\Documents and Settings\jason.NESS\Desktop\NFO Generator.au3 (16) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$title = StringTrimLeft (StringTrimRight ($temp [2], 4),1)
$title = StringTrimLeft (StringTrimRight (^ ERROR

Not quite sure what to do there.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi,

this looks like you have an avi file, which doesn't meets the name structure you given:

Name1 - NumberxNumber - Name2.avi

For debugging:

1) Add #include <array.au3> at the beginning of your script

2) Add _ArrayDisplay ($aravifiles, "Files") after line $aravifiles = _FileListToArray ($avidir, "*.avi", 1) so you can see the filenames

3) Add ArrayDisplay ($temp, "Split") after line $temp = StringSplit ($aravifiles [$i], "-", 2) so you can see the split. The split should split your filename into 3 elements: Name1, NumberxNumber, Name2.avi. This should be then $temp [0], $temp [1], $temp [2]

;-))

Stefan

P.S: You should post some filenames of your avi files as well...

Edited by 99ojo

Share this post


Link to post
Share on other sites

Stefan Thankyou so much for your help :mellow:

I have completed the script and through studying what you did have learn't a few new skills. The script I have created won't be a solution for everyone but it sure as hell works a treat for me and saves SOOOOOOOOO much time so thanks for your help.

There were just a couple of issues with some of the syntaxes you had which was confusing me but once I broke down the program and studied the help file I was on my way to understanding how the code was trying to work. I then added the extra info I wanted in my NFO file and lastly allowed the program to detect other video files than just avi (which required the ability for 3 and 4 character extensions to be processed).

The code isn't perfect but it works well so long as all files are in the correct format. I am sure if others need this they can modify it for their needs.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.10.0
 Author:   Jester Mgee

 Script Function:
    This script is designed to create a simple .NFO file for video files. The script is hard coded to work on files with the following format names:

    Show Name - SSxEE - Episode Title.ext

    The Episode Title, Season Number (SS) and Episode Number (EE) will all be extracted and entered into an NFO file that will be created in the folder
    with the video files.

    The script is run from within the folder that NFO files are to be generated and will process the formats of files listed in the script.
    IF an NFO file already resides in the directory it will be replaced automatically.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <file.au3>
#include <array.au3>
Global $aravifiles, $file, $title, $season, $episode
$avidir = @ScriptDir
$nfodir = @ScriptDir

; Process 3 character extension files
;get avi files
$aravifiles = _FileListToArray ($avidir, "*.avi", 1)
_processfilename3()
;get mpg files
$aravifiles = _FileListToArray ($avidir, "*.mpg", 1)
_processfilename3()
;get wmv files
$aravifiles = _FileListToArray ($avidir, "*.mpg", 1)
_processfilename3()
;get mkv files
$aravifiles = _FileListToArray ($avidir, "*.mkv", 1)
_processfilename3()

; Process 4 character extension files
$aravifiles = _FileListToArray ($avidir, "*.divx", 1)
_processfilename4()
$aravifiles = _FileListToArray ($avidir, "*.mpeg", 1)
_processfilename4()
$aravifiles = _FileListToArray ($avidir, "*.mt2s", 1)
_processfilename4()

; Function for 3 character filenames (avi; mpg; wmv; mkv)
Func _processfilename3()
;Loop over array with filenames
For $i = 1 To UBound ($aravifiles) - 1
    ;Cut 4 char from the right -> get rid of extension and set new extension
    $file = $nfodir & "\" & StringTrimRight ($aravifiles [$i], 4) & ".nfo"
    ;Split Avi Filename by -
    $temp = StringSplit ($aravifiles [$i], " - ", 1)
    ;_ArrayDisplay ($temp, "StringSplit - File Name")
    ;last element is title -> get rid of extension and rid of leading space
    $title = StringTrimRight ($temp [3], 4)
    ;title and episode is split of 2 nd element of 1st stringsplit, splitting by x
    $temp1 = StringSplit ($temp [2], "x", 1)
    ;_ArrayDisplay ($temp1, "StringSplit - Season / Episode")
    ;season is last elemnt of split, get rid of spaces
    $season = StringStripWS ($temp1 [1], 8)
    ;episode is 1st elemnt of split, get rid of spaces
    $episode = StringStripWS ($temp1 [2], 8)
    ;write nfo file
    _writenfo ()
Next
EndFunc

; Function for 3 character filenames (divx; mpeg; mt2s)
Func _processfilename4()
;Loop over array with filenames
For $i = 1 To UBound ($aravifiles) - 1
    ;Cut 5 char from the right -> get rid of extension and set new extension
    $file = $nfodir & "\" & StringTrimRight ($aravifiles [$i], 5) & ".nfo"
    ;Split Avi Filename by -
    $temp = StringSplit ($aravifiles [$i], " - ", 1)
    ;_ArrayDisplay ($temp, "StringSplit - File Name")
    ;last element is title -> get rid of extension and rid of leading space
    $title = StringTrimRight ($temp [3], 5)
    ;title and episode is split of 2 nd element of 1st stringsplit, splitting by x
    $temp1 = StringSplit ($temp [2], "x", 1)
    ;_ArrayDisplay ($temp1, "StringSplit - Season / Episode")
    ;season is last elemnt of split, get rid of spaces
    $season = StringStripWS ($temp1 [1], 8)
    ;episode is 1st elemnt of split, get rid of spaces
    $episode = StringStripWS ($temp1 [2], 8)
    ;write nfo file
    _writenfo ()
Next
EndFunc

Func _writenfo ()
    If FileExists ($file) Then FileDelete ($file)
    FileWriteLine ($file, "<episodedetails>")
    ;chr (9) is a TAB
    FileWriteLine ($file, Chr (9) & "<title>" & $title & "</title>")
    FileWriteLine ($file, Chr (9) & "<rating>5.0</rating>")
    FileWriteLine ($file, Chr (9) & "<season>" & $season & "</season>")
    FileWriteLine ($file, Chr (9) & "<episode>" & $episode & "</episode>")
    FileWriteLine ($file, Chr (9) & "<plot>Auto-Generated NFO File.</rating>")
    FileWriteLine ($file, Chr (9) & "<credits>Jester Mgee Systems</credits>")
    FileWriteLine ($file, Chr (9) & "<director></director>")
    FileWriteLine ($file, Chr (9) & "<aired></aired>")
    FileWriteLine ($file, Chr (9) & "<actor>")
    FileWriteLine ($file, Chr (9) & Chr (9) &"<name></name>")
    FileWriteLine ($file, Chr (9) & Chr (9) &"<role></role>")
    FileWriteLine ($file, Chr (9) & Chr (9) &"<thumb></thumb>")
    FileWriteLine ($file, Chr (9) & "</actor>")
    FileWriteLine ($file, "</episodedetails>")
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0