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!

automated Defragmentation 1

Status
Not open for further replies.

gurner

Technical User
Feb 13, 2002
522
0
0
US
I have quite a few tasks automated off the command line using Scheduled tasks but haven't yet figured out how to automate Defragmentation

any tips?
 
Yeah good point. There isn't a .exe app anymore as its in a MMC Snap-In. Anybody know? Steve Hewitt
Systems Manager
 
The defrag in 200/xp is actually a cut-down version of a product called Diskeeper, made by Executive Software. You cannot schedule defrags, or do concurrent drive defrags, but if you purchase the real thing, (download a trial first - it is an excellent piece of software - pagefile defragging, mft, the works) you can use scheduling and concurrent drive defrags.

It also has 'Smart Scheduling' which will asses your drive and see if it needs it or not. If it doesn't it will have another look at it's leisure. It really is spot on.

Sounds like I work for ES!! I don't! Promise.
 
Hi,

There is a script out there that allows you to use the existing defragmention program in Windows and schedule it in 2000. Do know whether this is what you want or not?

If it is, I'll dig up the script for you.

Tony.
 
Unfortunately I can't post this today as I am busy, but I will post on Monday.

Tony
 
Hi,

Here is the vbs:

' This script launches defrag and sends keys to the UI in order to automate the defrag
' process.
'
' Based on code from Windows & .Net Magazine
' Graham Reeds, 2002

' start the script and get access to the shell
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 500


' get the number of mounted drives
set FSysObject = CreateObject("Scripting.FileSystemObject")
set AllDrives = FSysObject.Drives'


FirstTime = TRUE
On Error Resume Next
For Each iDrive In AllDrives
If iDrive.DriveType <> 1 And iDrive.DriveType <> 3 And iDrive.DriveType <> 4 Then
If FirstTime = TRUE Then
' Send a tab key to highlight the current Drive
WshShell.Sendkeys &quot;{TAB}&quot;
WScript.Sleep 500
End If 'FirstTime = TRUE

' if this isn't the first time pick the next drive letter
If FirstTime = FALSE Then
' Send a down key key to highlight the next Drive
WshShell.Sendkeys &quot;{DOWN}&quot;
WScript.Sleep 500
End If 'FirstTime = FALSE
FirstTime = FALSE

' Send an ALT-A key to bring down the defrag menu
WshShell.SendKeys &quot;%A&quot;
WScript.Sleep 200

' Send a D to start the defrag for the drive:
WshShell.SendKeys &quot;D&quot;

' Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate(&quot;Defragmentation Complete&quot;) = FALSE
WScript.sleep 5000
Wend

' Bring the msgbox to the foreground
WshShell.AppActivate(&quot;Defragmentation Complete&quot;)
WScript.Sleep 200

' Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys &quot;{TAB}&quot;
WScript.Sleep 500

' Send key to Close the Defragmentation Complete window
WshShell.Sendkeys &quot;{ENTER}&quot;
WScript.Sleep 500

End If ' iDrive.DriveType
Next 'iDrive


' Send key to Close the Defragmentation Complete window
WshShell.Sendkeys &quot;{ENTER}&quot;
WScript.Sleep 500

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


Just set this to run at a particular time in task scheduler. May need to be adapted to run defrag on multiple drives.

Tony.
 
I have found a Defrag Exe that can run off the command line.

It was on my machine in C:\WINDOWS\SYSTEM32\DLLCACHE

I have checked it and it is the Windows Defrag program (but it has switches for command line use!)

There isn't a copy of it on any other XP machines or 2K servers.

I am going to test it on a test machine, see if it ports over to a test 2K server box
 
Damn, doesn't appear to work on an NT platform

seems fine on XP
 
Does this script thing work whilst the server is not logged on, as it seems to require the actions of the GUI for it's use??

Cheers.
 
Yeah, thats a good point. Can it run whilst not logged on to the server? I would imagine so, only services run when not logged on.

Steve Hewitt
Systems Manager
 
does anyone know whether a product like Srvany can run a VBS script like this as a service for running when not logged on

although as you say the GUI is needed for the sendkeys move so you probably need to be logged on

Also as i found over the weekend, this script works really well!

until as i found if it comes across a drive with insufficent space (i modified the script to defrag 8 drives but the last one only has 300Mb left) it was asked a different question and stopped!
 
Seems to defrag fine during Client/Server activity

seems to get easily interupted if you start working in the same session though.

i think (haven't tested it yet) that terminal services should be fine, as this script is GUI based SendKeys
 
Wow,

Loads of interest! Can I officialy make a slight change in the title of the thread:

&quot;Automated Defragmentation whilst logged off.&quot;

I mean, who leaves a server logged on - even more so overnight or when your not there! Anyone know?

If theres any M$ employees out there reading this take note! We need either a CMD defrag tool (like XP) or a way to schedule them!

Steve Hewitt
Systems Manager
 
Thanks to Tonykblen for the great script. It works like a charm on 2 Win2K Pro workstations, but on my 3rd Win2K Pro machine, it hangs. I added some code to write to a log to see where it's hanging and its on this code (below). It just loops, meaning it hasn't detected that the app. is active. I scheduled it for 3:00 a.m., with a Task Scheduler shutdown after 5 hours if still running, and the scheduler had to force a shutdown since it was still looping on the AppActivate test. The defrag mmc seems to have launched (I can see it in the Task Manager), but the GUI is not visible on the screen.

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

Any ideas as to why it can't "activate" the defrag. app.?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top