Jump to content

mp3 directory cleaner


Recommended Posts

I wote this script (well, clobbed together a buch of scripts from these forums) because we lost two 80GB HDs of mp3s at our Low Power FM Radio station after a power spike.

When the files were recovered from the currupt HDs it recovered everything including non .mp3 files and previously deleted junk files.

In order to clean up our data quickly to get on air again I made this little app to slog through the thousands of files and folders.

It basicly removes anything not an .mp3

It also removes empty folders, and logs small .mp3 file to be checked for curruption.

PS. If anyone knows how I can use AutoIt to check a .mp3 file to see if it is currupt properly instead of using file size I'd love to know how it's done. Cheers.

;by cyanide for mofo fm 107.4 - July 2007
;cleans all non .mp3 files and empty folders out of selected directory and any sub directories.

;------------------------------------------------- INCLUDES --------------------------------------------------;

#include<Array.au3>
#include <GUIConstants.au3>
#include <file.au3>
#Include <String.au3>

;------------------------------------------- VARS/ KEY EVENTS ------------------------------------------------;

Dim $c_log = @ScriptDir & "\clean.log"
Dim $ff_log = @ScriptDir & "\flagged.lst"

HotKeySet("{ESC}", "Terminate")
Opt("GUIOnEventMode", 1)

;------------------------------------------------- GUI ---------------------------------------------------;

$mp3Cleaner = GUICreate("mp3 directory cleaner", 400, 325, 200, 100, BitOR($WS_MINIMIZEBOX,$WS_SYSMENU,$WS_GROUP,$WS_BORDER), BitOR($WS_EX_OVERLAPPEDWINDOW,$WS_EX_WINDOWEDGE,$WS_EX_ACCEPTFILES))

GUICtrlCreateGroup (" Options ", 5, 5, 200, 130)
    GUICtrlCreateLabel ("If non .mp3 file found:",  20, 25, 150, 20)
    $radio1 = GUICtrlCreateRadio ("Permantly Delete.", 20, 45, 100, 20)
    $radio2 = GUICtrlCreateRadio ("Send to Recycle Bin.", 20, 65, 120, 20)
    $checkbox1 = GUICtrlCreateCheckbox ("Delete Empty Folders", 20, 90, 120, 20)
    $checkbox2 = GUICtrlCreateCheckbox ("Flag .mp3 file less than", 20, 110, 130, 20)
    $input1 = GuiCtrlCreateInput("100", 150, 110, 30, 20)
    GuiCtrlCreateLabel("KB", 180, 113, 20, 18)
GUICtrlCreateGroup ("",-99,-99,1,1);close group

$dirPL = GUICtrlCreateButton ("Select MP3 Folder and Start Cleaning", 10,143,380,40)
GUICtrlSetTip (-1, "Select Folder and Start Clean")
GUICtrlSetOnEvent ($dirPL, "DirPL")

GUICtrlCreateGroup (" Job Status ", 210, 5, 180, 100)
    $feedback1 = GuiCtrlCreateLabel("Status..................:" & @CRLF & "Files Checked......:" & @CRLF & "Files Removed.....:" & @CRLF & "Folders Removed.:" & @CRLF & "Files Flagged........:", 220, 20, 90, 70)
    $feedback2 = GuiCtrlCreateLabel("Waiting.", 311, 20, 78, 15)
    $feedback3 = GuiCtrlCreateLabel("0", 311, 34, 78, 15)
    $feedback4 = GuiCtrlCreateLabel("0", 311, 47, 78, 15)
    $feedback5 = GuiCtrlCreateLabel("0", 311, 60, 78, 15)
    $feedback6 = GuiCtrlCreateLabel("0", 311, 73, 78, 15)
GUICtrlCreateGroup ("",-99,-99,1,1);close group

$feedback7 = GuiCtrlCreateLabel("(Push 'Esc' to stop clean and quit.)", 220, 110, 180, 20)

GUICtrlCreateGroup (" WARNING! ", 5, 190, 385, 75)
    $feedback8 = GuiCtrlCreateLabel("'mp3 directory cleaner' will delete ALL non .mp3s found in the folder that you select (and sub folders within). Make sure you select the correct directory and that there are no non .mp3 files you want to keep within them." & @CRLF & "USE AT YOUR OWN RISK! - Be careful when selecting your mp3 directory.", 10, 205, 375, 55)
