AutoDefrag


defrag

I thought this would be an easy task

– Automate disk defragmenting for non-admin users –

However, it took me about 4 hours to get it working (partly due to my beginner WSH/VB scripting skills)

It needed  to be run by a normal user on Windows 2000 and XP machines and not require any additional software to be installed . So, this is how I did it,

  1. Create a folder on the c: drive called "Scripts"
  2. create a new file in the folder called "RunAs.vbs".
  3. Right-click the file and choose edit to open it in notepad.
  4. Paste this code
    Option explicit
    Dim oShell
    set oShell= Wscript.CreateObject("WScript.Shell")
    oShell.Run "runas /noprofile /user:administrator" & Chr(34) _
       & "cscript.exe c:ScriptsAutoDefrag.vbs" & Chr(34)
    WScript.Sleep 100
    oShell.Sendkeys "password"
    Wscript.Sleep 100
    oShell.SendKeys "~"
    Wscript.Quit
  5. Save and close the file
  6. Now create another file in the same way called "AutoDefrag.vbs" and paste this code from http://windowsitpro.com/Articles/Index.cfm?ArticleID=8276 
    ‘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
  7. Save and close the file.
  8. That’s it! Just double-click the RunAs.vbs file and it should run for any user
  9. This script can also be run from a network share, simply substitute the "Administrator" for some user with domain admin rights (e.g. "DomainSuperUser") and change the script path to a UNC name (e.g. \servershareAutodefrag.vbs")

Sources of information

Advertisements

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s