Checkboxes for drives and options are added dynamically depending on your hardware and OS. The entire GUI is automatically sized to fit everything neatly. I'm quite proud of this ;-)
Note: You'll need AHK 1.0.10+ for the DriveGet command (Thanks!) and also AU3GUI.exe (I suggest you just put it in C:\Windows)
; Determine drives with free space and defragment them ; This script requires AU3GUI.exe (NOT AU3GUIXP.exe) #NoTrayIcon SetBatchLines,-1 StringTrimRight,title,A_ScriptName,4 ; Basic parameters for the GUI; Height & focus set later EnvSet,GUI.title,%title% EnvSet,GUI.x,-1 EnvSet,GUI.y,-1 EnvSet,GUI.w,300 EnvSet,GUI.icon,defrag.ico EnvSet,GUI.action,1 EnvSet,GUI.file,%temp%\au3gui.ini ; Now add GUI objects as needed n=0 ; GUI object number y=5 ; position of object yg=%y% ; position of group hg=25 ; height of empty group ; Get list of drives, remove floppies (prevent slow & noisy attempt to read empty drive) DriveGet,list,list StringReplace,list,list,A, StringReplace,list,list,B, ; Add a checkbox for each drive with free space and save drives list Loop,Parse,list { DriveSpaceFree,free,%A_LoopField%:\ If free > 0 { GoSub,AddDriveCheckBox SetEnv,drives,%drives%%A_LoopField% } } ; Put a group box around drive checkboxes n++ EnvSet,OBJ%n%.type,group EnvSet,OBJ%n%.x,10 EnvSet,OBJ%n%.y,%yg% EnvSet,OBJ%n%.w,280 EnvSet,OBJ%n%.h,%hg% EnvSet,OBJ%n%.text,Drives to defragment ; Option checkboxes EnvAdd,y,30 yg=%y% ; position of group hg=25 ; height of empty group ; Option to scan drives for errors first (win98 only) IfEqual,A_OSVersion,WIN_98 { n++ n_scan=%n% EnvAdd,y,20 EnvAdd,hg,20 EnvSet,OBJ%n%.type,checkbox EnvSet,OBJ%n%.x,20 EnvSet,OBJ%n%.y,%y% EnvSet,OBJ%n%.text,Check drives for errors first EnvSet,OBJ%n%.selected,1 EnvSet,OBJ%n%.tooltip,Use scandisk or chkdsk to check each drive for errors before trying to defragment it ; Option for full or quick defragment (Win98 only) n++ n_full=%n% EnvAdd,y,20 EnvAdd,hg,20 EnvSet,OBJ%n%.type,checkbox EnvSet,OBJ%n%.x,20 EnvSet,OBJ%n%.y,%y% EnvSet,OBJ%n%.text,Full defragment (including free space) EnvSet,OBJ%n%.selected,1 EnvSet,OBJ%n%.tooltip,Defragments files and free space, but can take much longer } ; Option to run minimized n++ n_min=%n% EnvAdd,y,20 EnvAdd,hg,20 EnvSet,OBJ%n%.type,checkbox EnvSet,OBJ%n%.x,20 EnvSet,OBJ%n%.y,%y% EnvSet,OBJ%n%.text,Run defragment minimized EnvSet,OBJ%n%.selected,1 EnvSet,OBJ%n%.tooltip,Run programs minimized so you can continue working without interruption ; Option to shut down when finished n++ n_shut=%n% EnvAdd,y,20 EnvAdd,hg,20 EnvSet,OBJ%n%.type,checkbox EnvSet,OBJ%n%.x,20 EnvSet,OBJ%n%.y,%y% EnvSet,OBJ%n%.text,Shut down the computer when done EnvSet,OBJ%n%.selected,0 EnvSet,OBJ%n%.tooltip,Shut down the computer when defragmentation is finished ; Put a group box around option checkboxes n++ EnvSet,OBJ%n%.type,group EnvSet,OBJ%n%.x,10 EnvSet,OBJ%n%.y,%yg% EnvSet,OBJ%n%.w,280 EnvSet,OBJ%n%.h,%hg% EnvSet,OBJ%n%.text,Options ; Buttons for start or cancel n++ EnvAdd,y,40 EnvSet,OBJ%n%.type,button EnvSet,OBJ%n%.x,50 EnvSet,OBJ%n%.y,%y% EnvSet,OBJ%n%.w,60 EnvSet,OBJ%n%.h,-1 EnvSet,OBJ%n%.text,Start EnvSet,OBJ%n%.submit,1 EnvSet,OBJ%n%.close,1 EnvSet,GUI.focus,%n% ; Start button has focus n++ EnvSet,OBJ%n%.type,button EnvSet,OBJ%n%.x,190 EnvSet,OBJ%n%.y,%y% EnvSet,OBJ%n%.w,60 EnvSet,OBJ%n%.h,-1 EnvSet,OBJ%n%.text,Cancel EnvSet,OBJ%n%.cancel,1 EnvAdd,y,30 EnvSet,GUI.h,%y% ; height of GUI to contain displayed objects neatly ; Display the GUI FileDelete,%temp%\au3gui.ini RunWait,au3gui ; Read and process the responses IniRead,Status,%temp%\au3gui.ini,GUI,GUI IfNotEqual,Status,SUBMIT,Exit ; User cancelled IniRead,scan,%temp%\au3gui.ini,GUI,OBJ%n_scan% IniRead,full,%temp%\au3gui.ini,GUI,OBJ%n_full% IniRead,min, %temp%\au3gui.ini,GUI,OBJ%n_min% IniRead,shut,%temp%\au3gui.ini,GUI,OBJ%n_shut% Loop,Parse,drives { IniRead,drive,%temp%\au3gui.ini,GUI,OBJ%A_Index% IfEqual,drive,checked,GoSub,Defragment } ; All done! FileDelete,%temp%\au3gui.ini IfEqual,shut,checked,Shutdown 9 Exit ;******************************************************************************* ; Add a checkbox for a drive ;******************************************************************************* AddDriveCheckBox: n++ EnvAdd,y,20 EnvAdd,hg,20 EnvSet,OBJ%n%.type,checkbox EnvSet,OBJ%n%.x,20 EnvSet,OBJ%n%.y,%y% ; Get the drive volume label (thanks to new function!) DriveGet,label,label,%A_LoopField%: StringLower,label,label,T EnvSet,OBJ%n%.text,%label% (%A_LoopField%:) - %free%MB free ; Select only fixed drives by default (some removable drives can be defragmented) DriveGet,type,type,%A_LoopField%: IfEqual,type,fixed,EnvSet,OBJ%n%.selected,1 Return ;******************************************************************************* ; Defragment a drive ;******************************************************************************* Defragment: IfEqual,min,checked,SetEnv,runopt,min ; Minimize IfEqual,A_OSVersion,WIN_98 { IfEqual,full,checked SetEnv,option,/f Else SetEnv,option,/u IfEqual,scan,checked,RunWait,%windir%\scandskw.exe %A_LoopField%: /noninteractive /silent,,%runopt% RunWait,%windir%\defrag.exe %A_LoopField%: /noprompt %option%,,%runopt% } Else RunWait,defrag.exe %A_LoopField%: -v -f,,%runopt% Return