+ Post New Thread
Results 1 to 2 of 2
Scripts Thread, Script to Defrag all drives on log on in Coding and Web Development; Done this script so that I can just log in a machine and it defrags on log in. Ideal to ...
  1. #1

    nephilim's Avatar
    Join Date
    Nov 2008
    Location
    Dunstable
    Posts
    11,904
    Thank Post
    1,626
    Thanked 1,892 Times in 1,406 Posts
    Blog Entries
    2
    Rep Power
    428

    Script to Defrag all drives on log on

    Done this script so that I can just log in a machine and it defrags on log in. Ideal to set at the end of a day, now just need to work out how to add a when complete shut down PC command.

    Code:
    'defrag_all.vbs
    'Defrags all hard disks - Can be run as a Scheduled Task
    'It will create an error log and display it
    ' Carl Taylor 04/10/2010
    
    Option Explicit
    
    Dim WshShell, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive
    
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set fso = CreateObject("Scripting.FileSystemObject")
    X = 0
    
       Set dc = fso.Drives
    For Each d in DC 
    	If d.DriveType = 2 Then
    	   X = X + 1
    
    'Determine drive letter of first fixed disk
    'This is the drive that the error report will be placed on
    		If X = 1 Then
    		   Drive = d
    		End If
    	End If
    Next
    
    ReDim A(X)
    ReDim ErrStr(X)
    
    X = 0
    For Each d in dc
          If d.DriveType = 2 Then
          X = X + 1
          Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)
    
    'Determine the Error code returned by Defrag for the current drive and save it
    If return = 0 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag completed successfully" & vbCRLF
    elseif return = 1 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (defrag was cancelled manually) " & vbCRLF
    elseif return = 2 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (there was a command line error. Check your command line for valid switches and drives)" & vbCRLF
    elseif return = 3 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (there was an unknown error)" & vbCRLF
    elseif return = 4 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (defrag could not run due to insufficient memory resources)" & vbCRLF
      'errorlevel 5 is not currently used
    elseif return = 5 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (general error)" & vbCRLF
    elseif return = 6 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & "(System error: either the account used to run defrag is not an administrator, there is a problem loading the resource DLL, or a defrag engine could not be accessed. Check for proper user permissions and run Sfc.exe to validate system files)" & vbCRLF
    elseif return = 7 then
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (There is not enough free space on the drive. Defrag needs 15% free space to run on a volume)" & vbCRLF
    else
      ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with an unknown error level: " & return & vbCRLF
    end if
    
           End If
       Next
    
    'Create the Error Report in the root of the first fixed disk.
    Set MyFile = fso.OpenTextFile(Drive & "\defragreport.txt", 2, True)
    MyFile.WriteLine(Date) & vbCRLF
    MyFile.WriteLine(Time) & vbCRLF
       For I = 1 to X
          MyFile.WriteLine(ErrStr(I))
       Next
       MyFile.Close
    
    Return = WshShell.Run(Drive & "\defragreport.txt",3,True)
    
    Set WshShell = Nothing
    Set fso = Nothing
    This works well on our XP and W7 test machines, so looking to implement it across my network
    Last edited by nephilim; 5th October 2010 at 11:11 AM.

  2. #2
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    429
    Thank Post
    68
    Thanked 90 Times in 75 Posts
    Rep Power
    61
    Have you looked at MyDefrag v4.3.1

    It seems really good, and I have it on all my home machines for testing.

    BoX

SHARE:
+ Post New Thread

Similar Threads

  1. Script to delete profile at log off?
    By woody in forum Windows
    Replies: 74
    Last Post: 15th February 2013, 02:28 PM
  2. Replies: 23
    Last Post: 12th October 2010, 02:59 PM
  3. HDD Defrag: Puran Defrag
    By DaveP in forum Downloads
    Replies: 0
    Last Post: 29th January 2010, 02:45 PM
  4. Name mapped drives via script
    By Bluetooth in forum Scripts
    Replies: 5
    Last Post: 20th November 2009, 09:15 PM
  5. Defrag script for more than 1 pc on a network
    By lafleur1977 in forum Scripts
    Replies: 5
    Last Post: 12th June 2009, 05:52 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •