TV recordings renamed with metadata COMMENT tag

Discussion in 'MediaPortal 1 Talk' started by Mike Amory, August 4, 2016.

  1. Mike Amory

    Mike Amory Portal Member

    Joined:
    June 8, 2015
    Messages:
    17
    Likes Received:
    1
    Gender:
    Male
    Ratings:
    +2 / 0
    Home Country:
    Finland Finland
    MediaPortal 1 doesn't allow to use COMMENT tag in the filename format of TV recordings. In Finland series and episode info is inside the COMMENT tag. This FoxPro program renames TV recordings with the COMMENT tag. This program is freeware and you can modify it as you please.

    ----------------- SNIP ------------------
    * FILE: bulkrename.prg
    * This program adds metadata to file names of MediaPortal TV recordings.
    * 40 leftmost and 40 rightmost characters of COMMENT tag in .xml file are added and
    * the first occurrence of character @ in the file name marks the placing of the metadata.
    * This program is useful if series and episode info is inside the COMMENT tag.
    * MediaPortal doesn't allow to use COMMENT tag in the filename format of TV recordings
    * Windows 7 gives OLE error code 0x80070057 if files are on external drive bigger than 2TB,
    * so this program gives the user option to directly rename all files in selected directory
    * or write the rename commands into a batch file "bulkrename.cmd" that user can run later (recommended).
    * Visual FoxPro does not recognize a path name properly if disk or directory name contains an exclamation point (!).
    * Running batch file "bulkrename.cmd" may give an error "the system cannot find file specified",
    * changing the character code page in the beginning of the batch file solves this problem.
    * Default character set is ANSI (Windows-1252), that is character encoding of the Latin alphabet
    * Preserves the original case of paths and file names.
    * Works with Visual Foxpro 9.0.
    * Example:
    * Original filename: Blacklist @ SE_2016-05-19-1_25-TV5.ts
    * Renamed file: Blacklist @ (Series 1, Episode 19. The Pavlovich Brothers.) SE_2016-05-19-1_25-TV5.ts

    CLEAR ALL
    CLOSE ALL
    CREATE TABLE Recordings (Recname c(250), Meta m) && Create a temp table
    USE Recordings ALIAS Recordings && Open the table
    DIMENSION Temparray(1,5) && Declare array to store file names
    ofs=CREATEOBJECT('scripting.filesystemobject')
    o=CREATEOBJECT('scripting.filesystemobject')
    path = GETDIR()
    IF EMPTY(path)
    WAIT 'Directory not selected' WINDOW
    RETURN
    ENDIF
    path = ofs.GetAbsolutePathName(path)
    path = ADDBS(path)
    WAIT 'Press Y if you want to send commands to a batch file instead of executing them.' TO Retval WINDOW
    IF ALLTRIM(UPPER(Retval))='Y'
    Writebatch=.T.
    IF FILE(path + 'bulkrename.cmd') && Does file exist?
    fhandle = FOPEN(path + 'bulkrename.cmd',12) && If so, open read/write unbuffered
    ELSE
    fhandle = FCREATE(path + 'bulkrename.cmd') && If not create it
    ENDIF
    IF fhandle < 0 && Check for error opening file
    WAIT 'Cannot open or create output file' WINDOW
    RETURN
    ELSE
    FPUTS(fhandle, 'chcp 1252')
    ENDIF
    ELSE
    Writebatch=.F.
    ENDIF
    ln = ADIR(Temparray, path + '*.ts','A' ,1) && Use ADIR() function to get recording names into array
    * DIMENSION lcFileName2(1,1) AS String, lcNewFileName(1,1) AS String
    FOR i = 1 TO ln && Store file names into Recordings table
    lcFileName2=path + ofs.getfile(path + Temparray[i,1]).name
    IF .NOT. FILE(lcFileName2)
    WAIT 'Error locating a file' WINDOW
    RETURN
    ENDIF
    SELECT Recordings
    APPEND BLANK
    REPLACE Recname WITH Temparray(i,1)
    IF FILE(path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml') && Tries to find the xml file of the recording
    APPEND MEMO Meta FROM path + LEFT(TRIM(Recname), LEN(TRIM(Recname))-3) +'.xml' && Metadata is added into the memo field
    Comment = SUBSTR(Meta,AT('<name>COMMENT</name>', Meta )+35)
    Comment = LEFT(Comment,AT('</value>', Comment)-1)
    Comment = CHRTRAN(Comment, '*/:<>?|\' + CHR(34), '_________' ) && Replacing invalid characters with underscore
    Addstr1 = LEFT(Comment,40) && 40 characters from the beginning of COMMENT tag
    Addstr2 = RIGHT(Comment,40) && 40 characters from the end of COMMENT tag
    IF EMPTY(Comment)
    Addstr = '@ ( ) '
    ELSE
    IF LEN(Comment) < 81
    Addstr = '@ (' + Comment + ') '
    ELSE
    Addstr = '@ (' + Addstr1 + '...' + Addstr2 + ') '
    ENDIF
    ENDIF
    Addstr = STRCONV(Addstr, 11) && Converts UTF-8 characters to double-byte characters, works for Scandinavian character set
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
    IF Writebatch=.T.
    tmpstr = 'RENAME ' + CHR(34) + lcFileName2 + CHR(34) + ' ' + CHR(34) + JUSTFNAME(lcNewFileName) + CHR(34)
    FPUTS(fhandle, tmpstr)
    ELSE
    o.movefile(lcFileName2, lcNewFileName)
    ENDIF
    ENDIF
    lcFileName2 = LEFT(TRIM(lcFileName2), LEN(TRIM(lcFileName2))-3) +'.xml'
    lcNewFileName = STUFF(lcFileName2, AT('@',lcFileName2),1, Addstr)
    IF FILE(lcFileName2)
    IF Writebatch=.T.
    tmpstr = 'RENAME ' + CHR(34) + lcFileName2 + CHR(34) + ' ' + CHR(34) + JUSTFNAME(lcNewFileName) + CHR(34)
    FPUTS(fhandle, tmpstr)
    ELSE
    o.movefile(lcFileName2, lcNewFileName)
    ENDIF
    ENDIF
    ENDIF
    NEXT i
    IF Writebatch=.T.
    =FCLOSE(fhandle) && Close file
    MODIFY FILE (path + 'bulkrename.cmd') NOWAIT && Open file in edit window
    ENDIF
    RETURN
    ----------------- SNIP ------------------


     
Loading...

Users Viewing Thread (Users: 0, Guests: 0)

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice
  • About The Project

    The vision of the MediaPortal project is to create a free open source media centre application, which supports all advanced media centre functions, and is accessible to all Windows users.

    In reaching this goal we are working every day to make sure our software is one of the best.

             

  • Support MediaPortal!

    The team works very hard to make sure the community is running the best HTPC-software. We give away MediaPortal for free but hosting and software is not for us.

    Care to support our work with a few bucks? We'd really appreciate it!