souldjer777

Defragmentation for Windows 7 x64

3 posts in this topic

Here is my defragmentation app for Windows 7 x64 - it's handy and I thought I would share as AutoIT has helped me within every aspect of my IT life :)

Thank you AutoIT!  :ILA:

Yes, I expect someone to chime in soon for modifications or whatever... but it's been a while since I posted and I believe this will help some folks at home with Windows 7 where their hard drives aren't SSD or Solid State Drives. It scans everything you have using DriveGetDrive("ALL") and will skip network drives, usb drives, SSD drives etc. I'm working on other OS's now... maybe I'll post a new ver. when I have Windows Server, Windows 8, etc. built in.

 

#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <File.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#RequireAdmin

#Region
#AutoIt3Wrapper_Icon=defrag.ico
#AutoIt3Wrapper_Res_Comment=Windows 7 x64 Defrag
#AutoIt3Wrapper_Res_Description=Windows 7 64 bit defragmentation application
#AutoIt3Wrapper_Res_Fileversion=19.0.0.38
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_LegalCopyright=souldjer777
#EndRegion

Global $ProgramTitle, $idComboBox, $ButtonDefrag01, $ButtonEXIT01, $SRVariable01, $ReturnedValue01, $sFilePath, _
$hFileOpen, $aArray1, $i, $sInfo1, $sInfo2, $sInfo3, $DefragDrive01

$ProgramTitle = "Windows 7 x64 Defrag"

If @OSArch <> "X64" Then
    MsgBox (0, $ProgramTitle, "EXITING - OS Architecture is not 64 bit" & @CR & "Your Architecture: " & @OSArch)
    Exit
EndIf

If @OSVersion <> "WIN_7" Then
    MsgBox (0, $ProgramTitle, "EXITING - OS Type is not Windows 7" & @CR & "Your OS: " & @OSVersion)
    Exit
EndIf

VerGui01()

Func VerGui01()

