General discussion


Command line defrag of Win2K server?

By peter.berger@genexsrvcs ·
I would like to schedule automatic/repeated hard drive defragments during Sunday mornings. In XP this is easy to do with defrag.exe. Unfortunatly I cannot seem to do so in Win2K Server. The command dfrgntfs.exe doesn't run from the command line and dfrg.msc is GUI-based. I tried copying XP's defrag.exe to Win2K but when I run it I get DLL errors.

Does anyone know of a free method to cron/schedule drive defrags from a command line/batch script?

Any help/411 would be great. Thanks in advance.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by azizhakim In reply to Command line defrag of Wi ...

I did glue a few pieces of this vbs script and tested it on one machine.It quickly exited cause the machine did not need defrag.I have put it the piece where it scans each drive and analyses and defrag if needed.It will work regardless.if you send me mail I will send you zip file or you can copy the text,save and run.Watch for line breaks and I hope this will help.Below is the script text

StrComputer = "."
Set objWmiService = Getobject("Winmgmts:\\" & Strcomputer & "\root\cimv2")
Set Coldisks = ObjWMIService.Execquery("SELECT * From Win32_logicalDisk WHERE DriveType=3")
For each objDisk In Coldisks
'This script launches defrag and sends keys to the UI in order to automate the defrag

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

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

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

'Sleep for 5 seconds
wscript.sleep 5000

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

'Send a s to stop the defrag
WshShell.SendKeys "D"
WScript.Sleep 3000

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

Collapse -

A -- I tried your script (pretty slick that you can do that) but it seemed to defrag my C drive twice but never did any of the other partitions. I don't know programming (I'm a hardware guy). Thanks for the 1st attempt though. If you figure it out then it would be greatly appreciated. Thanks.

Related Discussions

Related Forums