Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need a script to run defrag in Wi 1

Status
Not open for further replies.

ShadowFox333

Programmer
Oct 15, 2002
97
0
0
US
I need a script to run defrag in Windows 2000.
I can get the program to launch but I dont know how
to do the alt A and alt D.
T I A
BOb
 
Try the SendKeys method:
Code:
Set Sh=WScript.CreateObject("WScript.Shell")
Sh.SendKeys "%a" 'for Alt-A
Sh.SendKeys "%d" 'for Alt-D
You may have to use the Sleep method for timing issue:
Code:
WScript.Sleep 1000 'Pause 1 second

Hope This Help
PH.
 
Here is a script that worked very well for me. You may need to make some modifications in case you have multiple drives.


'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 2000

'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

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top