Jump to content

Running MS Disk Defragmenter


jwkh
 Share

Recommended Posts

I'm trying to figure out how to automate the MS DF. I have a simple script using ScriptWriter, but I was wondering if using a program's drive pathing, telling it which buttons to select, etc.?

TIA,

jwkh

Link to comment
Share on other sites

I'm trying to figure out how to automate the MS DF. I have a simple script using ScriptWriter, but I was wondering if using a program's drive pathing, telling it which buttons to select, etc.?

TIA,

jwkh

Can you not just use

#include <Process.au3>

$drive = "c:"
_RunDOS("defrag " & $drive)
Link to comment
Share on other sites

I'm trying to figure out how to automate the MS DF. I have a simple script using ScriptWriter, but I was wondering if using a program's drive pathing, telling it which buttons to select, etc.?

TIA,

jwkh

I've already done this, see how you get on with this.

;error log events
Global Const $SUCCESS = 0
Global Const $ERROR =1
Global Const $WARNING =2     
Global Const $INFORMATION =4     
Global Const $AUDIT_SUCCESS =8   
Global Const $AUDIT_FAILURE =16  

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler for event log to prevent crash

Defrag("C")



Func Defrag($DrLeter)

    LogEvent ($INFORMATION, "Started a Defrag on Drive " & $DrLeter) 
    
    If WinExists ("Disk Defragmenter") then Exit
    Run ("mmc Dfrg.msc " & $DrLeter & ":", @SystemDir)
    WinWait ("Disk Defragmenter")
    ControlClick ("Disk Defragmenter","","Defragment")
    WinWait ("Defragmentation Complete")
    ControlClick ("Defragmentation Complete","","Close")
    WinClose ("Disk Defragmenter")
    
    LogEvent ($SUCCESS, "DriveSpace Defrag Finished") 
    
EndFunc

Func LogEvent($type,$text)

$WshShell = ObjCreate("WScript.Shell")
if not @error or $WshShell <> 0 Then
   $WshShell.LogEvent ($type,$text ) 
EndIf
$WshShell = 0

EndFunc

Func MyErrFunc() 
    
   $HexNumber=hex($oMyError.number,8) 
   Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription ) 

   SetError(1); something to check for when this function returns 
Endfunc
Link to comment
Share on other sites

Can you not just use

#include <Process.au3>

$drive = "c:"
_RunDOS("defrag " & $drive)
I'd like to automate the process for N number of drives and use MS's task scheduler to kick it off. The include...I assume you need AutoIt installed on the system or the script compiled.

(I haven't done any scripting (DOS batch files, etc.) in over 15 years. And saying I'm a Newbie to AutoIt is being very kind.)

Thanks for your reply!

jwkh

Link to comment
Share on other sites

I've already done this, see how you get on with this.

;error log events
Global Const $SUCCESS = 0
Global Const $ERROR =1
Global Const $WARNING =2     
Global Const $INFORMATION =4     
Global Const $AUDIT_SUCCESS =8   
Global Const $AUDIT_FAILURE =16  

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc"); Install a custom error handler for event log to prevent crash

Defrag("C")
Func Defrag($DrLeter)

    LogEvent ($INFORMATION, "Started a Defrag on Drive " & $DrLeter) 
    
    If WinExists ("Disk Defragmenter") then Exit
    Run ("mmc Dfrg.msc " & $DrLeter & ":", @SystemDir)
    WinWait ("Disk Defragmenter")
    ControlClick ("Disk Defragmenter","","Defragment")
    WinWait ("Defragmentation Complete")
    ControlClick ("Defragmentation Complete","","Close")
    WinClose ("Disk Defragmenter")
    
    LogEvent ($SUCCESS, "DriveSpace Defrag Finished") 
    
EndFunc

Func LogEvent($type,$text)

$WshShell = ObjCreate("WScript.Shell")
if not @error or $WshShell <> 0 Then
   $WshShell.LogEvent ($type,$text ) 
EndIf
$WshShell = 0

EndFunc

Func MyErrFunc() 
    
   $HexNumber=hex($oMyError.number,8) 
   Msgbox(0,"","We intercepted a COM Error !" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription ) 

   SetError(1); something to check for when this function returns 
Endfunc
Thanks,

This seems to be closer to what I'm looking for.

Again, thanks!

jwkh

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
 Share

  • Recently Browsing   0 members

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