How to schedule a Disk Cleanup and defrag in XP via VBScript

Author Message
KenPark

  • Total Posts : 23
  • Scores: 0
  • Reward points : 0
  • Joined: 9/28/2008
  • Status: offline
How to schedule a Disk Cleanup and defrag in XP via VBScript Tuesday, November 25, 2008 2:38 AM (permalink)
0
'PRECONDITION
 'Create log files under e.g. c:\cleanup.log and c:\defrag.log
 '*************************************************
 'Script Name:Script Cleanup.vbs
 'Author: Kenneth Værsland
 'Created: 25/11/2008
 'Description:This script schedules the execution of the Cleanup utility
 'at 20:00 on the first day of each month. This utility removes unnecessary
 'files from the local disk drive.
 '*************************************************
 'Initialization section
 Option Explicit
 Dim WshShl, FsoObject, LogFile
 Set WshShl = WScript.createObject("WScript.Shell")
 Set WshShl = WScript.CreateObject("Scripting.fileSystemObject")
 'Main Processing Section
 CheckForCleanupLog()
 PerformCleanup()
 WScript.Quit()
 
 'Procedure Section
 'This procedure enures that a cleanup.log file exists
 Sub CheckForCleanupLog()
     If (FsoObject.FileExists ("C:\cleanup.log")) Then
         Set LogFile = FsoObject.openTextFile("C:\Cleanup.log", 8)
         LogFile.WriteLine "Cleanup process started on" & Date & " at " & Time
     Else
         Set LogFile = FsoObject.OpenTextFile("C:\cleanup.log", 2 ,"True")
         LogFile.WriteLine "Cleanup process started on" & Date & " at " & Time
     End If
 End Sub
 'This procedure execute the Cleanup utility
 Sub PerformCleanup()
     WshShl.Run "C:\windows\System32\cleanmgr /sagerun:1"
 End Sub 

'*********************************************************************
 '*************************************************
 'Script Name:Script defrag.vbs
 'Author: Kenneth Værsland
 'Created: 25/11/2008
 'Description:This script schedules the execution of the Windows defrag.exe
 'at 22:00 on the first day of each month. This Utility defrags disk drivers
 '*************************************************
 'Initialization section
 Dim WshShl, FsoObject, LogFile
 Set WshShl = Wscript.CreateObject ("WScript.Shell")
 Set FsoObject = Wscript.CreateObject("Scripting.FileSystemObject")
 'Main Processing Section
 CheckForDefragLog()
 PerformDefrag()
 Wscript.Quit()
 
 'Procedure Section
 'This procedure enures that a defrag.log exists
 Sub CheckForDefragLog()
     if (FsoObject.FileExists("c:\defrag.log")) Then
         Set LogFile = FsoObject.OpenTextFile("c:\defrag.log", 8)
         Logfile.Writeline "Defrag.exe Started on" & Date & " at " & time
     Else
         Set LogFile = FsoObject.OpenTextfile("c:\defrag.log", 2,"True")
         LogFile.Writeline "Defrag.exe Started on" & DATE & " at " & Time
     END IF
 end sub
 'This procedure executes the defrag.exe cmd line utility
 Sub PerformDefrag()
     WshShl.Run "%SystemRoot%\System32\defrag.exe c: /f"
 End Sub
 

'********************************************************************
 'WORKING WITH THE SCHEDULED TASK WIZARD
 '1.)Click start /all programs/accessories/system tools and then scheduled Task Wizard. The scheduled Tasks folder opens
 '2.)Double -click on the add scheduled task icon. The scheduled task wizard appears. Click Next
 '3.)A list of Windows apps is displayed. Click browse , locate your script, and click on Open.
 '4.)Type a descriptive name ofr the scheduled task, select a time frame for its execution and click next.
 '5.) Specify addtional execution time frame options, and click next.
 '6.) Next, you will be prompted to specify an optional user name and password. Click next
 '
 '
 '
 '
 '*************************************************
 'Script Name:Schedule.vbs
 'Author: Kenneth Værsland
 'Created: 25/11/2008
 'Description:This script schedules the execution of the Windows
 'Cleanup.exe utility
 '*************************************************
 'Initialization section
 Option Explicit
 Dim WshShl
 'Instantiate the WshShell object
 Set WshShl = Wscript.CreateObject("WScript.Shell")
 'Main Processing Section
 ScheduleScripts()
 WScript.Quit()
 'Procedure Section
 'This procedure schules the execution of other VBScripts
 Sub ScheduleScript()
     'Use the WshShell object's RUN() method to run the at cmd
     WshShl.Run "at 20:00 /every:1 c:\cleanup.vbs"
     WshShl.Run "at 22:00 /every:1 c:\defrag.vbs"
 End Sub  
 


EDIT: EBGREEN - Indentation and code tags are your friends.
<message edited by ebgreen on Tuesday, November 25, 2008 3:53 AM>
 
#1
    ebgreen

    • Total Posts : 8227
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    RE: How to schedule a Disk Cleanup and defrag in XP via VBScript Tuesday, November 25, 2008 3:53 AM (permalink)
    0
    Thanks for sharing. I have a couple of comments. First, there is no reason to explicitly use WScript.Quit() where you do since your aren't providing any exit code. Second, the reliance on global variables makes your Subs less portable than they would be otherwise.
    <message edited by ebgreen on Tuesday, November 25, 2008 4:10 AM>
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      DiGiTAL.SkReAM

      • Total Posts : 1259
      • Scores: 7
      • Reward points : 0
      • Joined: 9/7/2005
      • Location: Clearwater, FL, USA
      • Status: offline
      RE: How to schedule a Disk Cleanup and defrag in XP via VBScript Tuesday, November 25, 2008 5:38 AM (permalink)
      0
      Also, you first have to set the cleanmgr parameters(/sageset:1), before you can tell it to run using those parameters(/sagerun:1).
      First do a cleanmgr /sageset:1
      Select which options you want to run
      THEN you can do cleanmgr /sagerun:1
       
      "Would you like to touch my monkey?" - Dieter (Mike Meyers)

      "It is better to die like a tiger, than to live like a pussy."
      -Master Wong, from Balls of Fury
       
      #3
        CopFr001

        • Total Posts : 1
        • Scores: 0
        • Reward points : 0
        • Joined: 6/30/2009
        • Status: offline
        RE: How to schedule a Disk Cleanup and defrag in XP via VBScript Tuesday, June 30, 2009 10:15 AM (permalink)
        0
        DiGiTAL.SkReAM, based on your comments what should the code look like, just for a disk clean up?
         
        #4
          DiGiTAL.SkReAM

          • Total Posts : 1259
          • Scores: 7
          • Reward points : 0
          • Joined: 9/7/2005
          • Location: Clearwater, FL, USA
          • Status: offline
          RE: How to schedule a Disk Cleanup and defrag in XP via VBScript Friday, August 28, 2009 3:10 AM (permalink)
          0
          What I would do would be to manually run the cleanmgr /sageset:1 command from the Start->Run prompt.  Then, manually set the options I want.
          After that, the cleanmgr /sagerun:1 command would run the cleanmgr utility using the options you had previously selected.
           
          I've tried hacking the registry to set these options, and either it was beyond my feeble skills or it was just not possible - not sure whcih, it was a while ago. :)
          "Would you like to touch my monkey?" - Dieter (Mike Meyers)

          "It is better to die like a tiger, than to live like a pussy."
          -Master Wong, from Balls of Fury
           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9