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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unattended Defrag

Status
Not open for further replies.

BenJMF

IS-IT--Management
Jan 30, 2006
53
US
I found another post that provided the script below to run an unattended defrag. It works fine until it reaches section in red. The problem is the script is looking for a window that says "Defragmentation Complete" but the window that comes up at completion is called "Disk Defragmentor", same as the original window where you can choose Analyze, Defragment, etc. Is there anyway to get the script to choose the correct window if they have the same name?

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 defrag 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}"

 
What OS are you running? On WinXP you can initiate a defrag from the command line. Type the following to see the switches you can use:

defrag /?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I'm running XP Pro and I've tried that but even using the -v switch it doesn't seem to actually do the defrag. When I defrag using the GUI it takes about 45 minutes. When I use the method you describe it takes less than 5 so can it really be defragging?
 
I tried this and it seem to go work for me.
defrag c: -f -v

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top