Jump to content

Defrag Elby (Schedule Folder DeFragment)


Nologic

Recommended Posts

Nologic

Why:

Wanted to do a quick folder defrag for my Windows based server and clients, given that I update media fairly frequently.

 

You may wish to edit the script to defrag less frequently, as I have it setup to defrag every day at 5am.

 

References:

Microsoft

Stackoverflow

 

Requirements:

AutoIt

Auslogics Disk Defrag (silent)

or

Piriform Defraggler (console pops up briefly)

 

How:

This one hit wonder of a script, simply finds one of the above mentioned free defragger's, and sets a task in Windows Scheduler to trigger a folder defrag with that defragger.

 

Instructions:

Install AutoIt and set it to execute scripts when double clicked.

 

Install one of the Drive Defragmentation app's listed above.

 

Now Right Click on desktop and select "New\AutoIt v3 Script" from the context menu, and Rename it "Defrag Emby.au3"

 

Now Right Click "Defrag Emby.au3" & select "Edit Script" from the context menu.

 

Now paste the following code into:

; Auslogics Disk Defrag
;    http://www.auslogics.com/en/software/disk-defrag/
; Defraggler
;    https://www.piriform.com/defraggler/download

; To Remove Task Via The CMD Console
; schtasks /delete /tn "Defrag Emby" /f

; 24 Hour Clock Time
$sTime = '05:00'

$sAuslogics  = _Auslogics_FileFind()
$sDefraggler = _Defraggler_RegRead()

$sString = ''
Select
    Case FileExists( @SystemDir & '\schtasks.exe' ) = 0
        MsgBox( 0 , 'Error:', 'schtasks.exe Not Present.' )
        Exit
    Case $sAuslogics <> ''
        $sString &= _Auslogics_String( $sAuslogics , $sString , @AppDataCommonDir & '\MediaBrowser' )
        $sString &= _Auslogics_String( $sAuslogics , $sString , @AppDataCommonDir & '\MediaBrowser-Classic' )
        $sString &= _Auslogics_String( $sAuslogics , $sString , @AppDataDir & '\MediaBrowser-Classic' )
        $sString &= _Auslogics_String( $sAuslogics , $sString , @AppDataDir & '\MediaBrowser-Server'  )
        $sString &= _Auslogics_String( $sAuslogics , $sString , @AppDataDir & '\MediaBrowser-Theater' )
    Case $sDefraggler <> ''
        $sString &= _Defraggler_String( $sDefraggler , $sString , @AppDataCommonDir & '\MediaBrowser' )
        $sString &= _Defraggler_String( $sDefraggler , $sString , @AppDataCommonDir & '\MediaBrowser-Classic' )
        $sString &= _Defraggler_String( $sDefraggler , $sString , @AppDataDir & '\MediaBrowser-Classic' )
        $sString &= _Defraggler_String( $sDefraggler , $sString , @AppDataDir & '\MediaBrowser-Server'  )
        $sString &= _Defraggler_String( $sDefraggler , $sString , @AppDataDir & '\MediaBrowser-Theater' )
        If $sString <> '' Then $sString &= ' /S'
    Case Else
        MsgBox( 0 , 'Error:', 'Required Files Not Present.' )
        Exit
EndSelect
If $sString = '' Then
    MsgBox( 0 , 'Error:', 'Required Paths Not Present.' )
    Exit
Else
    ; Remove Old Task If Exists
    Run( @ComSpec & ' /c SchTasks /DELETE /TN "Defrag Emby" /F ' , '' , @SW_HIDE )
    ; Create New Task
    Run( @ComSpec & ' /c SchTasks /Create /SC DAILY /TN "Defrag Emby" /TR "' & $sString & '" /ST ' & $sTime , '' , @SW_HIDE )
EndIf
MsgBox( 0 , 'Complete:' , 'Operation has Completed.' )


Func _Auslogics_FileFind()
    $String = _ProgramFilesDir() & '\Auslogics\DiskDefrag\cdefrag.exe'
    If FileExists( $String ) Then
        Return $String
    Else
        Return ''
    EndIf
EndFunc

Func _Defraggler_RegRead()
    $String = RegRead( 'HKCU\SOFTWARE\Piriform\Defraggler' , 'InstallPath' )
    If $String  = '' Then $String =  RegRead( 'HKCU64\SOFTWARE\Piriform\Defraggler' , 'InstallPath' )
    If $String <> '' Then $String &= '\df.exe'
    If FileExists( $String ) Then
        Return $String
    Else
        Return ''
    EndIf
EndFunc

Func _ProgramFilesDir()
    Local $ProgramFileDir
    Switch @OSArch
        Case "X86"
            $ProgramFileDir = "Program Files"
        Case "X64"
            $ProgramFileDir = "Program Files (x86)"
    EndSwitch
    Return @HomeDrive & '\' & $ProgramFileDir
EndFunc

Func _Auslogics_String( $sAppPath , $sExcuteString , $sFolderPath )
    If FileExists( $sFolderPath ) Then
        If StringInStr( $sFolderPath , @AppDataCommonDir ) <> 0 Then $sFolderPath = StringReplace( $sFolderPath , @AppDataCommonDir , '%ProgramData%' )
        If StringInStr( $sFolderPath , @AppDataDir       ) <> 0 Then $sFolderPath = StringReplace( $sFolderPath , @AppDataDir       , '%APPDATA%' )
        If $sExcuteString = '' Then
            Return '\"' & $sAppPath & '\" -path:' & $sFolderPath & ' -quiet'
        Else
            Return ' && \"' & $sAppPath & '\" -path:' & $sFolderPath & ' -quiet'
        EndIf
    Else
        Return ''
    EndIf
EndFunc

Func _Defraggler_String( $sAppPath , $sExcuteString , $sFolderPath )
    If FileExists( $sFolderPath ) Then
        If StringInStr( $sFolderPath , @AppDataCommonDir ) <> 0 Then $sFolderPath = StringReplace( $sFolderPath , @AppDataCommonDir , '%ProgramData%' )
        If StringInStr( $sFolderPath , @AppDataDir       ) <> 0 Then $sFolderPath = StringReplace( $sFolderPath , @AppDataDir       , '%APPDATA%' )
        If $sExcuteString = '' Then
            Return '\"' & $sAppPath & '\" ' & $sFolderPath
        Else
            Return ' ' & $sFolderPath
        EndIf
    Else
        Return ''
    EndIf
EndFunc

Now save the script & execute it by double clicking it and if all went well...then delete the script and go do something else. :)

 

Note:

If you have working command lines for other defragger's I'm more than willing to add support for them. They don't need to be freeware ether.

 

** Update 1 **

Refactored code to be less Clumsy & Cleaner

Added a few more paths to test for.

Fixed fat fingering of Emby name...which resulted in "Defrag Elby" as the task name...and well this thread name...my copy & paste fetish causes a lot of problems some times.

Edited by Nologic
  • Like 3
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
×
×
  • Create New...