#Region ### START Koda GUI section ### Form=
$MainGUI1 = GUICreate($ProgramTitle, 298, 214, -1, -1)
GUISetBkColor(0xFFFFFF)
$Label1 = GUICtrlCreateLabel("Windows 7 x64 Defrag", 56, 16, 183, 24, $SS_CENTER)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Label5 = GUICtrlCreateLabel("After Defragmentation:", 80, 103, 135, 25, $SS_CENTER)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$idComboBox = GUICtrlCreateCombo("", 99, 133, 97, 25)
GUICtrlSetData(-1, "DO NOTHING|RESTART|SHUTDOWN", "DO NOTHING")
$ButtonDefrag01 = GUICtrlCreateButton("DEFRAG", 32, 168, 65, 25)
$ButtonEXIT01 = GUICtrlCreateButton("EXIT", 195, 168, 65, 25)
$Label6 = GUICtrlCreateLabel("This application recognizes all fixed disks and only", 25, 56, 244, 17)
$Label2 = GUICtrlCreateLabel("deframents local disks that are non usb or non SSD", 25, 80, 244, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    If $nMsg =-3 Then Exit
    If $nMsg =$ButtonDefrag01 Then  ButtonDisable01()
    If $nMsg =$ButtonEXIT01 Then Exit
WEnd

EndFunc


Func ButtonDisable01()

GUICtrlSetState ($ButtonDefrag01, $GUI_DISABLE)
GUICtrlSetState ($ButtonEXIT01, $GUI_DISABLE)
GUICtrlSetState ($idComboBox, $GUI_DISABLE)
$SRVariable01 = GUICtrlRead ($idComboBox)
LastChance01()

EndFunc


Func LastChance01()

$ReturnedValue01 = MsgBox (1, $ProgramTitle, "Press OK to proceed with defragmentation" & @CR & @CR & _
"After Defragmentation: " & $SRVariable01 & @CR & "OS Architecture: " & @OSArch & @CR & "OS Version: " & @OSVersion & @CR )

If $ReturnedValue01 = 1 Then DriveDetect01()
If $ReturnedValue01 = 2 Then Exit

EndFunc

Func DriveDetect01()

$sFilePath = @ScriptDir & "\drive_detect.txt"

; Create a temporary file to write data to.
If Not _FileCreate($sFilePath) Then
    MsgBox($ProgramTitle, "Error", " Error Creating/Resetting log     error:" & @error)
    Exit
EndIf


; Open the file for writing (append to the end of a file) and store the handle to a variable.
$hFileOpen = FileOpen($sFilePath, 10 )
If $hFileOpen = -1 Then
    MsgBox($ProgramTitle, "", "An error occurred when reading the file     error:" & @error)
    Exit
EndIf

DllCall("kernel32.dll", "int", "Wow64DisableWow64FsRedirection", "int", 1)
EnvSet("PATH", @WindowsDir&"\System32"&';'&@SystemDir)

$aArray1 = DriveGetDrive("ALL")
If @error Then
    ; An error occurred when retrieving the drives.
    MsgBox($ProgramTitle, "", "An error occurred when retreiving drive information     error:" & @error)
Else
    For $i = 1 To $aArray1[0]
        ; Show all the drives found and convert the drive letter to uppercase.
        ;MsgBox($ProgramTitle, "", "Drive " & $i & "/" & $aArray1[0] & ":" & @CRLF & StringUpper($aArray1[$i]))
        ; $sInfo1 > "Unknown", "Removable", "Fixed", "Network", "CDROM", "RAMDisk"
        $sInfo1 = DriveGetType(StringUpper($aArray1[$i]), 1)
        ;MsgBox (0, "", $sInfo1 )
        ; $sInfo2 > "SSD", "" (blank)
        $sInfo2 = DriveGetType(StringUpper($aArray1[$i]), 2)
        ;MsgBox (0, "", $sInfo2 )
        ; $sInfo3 > "Unknown", "SCSI", "ATAPI", "ATA", "1394", "SSA", "Fibre", "USB", "RAID", "iSCSI", "SAS", "SATA", "SD", "MMC", "Virtual", "FileBackedVirtual"
        $sInfo3 = DriveGetType(StringUpper($aArray1[$i]), 3)
        ;MsgBox (0, "", $sInfo3 )
        FileWriteLine($hFileOpen, $i & "," & StringUpper($aArray1[$i]) & "," & $sInfo1 & "," & $sInfo2  & "," & $sInfo3 )
        ; Example:
        ; 1,C:,Fixed,,RAID
        ; 2,D:,CDROM,,RAID
        ; 3,E:,Fixed,,USB
        $DefragDrive01 = StringUpper($aArray1[$i])
        Defrag01 ()
    Next
EndIf

FileClose($hFileOpen)
Shutdown01 ()

EndFunc


Func Defrag01 ()

If $sInfo1 = "Fixed" And $sInfo2 = "" And $sInfo3 <> "USB" And $sInfo3 <> "FileBackedVirtual" And $sInfo3 <> "Virtual" And $sInfo3 <> "SD" And $sInfo3 <> "MMC" And $sInfo3 <> "Unknown" Then
;MsgBox (0, "", 'Drive: ' &$DefragDrive01 & ' Info1: ' & $sInfo1 & ' Info2: ' & $sInfo2 & ' Info3: ' & $sInfo3)
SplashTextOn ($ProgramTitle, 'Defragmenting ' & $DefragDrive01  & @CR & 'Please wait...', 400, 80, -1, -1, 0, "", 10)
; https://social.msdn.microsoft.com/Forums/en-US/1e04b393-1201-4c75-ba89-86860773b68d/calling-dfrguiexe-or-defragexe-c-u-v-on-button-click-x64?forum=Vsexpressvb
RunWait(@ComSpec &' /c echo. & defrag.exe ' & $DefragDrive01 & ' /U /V & echo. ',@WindowsDir&"\System32")
SplashOff()
EndIf

EndFunc

Func Shutdown01()

If $SRVariable01 = "DO NOTHING" Then
    MsgBox (0, $ProgramTitle, "Defragmenation Processes Completed")
    Exit
EndIf
If $SRVariable01 = "RESTART" Then $SRVariable01 = "/r"
If $SRVariable01 = "SHUTDOWN" Then $SRVariable01 = "/s"

SplashTextOn ($ProgramTitle, 'Shutting Down ' & @CR & 'Please wait...', 400, 80, -1, -1, 0, "", 10)
Sleep (5000)
RunWait(@ComSpec & ' /c shutdown ' & $SRVariable01 & ' /t 4 /c "Closing Applications..." /f /d p:4:1', "", @SW_HIDE )
; Optional - Shutdown remote computer...
; https://technet.microsoft.com/en-us/library/cc732503.aspx
; shutdown /s /m \\servername /t 60 /c "Reconfiguring myapp.exe" /f /d p:4:1
SplashOff()
Exit

EndFunc

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Share this post


Link to post
Share on other sites



That is a whole lot of GUI to run a command line against defrag.exe


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I agree... it's a whole lot of unnecessary space - thank you for moving it to Example Scripts... I thought of that after I posted. Sry  :idiot:


"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Share this post


Link to post
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