GUICtrlCreateGroup ("",-99,-99,1,1);close group

$button2 = GUICtrlCreateButton ("About / Notes", 310,270,80,20)
GUICtrlSetTip (-1, "About / Notes")
GUICtrlSetOnEvent ($button2, "AboutDC")

$button3 = GUICtrlCreateButton ("Close", 10,270,80,20)
GUICtrlSetTip (-1, "Close")
GUICtrlSetOnEvent ($button3, "Terminate")

GUISetState(@SW_SHOW)

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")

;------------------------------------------------- LOOP --------------------------------------------------;

While 1
    Sleep(1)
WEnd    
    
;------------------------------------------------- FUNCTIONS --------------------------------------------------; 
    
Func DirPL()
    Local $j=0, $k=0
    If GUICtrlRead($radio1) = 4 And GUICtrlRead($radio2) = 4 Then
        MsgBox(0,"Info","Select what you want to do with the non .mp3 files first.")
        Return
    EndIf
    GUICtrlSetData( $feedback2, "Finding files...")
    $AudF = _SubDirFileListToArray("*.*","Add Folder(s)")
    If $AudF = 0 Then
        MsgBox(0,"Info","No folder selected!")
        Return
    EndIf
    GUICtrlSetData($feedback2, "Cleaning...")
    If $AudF[0] = 0 Then
        MsgBox(0,"Info","No files here!")
    EndIf
    If IsArray($AudF) Then
        For $i = 1 To $AudF[0]
            If StringRight(StringLower($AudF[$i]), 4) <> ".mp3" Then
                $o = FileOpen ($c_log, 1)
                FileWrite ($o, "Removed File: " & $AudF[$i] & @CRLF)
                FileClose ($o)
                If GUICtrlRead($radio1) = 1 Then
                    FileDelete ($AudF[$i])
                    $j=$j+1
                    GUICtrlSetData($feedback4, $j)
                EndIf
                If GUICtrlRead($radio2) = 1 Then
                    FileRecycle($AudF[$i])
                    $j=$j+1
                    GUICtrlSetData($feedback4, $j)
                EndIf
            Else
                If GUICtrlRead($checkbox2) = 1 Then
                    $file_size = FileGetSize($AudF[$i])/1024
                    If $file_size < GUICtrlRead($input1) Then
                        $o = FileOpen ($ff_log, 1)
                        FileWrite ($o, $AudF[$i] & @CRLF)
                        FileClose ($o)
                        $k=$k+1
                        GUICtrlSetData($feedback6, $k)
                    EndIf
                EndIf
            EndIf
            GUICtrlSetData( $feedback3, $i)
        Next
    EndIf
    If GUICtrlRead($checkbox1) = 1 Then
        DelEmptyFolders($start_folder)
    EndIf
    GUICtrlSetData($feedback2, "DONE!")
EndFunc

