Saturday, April 2, 2011

XBMC: deleting a file

Because XBMC wasn't designed with storing media info/images in the local folders in mind, when you delete a video from inside XBMC, it leaves behind the .nfo files + images associated with the video.

To keep my media folders clean, I wrote up a quick and dirty AutoIT script.
I'm sure it can be improved (probably massively), but I can't be bothered... it works for me.

These are also scheduled to run first thing every morning

TV ShowsMy TV shows are structured like:
[Drive]:\TV Shows\[ShowName]\[Season #]\[Episode]

;
; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
;
#Include <File.au3>
#Include <Array.au3>
#include <Date.au3>

$tvNormalMoveDir =          "Q:\TV Shows"

;----------------------------------

$FileList= _getdirandsubdirs($tvNormalMoveDir);
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf

;_ArrayDisplay($FileList,"$FileList")

$rows = UBound($FileList)

;Display $FileList's contents
$output = ""
For $r = 0 to UBound($FileList) - 1
    $cont = $tvNormalMoveDir&"\"&$FileList[$r];

    $files=_FileListToArray($cont,"*",1)

    ;_ArrayDisplay($files,"$FileList")

    Dim $arr[100];
    $fpCount = 0;
    $found = 0
    For $c = 1 to UBound($files) - 1;
        $fname = StringTrimRight($files[$c], 4)

        if (($files[$c] == $fname&".avi") OR ($files[$c] == $fname&".flv") OR ($files[$c] == $fname&".wmv")) then
            $found = 1
        elseif (($files[$c] == $fname&".mpg") OR ($files[$c] == $fname&".mpeg") OR ($files[$c] == $fname&".mp4")) then
            $found = 1
        elseif ( ($files[$c] == $fname&".mkv") OR ($files[$c] == $fname&".flv") OR ($files[$c] == $fname&".wmv")) then
            $found = 1
        endif

        if $found = 1 then
            $arr[$fpCount] = $fname;
            $fpCount = $fpCount + 1;
            $found = 0
        endif
    Next

    ;_ArrayDisplay($arr,"$FileList")

    $fpCount = 0;
    $found = 0;

    ; second pass, eliminate all where name doesn't match
    For $c = 1 to UBound($files) - 1;
        $fname = StringTrimRight($files[$c], 4)

        if ($arr[$fpCount] == $fname) then
            ContinueLoop
        else
            if ($arr[$fpCount + 1] <> $fname) then

                if ( ($files[$c] == $fname&".nfo") OR ($files[$c] == $fname&".tbn") ) then
                    $found = 1
                elseif ( ($files[$c] == $fname&".idx") OR ($files[$c] == $fname&".sub") ) then
                    $found = 1
                elseif ( (($files[$c] == $fname&".srt") OR ($files[$c] == $fname&".log") ) then
                    $found = 1
                endif

                if ($found == 1) then
                    $toDel = $tvNormalMoveDir&"\"&$FileList[$r]&"\"&$files[$c];
                    $found = 0

                    if (FileExists ($toDel)) then
                        FileDelete ($toDel)
                        $output = $output & $toDel & @CRLF;
                    endif

                endif
            else
                $fpCount = $fpCount + 1;
            endif
        endif

    Next

    ;_ArrayDisplay($arr,"$FileList")

Next

;MsgBox (0,"",$output)

IF ($output <> "") THEN

    $file = FileOpen($tvNormalMoveDir&"\cleanerLog.log", 1)
    FileWriteLine ( $file, "---------------------------------------------------------" )

    ; Show current local time
    $tCur = _Date_Time_GetLocalTime()
    FileWriteLine ( $file, "Current date/time .: " & _Date_Time_SystemTimeToDateTimeStr($tCur) )
    FileWriteLine ( $file, "===============================================================" )

    FileWriteLine ( $file, $output )
    FileClose($file)

ENDIF

Func _getdirandsubdirs($dir)
Dim $Array1[1];
$c=1;
$d=0;
$temp="";
$folders=_FileListToArray($dir,"*",2)
While $c < UBound($folders, 1)
    $temp=_FileListToArray($dir&"\"&$folders[$c],"*.",2)
    $d=1;

    While $d < UBound($temp, 1)
        if ($temp[$d] <> ".actors") then
            _ArrayAdd($folders,$folders[$c]&"\"&$temp[$d])
            _ArrayAdd($Array1, $folders[$c]&"\"&$temp[$d])
        endif
        $d=$d+1;
    WEnd
    $c=$c+1;
WEnd
_ArrayDelete($Array1, 0)
return $Array1;
EndFunc


MoviesMy Movies are structured like:
[Drive]:\Movies\[Name (Year)]\[Movie]

; AutoIt Version: 3.0
; Language:       English
; Platform:       Win9x/NT
;
#Include <File.au3>
#Include <Array.au3>
#include <Date.au3>

$movieNormalMoveDir =       "Q:\Movies"

;----------------------------------

$FileList= _getdirandsubdirs($movieNormalMoveDir);
If @Error=1 Then
    MsgBox (0,"","No Folders Found.")
    Exit
EndIf
;_ArrayDisplay($FileList,"$FileList")

$rows = UBound($FileList)

;Display $FileList's contents
$output = ""
For $r = 0 to UBound($FileList) - 1
    $cont = $movieNormalMoveDir&"\"&$FileList[$r];

    ; Shows the filenames of all files in the current directory.
    $search = FileFindFirstFile($cont&"\*.avi")

    ; Check if the search was successful
    If $search = -1 Then
        $search = FileFindFirstFile($cont&"\*.mp4")
        If $search = -1 Then
            $search = FileFindFirstFile($cont&"\*.mkv")
            If $search = -1 Then
                $search = FileFindFirstFile($cont&"\*.mpg")
                If $search = -1 Then
                    $search = FileFindFirstFile($cont&"\*.mpeg")
                    If $search = -1 Then
                        $search = FileFindFirstFile($cont&"\*.wmv")
                        If $search = -1 Then
                            $search = FileFindFirstFile($cont&"\*.flv")
                        EndIf
                    EndIf
                EndIf
            EndIf
        EndIf
    EndIf

    If ($search = -1) Then
        if (FileExists ($cont)) then
            $size = DirGetSize($cont, 1)
            $megs = Round($size[0] / 1024 / 1024)
            if ($megs < 100) then
                $output = $output & $cont & @CRLF & "Size (MB): "& $megs & @CRLF;
                DirRemove($cont, 1)
            endif
        endif
    endif

    ; Close the search handle
    FileClose($search)

Next

IF ($output <> "") THEN

    $file = FileOpen($movieNormalMoveDir&"\cleanerLog.log", 1)
    FileWriteLine ( $file, "---------------------------------------------------------" )

    ; Show current local time
    $tCur = _Date_Time_GetLocalTime()
    FileWriteLine ( $file, "Current date/time .: " & _Date_Time_SystemTimeToDateTimeStr($tCur) )
    FileWriteLine ( $file, "===============================================================" )

    FileWriteLine ( $file, $output )
    FileClose($file)

ENDIF



Func _getdirandsubdirs($dir)
$c=1;
$d=0;
$temp="";
$folders=_FileListToArray($dir,"*",2)

Dim $arrayDel[1]

While $c < UBound($folders, 1)
    $temp=_FileListToArray($dir&"\"&$folders[$c],"*",2)
    $d=1;

    While $d < UBound($temp, 1)
        if ($temp[$d] <> ".actors") then
            _ArrayAdd($folders,$folders[$c]&"\"&$temp[$d])
            _ArrayAdd($arrayDel,$folders[$c])
        endif
        $d=$d+1;
    WEnd
    $c=$c+1;
WEnd

_ArrayDelete($arrayDel, 0)
$aNewArray = _ArrayUnique($arrayDel)
;_ArrayDisplay($aNewArray,"$FileList")

For $x = 1 to UBound($aNewArray) - 1;
    ; lookup existing entry
    $iKeyIndex = _ArraySearch($folders, $aNewArray[$x])
    If Not @error Then
        _ArrayDelete($folders, $iKeyIndex)
    EndIf
Next

_ArrayDelete($folders, 0)
return $folders;
EndFunc

No comments:

Post a Comment