- Apr 6, 2002
- 7,141
- 1
- 0
I downloaded the below script to defrag my drives. It's not the greatest since it works using SendKeys, but it does work.
The problem with checking drives for errors though is that Windows needs exclusive access to the drive, which means you need to reboot. So what's a good way to schedule this to occur? Would I be better off using some third-party software? If so, what?
=============================================================
'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
'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
'Tab to Close button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
'Close msgbox
WshShell.Sendkeys "{ENTER}"
WScript.Sleep 500
'-------------------------------------
'Defrag D drive
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
WshShell.Sendkeys "D"
WScript.Sleep 500
'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
'Tab to Close button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
WshShell.Sendkeys "{ENTER}"
WScript.Sleep 500
'-------------------------------------
'-------------------------------------
'Defrag H drive
'WshShell.Sendkeys "{TAB}"
'WScript.Sleep 500
WshShell.Sendkeys "B" 'because the label on the drive is Backup
WScript.Sleep 500
'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
'Tab to Close button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
WshShell.Sendkeys "{ENTER}"
WScript.Sleep 500
'-------------------------------------
'Send and ALT-F4 to Close the Defrag program
'WshShell.Sendkeys "%{F4}"
The problem with checking drives for errors though is that Windows needs exclusive access to the drive, which means you need to reboot. So what's a good way to schedule this to occur? Would I be better off using some third-party software? If so, what?
=============================================================
'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
'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
'Tab to Close button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
'Close msgbox
WshShell.Sendkeys "{ENTER}"
WScript.Sleep 500
'-------------------------------------
'Defrag D drive
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
WshShell.Sendkeys "D"
WScript.Sleep 500
'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
'Tab to Close button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
WshShell.Sendkeys "{ENTER}"
WScript.Sleep 500
'-------------------------------------
'-------------------------------------
'Defrag H drive
'WshShell.Sendkeys "{TAB}"
'WScript.Sleep 500
WshShell.Sendkeys "B" 'because the label on the drive is Backup
WScript.Sleep 500
'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
'Tab to Close button
WshShell.Sendkeys "{TAB}"
WScript.Sleep 500
WshShell.Sendkeys "{ENTER}"
WScript.Sleep 500
'-------------------------------------
'Send and ALT-F4 to Close the Defrag program
'WshShell.Sendkeys "%{F4}"