Func _SubDirFileListToArray($filter = "*.*",$dtext = "",$rootdir = "",$flag = "",$initialdir ="");=== now supports multiple filters "*.xyz;*.abc"
    Dim $list[1]=[0]
    $fsf = FileSelectFolder ($dtext,$rootdir,$flag,$initialdir)
    Global $start_folder = $fsf
    If $fsf = StringLeft(@WindowsDir, 3) Then
        MsgBox(0,"WARNING!","YOU CANNOT SELECT YOUR SYSTEM DRIVE. IT IS TO DANGEROUS!" & @LF & "(not to mention fucking stupid)" & @LF & @LF & "Cleaning job will now cancel.")
        Return
    EndIf
    If $fsf = @ProgramFilesDir Then
        MsgBox(0,"WARNING!","YOU CANNOT SELECT YOUR PROGRAM FILES DIRECTORY, IT IS TO DANGEROUS!" & @LF & "(not to mention fucking stupid)" & @LF & @LF & "Cleaning job will now cancel.") 
        Return
    EndIf
    If $fsf = @WindowsDir Then
        MsgBox(0,"WARNING!","YOU CANNOT SELECT YOUR WINDOWS DIRECTORY, TO DANGEROUS!" & @LF & "(not to mention fucking stupid)" & @LF & @LF & "Cleaning job will now cancel.")
        Return
    EndIf
    If @error = 1 Then Return
    $sFilter = StringSplit($filter,";")
    If @error = 1 Then
        $sFilter[0]= 1
        $sFilter[1]=$filter
    EndIf
    For $y =  1 To $sFilter[0]
    $fla1 = _FileListToArray ($fsf, $sFilter[$y], 1)
    If @error = 2 Then Return 0
        If IsArray ($fla1) Then
            For $i = 1 To $fla1[0]
            _ArrayAdd ($list, $fsf&"\"&$fla1[$i])
            Next
        EndIf
        $DLTA = _DirListToArray($fsf)
         If IsArray ($DLTA) Then
            For $i = 1 To $DLTA[0]
            $fla = _FileListToArray ($DLTA[$i],$sFilter[$y], 1)
                If IsArray ($fla) Then
                    For $x = 1 To $fla[0]
                        _ArrayAdd ($list,  $DLTA[$i]&"\"&$fla[$x])
                    Next
                EndIf
            Next
        EndIf
    Next
    $list [0] = UBound ($list) - 1
    Return $list
EndFunc

Func _DirListToArray($sPath);======= By: MsCreatoR
    Local $i, $j, $rlist[1]=[0], $blist, $alist=_FileListToArray ($sPath, '*', 2)
    If IsArray ($alist) Then
        For $i=1 To $alist [0]
            _ArrayAdd ($rlist, $sPath & "\" & $alist [$i])
            $blist = _DirListToArray ($sPath & "\" & $alist [$i])
            If $blist[0]>0 Then
                For $j=1 To $blist [0]
                    _ArrayAdd ($rlist, $blist [$j])
                Next
            EndIf
        Next
    EndIf
    $rlist [0] = UBound ($rlist) - 1
    Return $rlist
EndFunc

Func drop() 
    For $i = 0 To UBound($gaDropFiles) - 1
        openfile($gaDropFiles[$i])
    Next
EndFunc

Func DelEmptyFolders($sPath)
    Local $j=0
    $DLTA = _DirListToArray($sPath)
    If IsArray ($DLTA) Then
        For $i = 1 To $DLTA[0]
            $size = DirGetSize($DLTA[$i])
            If $size = 0 Then
                DirRemove($DLTA[$i], 1)
                $j=$j+1
                GUICtrlSetData($feedback5, $j)
            EndIf
        Next
    EndIf   
EndFunc

Func AboutDC()
    MsgBox(0,"About / Notes", "WARNING! - USE AT YOUR OWN RISK!" & @LF & _
     "'mp3 directory cleaner' will delete ALL non .mp3 files found in the folder that you select (and sub folders within)." & @LF & _
     "Make sure you select the correct directory and that there are no non .mp3 files you want to keep within them." & @LF & _
     "If you are unsure select 'Send to Recycle Bin' so that you can restore the file(s) if necessary." & @LF & @LF & _
     "NOTE: 'mp3 directory cleaner' will NOT allow you to attempt a cleaning job on your System Drive root (ie. C:/), 'Program Files' Directoy or 'WINDOWS' Directory." & @LF & @LF & _
     "A log file (clean.log) will be created if files are deleted or moved to the Recycle Bin. View the log in Notepad if you wish to see what non .mp3 files (and empty folders if option selected) were found and removed/deleted. You will find the log file in the same directory as the progam is run in." & @LF & @LF & _
     "A list of flagged file(s) will be created (flagged.lst) - (if the Flag .mp3 file option is selected) in the same directory as program is run in." & @LF & _
     "View the log in Notepad if you wish to see list of files less than specified size in you mp3 collection (helpfull for finding possible dud files)." & @LF & _
     "")
EndFunc

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            Exit 0
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE 
        Case @GUI_CtrlId = $GUI_EVENT_RESTORE 
    EndSelect
EndFunc

Func Terminate()
    Exit 0
EndFunc
Edited by cyanidemonkey

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...