Jump to content

Nice script for JKDefrag


jslegers
 Share

Recommended Posts

Hi,

I have made a nice little script for the use with JKDefrag http://www.kessels.com/Jkdefrag/

The script runs an analyze first and then a defragmentation based on the settings in the ini file.

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=Defrag.ico
#AutoIt3Wrapper_outfile=Defrag.exe
#AutoIt3Wrapper_Res_Description=Defrag script for JKDefrag
#AutoIt3Wrapper_Res_Fileversion=1.3.1.0
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****

#include <Date.au3>

; Setting autoit options
AutoItSetOption ( "TrayMenuMode", 1 )
HotKeySet ( "#{ESC}", "Quit" )
TraySetState ( 1 )

If IniRead ( @ScriptDir & "\" & "Defrag.ini", "General", "TrayIcon", 1 ) = 0 Then
    TraySetState ( 2 )
EndIf

; Determing language and setting variables
If @OSLang = 0409 Then
    $StartAn = "Starting analyze of fragmentation at "
    $Analy = "Analyzing "
    $Start = "Starting defragmentation of "
    $Defrag = "Defragmenting "
    $NoDefrag = "No defragmentation needed for "
    $Drive = " drive."
ElseIf @OSLang = 0413 Then
    $StartAn = "Start met de analyse van de fragmentatie om "
    $Analy = "Analiseren van de "
    $Start = "Starten van defragmenteren "
    $Defrag = "Defragmenteren van de "
    $NoDefrag = "Geen defragmentatie nodig van "
    $Drive = " schijf."
EndIf

$Varfragini = IniRead ( @ScriptDir & "\" & "Defrag.ini", "General", "FragmentedFiles", 100 )
$Vartimeini = IniRead ( @ScriptDir & "\" & "Defrag.ini", "General", "Timeout", 5 )
;ConsoleWrite($Vartimeini & @LF)

; Adding 5 minutes to the current time
$sTime = _DateAdd ( 'n', $Vartimeini, _NowCalc())
$sNewTime = _DateTimeFormat ( $sTime, 4 )

; Setting Tray Icons options.
TraySetToolTip ( $StartAn & $sNewTime )

; Waiting for x minutes.
Sleep ( $Vartimeini * 60000 )

$Vardrive = DriveGetDrive( "FIXED" )
If NOT @error Then
    For $i = 1 to $Vardrive[0]
    ; Analyzing drive.
        TrayTip ( "", $Analy & StringUpper ( $Vardrive[$i] ) & $Drive, 3, 1 )
        $JkDefragCom = "C:\Program Files\Defrag\JkDefragCmd.exe" 
        $JkParam = "-a 1 -l C:\Analyze_" & StringUpper ( StringTrimRight ( $Vardrive[$i], 1 )) & ".log " & $Vardrive[$i]
        ShellExecuteWait ( $JkDefragCom, $JkParam,  "", "", @SW_HIDE )
        $Log = FileReadLine ( "C:\Analyze_" & StringTrimRight ( $Vardrive[$i], 1 ) & ".log" ,17 )
        $Ret = StringRegExp($Log, ": ([0-9]{0,}),", 1, 1)
        FileDelete ( "C:\Analyze_" & StringTrimRight ( $Vardrive[$i], 1 ) & ".log" )
    ;ConsoleWrite($Ret[0] & @LF)
        If $Ret[0] > $Varfragini Then
        ; Starting defragmentation of drive.
            TrayTip ( "", $Start & StringUpper ( $Vardrive[$i] ) & $Drive, 3, 1 )
            Sleep ( 3000 )
            TrayTip ( "", "", 0 )
            TraySetToolTip ( $Defrag & StringUpper ( $Vardrive[$i] ) & $Drive )
            $JkParam = "-a 3 -d 1 -s 100 -f 1 -l C:\Defrag_" & StringUpper ( StringTrimRight ( $Vardrive[$i], 1 )) & ".log " & $Vardrive[$i]
            ShellExecuteWait ( $JkDefragCom, $JkParam, "", "",  @SW_HIDE )
        Else
            TrayTip ( "Status", $NoDefrag & StringUpper ( $Vardrive[$i] ) & $Drive, 10, 1 )
            Sleep ( 3000 )
        EndIf
    Next
EndIf

; Function Quit to stop the script.
Func Quit()
    ProcessClose ( "JkDefragCmd.exe" )
    Exit
EndFunc

Here is the ini file named Defrag.ini :

[General]
FragmentedFiles=5
Timeout=1
TrayIcon=1

Have fun with it.

Link to comment
Share on other sites

  • 1 year later...

Hi,

I have made a nice little script for the use with JKDefrag http://www.kessels.com/Jkdefrag/

The script runs an analyze first and then a defragmentation based on the settings in the ini file.

Great job

would it be possible to make it defrag only when you are not actively using the computer, I mean on idle?

Link to comment
Share on other sites

Hey guys, thought i would take a stab at adding idle defragmentation. i still need to add in the fragmented file checking from the analysis so that the utility knows whether or not defragmenting is even necessary... (done) so here is my beta, updates to come.

#include <Constants.au3>
#include <Timers.au3>
#include <Array.au3>
#include <Date.au3>
#include <File.au3>
#NoTrayIcon

Opt("TrayMenuMode", 3) ; Default tray menu items (Script Paused/Exit) will not be shown.
Global $idsettings = @ScriptDir & "\IDSettings.ini", $LogDir = iniVarGet("LogDir", @ScriptDir & "\logs"), $FragThresh = iniVarGet("FragThresh", 100), $IdleSet = iniVarGet("IdleSet", 30), $idlerun = 0

$settingsitem = TrayCreateMenu("Settings")
$Idle15_item = TrayCreateItem("Set idle to 15 minutes", $settingsitem, -1, 1)
If $IdleSet = 15 Then TrayItemSetState($Idle15_item, $TRAY_CHECKED)
$Idle30_item = TrayCreateItem("Set idle to 30 minutes", $settingsitem, -1, 1)
If $IdleSet = 30 Then TrayItemSetState($Idle30_item, $TRAY_CHECKED)
$Idle60_item = TrayCreateItem("Set idle to 60 minutes", $settingsitem, -1, 1)
If $IdleSet = 60 Then TrayItemSetState($Idle60_item, $TRAY_CHECKED)
TrayCreateItem("", $settingsitem)
$LogDir_item = TrayCreateItem("Choose Log Directory", $settingsitem)
TrayCreateItem("", $settingsitem)
$FragThresh_item = TrayCreateItem("Fragmentation Threshhold", $settingsitem)
TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()
$Vardrive = DriveGetDrive("FIXED")
$JkDefragCom = FindJKDefrag()

If FileExists($LogDir) = 0 Then
    DirCreate($LogDir)
EndIf

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            If _Timer_GetIdleTime() >= $IdleSet * 60000 And ProcessExists("JkDefragCmd.exe") = 0 And $idlerun = 0 Then
                $idlerun = 1
                For $i = 1 To $Vardrive[0]
                    TrayTip("IdleDefrag", "Analyzing " & $Vardrive[$i], 20)
                    $JkParamAnalyze = '-a 1 -l "' & $LogDir & '\' & @YEAR & '-' & @MON & '-' & @MDAY & '_Analyze_' & StringUpper(StringTrimRight($Vardrive[$i], 1)) & '.log" ' & $Vardrive[$i]
                    ShellExecuteWait($JkDefragCom, $JkParamAnalyze, "", "", @SW_HIDE)
                    $AnalysisRes = FileReadLine($LogDir & '\' & @YEAR & '-' & @MON & '-' & @MDAY & '_Analyze_' & StringUpper(StringTrimRight($Vardrive[$i], 1)) & '.log', 18)
                    $AnalysisResSplit = StringSplit(StringStripWS($AnalysisRes, 8), ":(", 0)
                    If Number($AnalysisResSplit[4]) > Number ($FragThresh) Then
                        TrayTip("IdleDefrag", "Defragmenting " & $Vardrive[$i] & " - " & $AnalysisResSplit[4] & "/" & $FragThresh, 20)
                        $JkParamDefrag = '-a 3 -d 1 -s 100 -f 1 -l "' & $LogDir & '\' & @YEAR & '-' & @MON & '-' & @MDAY & '_Defrag_' & StringUpper(StringTrimRight($Vardrive[$i], 1)) & '.log" ' & $Vardrive[$i]
                        ShellExecute($JkDefragCom, $JkParamDefrag, "", "", @SW_HIDE)
                    EndIf
                Next
            ElseIf _Timer_GetIdleTime() < 100 And ProcessExists("JkDefragCmd.exe") Then
                $idlerun = 0
                ProcessClose("JkDefragCmd.exe")
            EndIf
            ContinueLoop
        Case $msg = $LogDir_item
            $LogDir = FileSelectFolder("Choose a log folder", "")
            iniVarSet("LogDir", '"' & $LogDir & '"')
        Case $msg = $aboutitem
            MsgBox(64, "About", "Created by Jslegers, remixed by Ghetek." & @CRLF & @CRLF & "Idle: " & $IdleSet & " minutes" & @CRLF & "Log dir: " & $LogDir & @CRLF & "Fragmentation Threshhold: " & $FragThresh)
        Case $msg = $Idle15_item
            TrayItemSetState($Idle15_item, $TRAY_CHECKED)
            $IdleSet = 15
            iniVarSet("IdleSet", 15)
        Case $msg = $Idle30_item
            TrayItemSetState($Idle30_item, $TRAY_CHECKED)
            $IdleSet = 30
            iniVarSet("IdleSet", 30)
        Case $msg = $Idle60_item
            TrayItemSetState($Idle60_item, $TRAY_CHECKED)
            $IdleSet = 60
            iniVarSet("IdleSet", 60)
        Case $msg = $FragThresh_item
            $FragThresh = InputBox("IdleDefrag","Defragmentation Threshhold",iniVarGet("FragThresh", 100))
            iniVarSet("FragThresh", $FragThresh)
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Quit()

Func iniVarGet($SettingKey, $SettingDef)
    Return IniRead($idsettings, "Settings", $SettingKey, $SettingDef)
EndFunc   ;==>iniVarGet

Func iniVarSet($SettingKey, $SettingVal)
    If FileExists($idsettings) = 0 Then
        _FileCreate($idsettings)
    EndIf
    IniWrite($idsettings, "Settings", $SettingKey, $SettingVal)
EndFunc   ;==>iniVarSet

Func FindJKDefrag()
    If FileExists("C:\Program Files\Defrag\JkDefragCmd.exe") Then
        Return "C:\Program Files\Defrag\JkDefragCmd.exe"
    ElseIf FileExists(@ScriptDir & "\JkDefragCmd.exe") Then
        Return @ScriptDir & "\JkDefragCmd.exe"
    EndIf
EndFunc   ;==>FindJKDefrag

Func Quit()
    ProcessClose("JkDefragCmd.exe")
    Exit
EndFunc   ;==>Quit

as you can see the analysis portion of the jkdefrag cannot be stopped once started, only the actual defrag process. i hope to change this by moving from the shellexecute to a normal run so that i can track the pid. I also need to create an ini file and/or registry area to store the settings. (done)

Edited by ghetek
Link to comment
Share on other sites

ame='automatedfaults' date='22 November 2009 - 06:08 AM' timestamp='1258895304' post='747650']

Hey guys, thought i would take a stab at adding idle defragmentation. i still need to add in the fragmented file checking from the analysis so that the utility knows whether or not defragmenting is even necessary... (done) so here is my beta, updates to come.

#include <Constants.au3>
#include <Timers.au3>
#include <Array.au3>
#include <Date.au3>
#include <File.au3>
#NoTrayIcon

Opt("TrayMenuMode", 3) ; Default tray menu items (Script Paused/Exit) will not be shown.
Global $idsettings = @ScriptDir & "\IDSettings.ini", $LogDir = iniVarGet("LogDir", @ScriptDir & "\logs"), $FragThresh = iniVarGet("FragThresh", 100), $IdleSet = iniVarGet("IdleSet", 30), $idlerun = 0

$settingsitem = TrayCreateMenu("Settings")
$Idle15_item = TrayCreateItem("Set idle to 15 minutes", $settingsitem, -1, 1)
If $IdleSet = 15 Then TrayItemSetState($Idle15_item, $TRAY_CHECKED)
$Idle30_item = TrayCreateItem("Set idle to 30 minutes", $settingsitem, -1, 1)
If $IdleSet = 30 Then TrayItemSetState($Idle30_item, $TRAY_CHECKED)
$Idle60_item = TrayCreateItem("Set idle to 60 minutes", $settingsitem, -1, 1)
If $IdleSet = 60 Then TrayItemSetState($Idle60_item, $TRAY_CHECKED)
TrayCreateItem("", $settingsitem)
$LogDir_item = TrayCreateItem("Choose Log Directory", $settingsitem)
TrayCreateItem("", $settingsitem)
$FragThresh_item = TrayCreateItem("Fragmentation Threshhold", $settingsitem)
TrayCreateItem("")
$aboutitem = TrayCreateItem("About")
TrayCreateItem("")
$exititem = TrayCreateItem("Exit")

TraySetState()
$Vardrive = DriveGetDrive("FIXED")
$JkDefragCom = FindJKDefrag()

If FileExists($LogDir) = 0 Then
    DirCreate($LogDir)
EndIf

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            If _Timer_GetIdleTime() >= $IdleSet * 60000 And ProcessExists("JkDefragCmd.exe") = 0 And $idlerun = 0 Then
                $idlerun = 1
                For $i = 1 To $Vardrive[0]
                    TrayTip("IdleDefrag", "Analyzing " & $Vardrive[$i], 20)
                    $JkParamAnalyze = '-a 1 -l "' & $LogDir & '\' & @YEAR & '-' & @MON & '-' & @MDAY & '_Analyze_' & StringUpper(StringTrimRight($Vardrive[$i], 1)) & '.log" ' & $Vardrive[$i]
                    ShellExecuteWait($JkDefragCom, $JkParamAnalyze, "", "", @SW_HIDE)
                    $AnalysisRes = FileReadLine($LogDir & '\' & @YEAR & '-' & @MON & '-' & @MDAY & '_Analyze_' & StringUpper(StringTrimRight($Vardrive[$i], 1)) & '.log', 18)
                    $AnalysisResSplit = StringSplit(StringStripWS($AnalysisRes, 8), ":(", 0)
                    If Number($AnalysisResSplit[4]) > Number ($FragThresh) Then
                        TrayTip("IdleDefrag", "Defragmenting " & $Vardrive[$i] & " - " & $AnalysisResSplit[4] & "/" & $FragThresh, 20)
                        $JkParamDefrag = '-a 3 -d 1 -s 100 -f 1 -l "' & $LogDir & '\' & @YEAR & '-' & @MON & '-' & @MDAY & '_Defrag_' & StringUpper(StringTrimRight($Vardrive[$i], 1)) & '.log" ' & $Vardrive[$i]
                        ShellExecute($JkDefragCom, $JkParamDefrag, "", "", @SW_HIDE)
                    EndIf
                Next
            ElseIf _Timer_GetIdleTime() < 100 And ProcessExists("JkDefragCmd.exe") Then
                $idlerun = 0
                ProcessClose("JkDefragCmd.exe")
            EndIf
            ContinueLoop
        Case $msg = $LogDir_item
            $LogDir = FileSelectFolder("Choose a log folder", "")
            iniVarSet("LogDir", '"' & $LogDir & '"')
        Case $msg = $aboutitem
            MsgBox(64, "About", "Created by Jslegers, remixed by Ghetek." & @CRLF & @CRLF & "Idle: " & $IdleSet & " minutes" & @CRLF & "Log dir: " & $LogDir & @CRLF & "Fragmentation Threshhold: " & $FragThresh)
        Case $msg = $Idle15_item
            TrayItemSetState($Idle15_item, $TRAY_CHECKED)
            $IdleSet = 15
            iniVarSet("IdleSet", 15)
        Case $msg = $Idle30_item
            TrayItemSetState($Idle30_item, $TRAY_CHECKED)
            $IdleSet = 30
            iniVarSet("IdleSet", 30)
        Case $msg = $Idle60_item
            TrayItemSetState($Idle60_item, $TRAY_CHECKED)
            $IdleSet = 60
            iniVarSet("IdleSet", 60)
        Case $msg = $FragThresh_item
            $FragThresh = InputBox("IdleDefrag","Defragmentation Threshhold",iniVarGet("FragThresh", 100))
            iniVarSet("FragThresh", $FragThresh)
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Quit()

Func iniVarGet($SettingKey, $SettingDef)
    Return IniRead($idsettings, "Settings", $SettingKey, $SettingDef)
EndFunc   ;==>iniVarGet

Func iniVarSet($SettingKey, $SettingVal)
    If FileExists($idsettings) = 0 Then
        _FileCreate($idsettings)
    EndIf
    IniWrite($idsettings, "Settings", $SettingKey, $SettingVal)
EndFunc   ;==>iniVarSet

Func FindJKDefrag()
    If FileExists("C:\Program Files\Defrag\JkDefragCmd.exe") Then
        Return "C:\Program Files\Defrag\JkDefragCmd.exe"
    ElseIf FileExists(@ScriptDir & "\JkDefragCmd.exe") Then
        Return @ScriptDir & "\JkDefragCmd.exe"
    EndIf
EndFunc   ;==>FindJKDefrag

Func Quit()
    ProcessClose("JkDefragCmd.exe")
    Exit
EndFunc   ;==>Quit

as you can see the analysis portion of the jkdefrag cannot be stopped once started, only the actual defrag process. i hope to change this by moving from the shellexecute to a normal run so that i can track the pid. I also need to create an ini file and/or registry area to store the settings. (done)

Looks good will give it a tryout
Link to comment
Share on other sites

  • 2 months later...

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...