bfairchild
05-21-2003, 08:11 PM
here is a script that will allow you to schedule defragmenting a PC running win2k or winXP. these are two seperate scripts and only works with the appropriate OS. Just cut & paste to your fav txt editor and give a .vbs extension. Then you can setup in task scheduler.
Win2k only
------------
' VBScript source code
'
' Defrag Windows 2000 Script
'
'
' Schedule this script to automate defragmenting.
'
'
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
'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 500
'Send keys to move to top of drive/partition table.
WshShell.SendKeys "{TAB}{HOME}"
WScript.Sleep 501
'Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 502
'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("Defragmenter Complete") = FALSE
wscript.sleep 5001
Wend
'Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmenter Complete"
WScript.Sleep 503
'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 504
'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 505
'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}"
WinXP only
-------------
' VBScript source code
'
' Defrag Windows XP Script
'
'
' Schedule this script to automate defragmenting.
'
'
'
set WshShell = CreateObject("WScript.Shell")
'Launch Defrag from the command line and wait for 2 seconds
WshShell.Run "compmgmt.msc"
WScript.Sleep 2000
'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Computer Management") = FALSE
wscript.sleep 1000
Wend
'Bring the application to the foreground
WshShell.AppActivate "Computer Management"
WScript.Sleep 500
'Send keys to move to Disk Defragmenter. Increase 1000 to 2000 if necessary (1 sec. to 2 sec.)
WshShell.SendKeys "D"
WScript.Sleep 1000
WshShell.SendKeys "D"
WScript.Sleep 1000
'Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 502
'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("Disk Defragmenter") = FALSE
wscript.sleep 501
Wend
'Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 503
'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "C"
Wscript.Sleep 505
'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}"
Enjoy
Win2k only
------------
' VBScript source code
'
' Defrag Windows 2000 Script
'
'
' Schedule this script to automate defragmenting.
'
'
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
'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 500
'Send keys to move to top of drive/partition table.
WshShell.SendKeys "{TAB}{HOME}"
WScript.Sleep 501
'Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 502
'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("Defragmenter Complete") = FALSE
wscript.sleep 5001
Wend
'Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Defragmenter Complete"
WScript.Sleep 503
'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 504
'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 505
'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}"
WinXP only
-------------
' VBScript source code
'
' Defrag Windows XP Script
'
'
' Schedule this script to automate defragmenting.
'
'
'
set WshShell = CreateObject("WScript.Shell")
'Launch Defrag from the command line and wait for 2 seconds
WshShell.Run "compmgmt.msc"
WScript.Sleep 2000
'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Computer Management") = FALSE
wscript.sleep 1000
Wend
'Bring the application to the foreground
WshShell.AppActivate "Computer Management"
WScript.Sleep 500
'Send keys to move to Disk Defragmenter. Increase 1000 to 2000 if necessary (1 sec. to 2 sec.)
WshShell.SendKeys "D"
WScript.Sleep 1000
WshShell.SendKeys "D"
WScript.Sleep 1000
'Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys "%A"
WScript.Sleep 502
'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("Disk Defragmenter") = FALSE
wscript.sleep 501
Wend
'Bring the Defragmentation Complete window to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 503
'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "C"
Wscript.Sleep 505
'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}"
Enjoy