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

Is it possible to schedule Defrag to stop at a certain time?

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi,
I'd like to schedule defrag.exe to run on a weekly basis over the weekend, but I'd also like to be sure that it stops before work begins on Monday.

Is there a way to automatically stop defrag if it's still running on Monday?
 
I'm not sure about stopping a defrag... but starting a defrag can be automated if this is Windows Server 2003 that we're talking about.

You can use the "AT" command to call a VBScript which will do this. Let me know if you want the details of this.
 
I don't think that you can schedule it to stop. If you start it though a scheduled task you can tell it to stop after running for X hours however. That may be good enough.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
you can tell it to stop after running for X hours however.
Yeah, that's the option I was thinking of... but how does it stop the defrag? Does it just kill the task, or can it shut down the defrag gracefully?
 
I just tried a test on my XP machine at home by scheduling Media Player to start, and I told it to "Stop the task if it runs for 0 hours & 1 minute".
It started Media Player at the specified time, but after a minute, it didn't close Media Player.

Does the program you're scheduling have to be written to specifically accept messages from Scheduled Tasks that tell it when to stop?
 
I'm not sure if it does or not. I believe that it will just kill it. The media player probably isn't a good example. I think it shells it self out. Try it with notepad and see how that respondes.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
If it just kills the process, that would probably be dangerous to do when defragmenting a hard drive. :-(
I guess I'll have to find a better way to defrag the drives then.
 
I found this VBS script which looks like it might work (albeit in a clumsy sort of way). The only thing I need to do is figure out how to modify it to accept a parameter for the drive to defrag (instead of defragging all the drives) as well as a timer that clicks the Stop button when the time expires... Is anyone here good with VBScript? ;-)
Code:
'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


'Modifications by Alfonsom, fixed/updated by Josh West.
Dim oFSO, oDrives, oDrive, firstjump
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oDrives = oFSO.Drives
firstjump = 0


'We use this variable to check if we have jumped first in
'the drive list.
'It is necessary because the key sequence is a bit
'difference in the first jump.
For Each oDrive In oDrives
    If oDrive.DriveType = 2 Then

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

    '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
        If WshShell.AppActivate("Disk Defragmenter") = False Then
            Exit For
        End If
        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

    'Bring the application to the foreground.
    WshShell.AppActivate "Disk Defragmenter"
    WScript.Sleep 200

    'Move down to next drive
    If firstjump = 0 Then WshShell.SendKeys "{TAB}{DOWN}"
        firstjump = 1
    Else: WshShell.SendKeys "{DOWN}"
    End If
Next

'Send and ALT-F4 to Close the Defrag program.
If WshShell.AppActivate("Disk Defragmenter") = True Then
    WshShell.AppActivate "Disk Defragmenter"
    WshShell.SendKeys "%{F4}"
End If

'Send and ALT-F4 to Close the Defrag program.
If WshShell.AppActivate("Disk Defragmenter") = True Then
    WshShell.AppActivate "Disk Defragmenter"
    WshShell.SendKeys "%{F4}"
End If
 
As a test, if you try running a scheduled task to run the defrag tool you will be able to stop the task, but if you check the task manager I think you will see that it is still running
I concur with a previous post, it is never advisable to stop a defrag process since I find an incomplete defrag fragments your files even worse than before in most cases
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top