'This script launches defrag and sends keys to the UI in
'order to automate the defrag process.
Set WshShell = CreateObject("WScript.Shell")
'Launch Defrag from the command line and wait for a second.
WshShell.Run "dfrg.msc"
WScript.Sleep 1000
'Wait until the application has loaded - Check every second.
While WshShell.AppActivate("Defragmentace disku") = False
WScript.Sleep 1000
Wend
'Modifications by Alfonsom, fixed/updated by Josh West.
Dim oFSO, oDrives, oDrive, firstjump
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrives = oFSO.Drives
firstjump = 0
'We use this variable to check if we have jumped first in
'the drive list.
'It is necessary because the key sequence is a bit
'difference in the first jump.
For Each oDrive In oDrives
If oDrive.DriveType = 2 Then
'Bring the application to the foreground.
WshShell.AppActivate "Defragmentace disku"
WScript.Sleep 200
'Send an ALT-A key to bring down the degrag menu.
WshShell.SendKeys "%A"
WScript.Sleep 200
'Send a D to start the defrag.
WshShell.SendKeys "D"
'Wait until the defrag is completed - Check for window
'every 5 seconds.
While WshShell.AppActivate("Defragmentace byla dokončena") = False
If WshShell.AppActivate("Defragmentace disku") = False Then
Exit For
End If
WScript.Sleep 5000
Wend