Jump to content

Automate 2003 Disk Defrag (VBS)


PriTyan

Recommended Posts

I have a VBScript that I have been running for a while on my 2000 Servers to automate Disk Defrag. This works great on 2000 Servers. See code below

'Automate Defrag for Windows 2000

Dim Window, sysd, cmd

set Window = CreateObject("WScript.Shell")

sysd = Window.ExpandEnvironmentStrings("%defdrive%")
cmd = "dfrg.msc %" & sysd

Window.Run cmd

WScript.Sleep 1000

While Window.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

Window.AppActivate "Disk Defragmenter"
WScript.Sleep 1000

Window.SendKeys "%A"
WScript.Sleep 250

Window.SendKeys "D"
WScript.Sleep 250

While Window.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5000
Wend

Window.AppActivate "Defragmentation Complete"
WScript.Sleep 250

Window.Sendkeys "{TAB}"
WScript.Sleep 250

Window.Sendkeys "{ENTER}"
WScript.Sleep 250

Window.Sendkeys "%{F4}"
WScript.Sleep 250

WScript.Quit

Here is my problem on 2003 Servers. The below code waits for the pop-up window titled "Defragmentation Complete" to identify that the defrag has completed, then the rest of the code procedes to send keys to close out of defrag.

While Window.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5000
Wend

The problem with 2003 defrag is that the title to the pop-up window that indicates Defrag has completes is the same as that Defrag app "Disk Defragmenter". So my problem is how do I identify the new pop-up window since it has the same name as the original window? See image below.

defrag.jpg

I've contiplated just putting a long Sleep command, but would like my code to be a little cleaner. :)

Any help would be appreciated.

P

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...