Defragment a disk

The script below calls the Windows utility Defragmenter under XP. For a reliable call, whereever the Defrag icon is, we prefer to call it through its msc extension (dfrg.msc). Under Vista or Windows 7, the defragmentation is done automatically by Windows so you don't need to use anymore this kind of script.

 

'PUT HERE DISK LETTER TO DEFRAGMENT

disk$="d:"

 
'dfrg.msc is in sub-directory system32 of Windows directory.
'windir$ function returns Windows directory

rep_msc$=windir$()+"\system32"

nom_msc$="dfrg.msc"

 

'The exe to call by Shell function is mmc and we add the parameters

exe$="mmc "+rep_msc$+"\"+nom_msc$+" "+disk$

 

shell(exe$)

 

 

'choice defrag

UseWindow("MMC.EXE|ATL:|Disk Defragmenter",1)

Click(Button,"Defragment")

 

 

'Specify here the maximum time for defrag

#PauseTimeout=7200

'wait until defrag is finished

Pause until

WinStatus(Active, Exact)

InWindow("MMC.EXE|#32770|Defragmentation Complete")

PauseFalse

MsgBox("'Wait for' at line " + #ErrorLine$ + " has failed !",16,"Runtime error")

End

EndPause

 

'close Defragmentation complete window

 

UseWindow("MMC.EXE|#32770|Defragmentation Complete",1)

Click(Button,"Close")

pause 1

 

 

'close application

CloseWindow("MMC.EXE|MMCMainFrame|Disk Defragmenter",1)