Auto Defrag

5 Dec

'This script launches defrag and sends keys to the UI in order to automate the defrag
'process for 3 partitions, Edited by Compu-Docs to defrag additional drives.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for 2 seconds
WshShell.Run "dfrg.msc"
WScript.Sleep 2000

'Wait until the application has loaded – Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

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

'1 Send keys to move to top of drive/partition table.
WshShell.SendKeys "{TAB}{HOME}"
WScript.Sleep 501

'1 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 502

'1 Send a D to start the defrag
WshShell.SendKeys "D"

'1 Wait until the defrag is completed – Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5001
Wend

'1 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 503

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

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

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

'2 Send key to move to next drive/partition.
WshShell.SendKeys "{DOWN}"
WScript.Sleep 507

'2 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 508

'2 Send a D to start the defrag
WshShell.SendKeys "D"

'2 Wait until the defrag is completed – Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5002
Wend

'2 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 509

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

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

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

'3 Send key to move to next drive/partition.
WshShell.SendKeys "{DOWN}"
WScript.Sleep 513

'3 Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 514

'3 Send a D to start the defrag
WshShell.SendKeys "D"

'3 Wait until the defrag is completed – Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5003
Wend

'3 Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 515

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

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

'End – Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 700

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

Note: Save as .vbs file

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: