News:

Registration disabled.  This forum is in maintenance mode (and has been for a few years now), so I've disabled registration.  If you already have an account, great, keep using it.  If not, unfortunately I won't be accepting any new registrations.  At some point in the near future this forum will be shutting down forever.

Main Menu

Let Windows DEFRAG be a Scheduled Task!

Started by toy4two, October 05, 2006, 05:47:12 PM

Previous topic - Next topic

toy4two

Found this cool script that works to allow you to defrag on a schedule (now you won't have to buy Disk Keeper!):

Just copy and paste the code below into a text file and give it the .vbs file extension, then use Scheduled Tasks to run it.

Cut here
------------------

'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("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend



'modifications Alfonso
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 "Disk Defragmenter"
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("Defragmentation Complete") = FALSE
wscript.sleep 5000
wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Move down to next drive
if firstjump=0 then
WshShell.Sendkeys "{TAB}{DOWN}"
firstjump=1
else
WshShell.SendKeys"{DOWN}"
end if

end if
next

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"