Jump to content

Automate System Clean-up?


Recommended Posts

Hi all,

So I just started looking into autoit recently and was trying to figure out how useful it might be. During that time I manually ran my routine system clean up procedure. Too bad I actually forget to complete a few steps in the list. I almost always do that (Human error). So thats the "Ah-Hah!" moment. Why not script the entire system cleanup procedure with autoit? A 'fire and forget' script that I can run 'on-demand' monthly. Right, start the script and go cut grass cuz its going to take at least an hour to get it all done. Heh, and no way I'll miss anything either!!!

Plus, Autoit makes it easy to control shutdown. For grass cutting, I'd want the machine to power down. Other times, I'd want the machine to slip into stand-by. And still others, I'd just want a msgbox telling me the crap was done so click OK. No doubt you all know the routine: Clean and defrag registry, delete junk files, defrag + optimize hard drives, wipe MFT and free space, Fix any HD errors and so on. No doubt you all have a crap load of tools to accomplish this. Thing is that a lot of this requires a *reboot*. Some of which would be self-imposed of course.

My current idea so far is to use Autoit to create a shortcut in the startup directory with the shortcut description text being used for job control. Relevant lines look like this:

$sFilePath = @StartupDir & "\SysCleanScript.lnk" 
$sScriptPath = @ScriptFullPath 
FileCreateShortcut($sScriptPath, $sFilePath, "", "", "03") 
;Job 2 processed, re-start script and continue to process job 3
This scheme would be used to handle starting the autoit script after multiple reboots and dealing with what job is next in the list after said reboot.

So I guess my question (Probably has little to do with autoit) is --> Is there a more elegant, faster, easier, and bullet proof way to deal with starting an autoit script after multi reboots? Registry maybe? Oh, and I do have a crude GUI in place as well. In fact, half of my software tools are GUI only and controlled by autoit.

Any help or suggestions greatly appreciated. Thanks

Edited by Melba23
Added some paragraphs
Link to comment
Share on other sites

  • Moderators

Skin4112,

Welcome to the AutoIt forums. :)

Now your wall-of-text is split into paragraphs it might attract a few more readers. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba23,

Awesome save. Ya know, when I looked at my last post I thought "Damn, I really did do a good job even though my text looked like a wall." Then I noticed the code block. I didn't create a pretty code block at all.

I can only guess and say my browser security/privacy methods were to blame for the wall-of-text. Maybe this time it'll be different?

Thanks again M23.

Link to comment
Share on other sites

I read it again and I think you want the script to run but only do certain tasks until the computer reboots. I would try a counter.

*Assume that the computer starts the script on boot. You can store the number of boots in an ini file and add 1 to it and save the ini file. This assumes that you will start the script when windows starts and wont close and open it again until the system reboots. The ini file can look like:

[Number of Boots]
1

At the beginning of your script:

Global $NumReboots
    
; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = @ScriptDir & "\NumberOfBoots.txt"

    ; Create a temporary file to write data to.
    If Not FileCreate($sFilePath, "[Number of Boots]" & @CRLF & "1" & @CRLF) Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred - The numberofboots.txt was not found. A new one was created and set to 1")

    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf



    ; Write data to the file using the handle returned by FileOpen.
$NumReboots = FileReadLine($hFileOpen, 1)
$NumReboots = $NumReboots +1
    FileWrite($hFileOpen, $numReboots)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)



If $NumReboots = 1 then
     do something 
ElseIf $NumBoots = 2 then    
     do something 
ElseIf $NumBoots = 3 then
    do something
Else     do something

I haven't tried the code but you get the point.

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Link to comment
Share on other sites

  • Moderators

Here is a script that I used way back when for XP. It is terribly outdated, but will give you an idea of one approach I used for multiple reboot scenarios. In short, you can call a different function in the script based on the command line parameter passed to the executable. As you go through each stage, you write out the command line parameter you want called next to the registry.

#AutoIt3Wrapper_OutFile=C:\Users\Hades\Desktop\Disk Maint.exe
#AutoIt3Wrapper_Res_Comment=Windows XP Disk Maintenance
#AutoIt3Wrapper_Res_Description=Disk Maintenance for Windows XP SP3
#AutoIt3Wrapper_Res_Fileversion=1.30
#AutoIt3Wrapper_Res_ProductVersion=1.30
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Icon=C:\Users\Hades\Documents\Icons\MyIcon.ico
#AutoIt3Wrapper_Compression=4

#NoTrayIcon
#include <File.au3>
#include <Array.au3>
#include <EventLog.au3>
#include <ie.au3>


    Select

        Case $CmdLine[0] = 0
            _updates()
        Case $CmdLine[1] = "/?"
            _help()
        Case $CmdLine[1] = "/r1"
            _r1()
        Case $CmdLine[1] = "/f1"
            _f1()
        Case $CmdLine[1] = "/r2"
            _r2()
        Case $CmdLine[1] = "/f2"
            _f2()

    EndSelect

Func _updates()


$ie = "http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us"

    ShellExecuteWait("sc.exe", 'config "bits" start= auto', "", "", @SW_HIDE)
    ShellExecuteWait("sc.exe", 'config "wuauserv" start= auto', "", "", @SW_HIDE)
    RunWait('net start "bits"', "", @SW_HIDE)
    RunWait('net start "wuauserv"', "", @SW_HIDE)

    $wsus = _IECreate($ie)
    WinSetState("Microsoft Update", "", @SW_MAXIMIZE)

While Winexists("Microsoft Update - ")

   Sleep(10000)

   Select
       Case WinExists("Installing Updates", "Some updates were not installed")
          _failure()
       Case WinExists("Installing Updates", "Installation complete")
          _updateReboot()
      Case Not WinExists("Microsoft Update - ")
          _r2()
    EndSelect

WEnd

EndFunc

Func _updates2()

$ie = "http://www.update.microsoft.com/microsoftupdate/v6/default.aspx?ln=en-us"

    $wsus = _IECreate($ie)
    WinSetState("Microsoft Update", "", @SW_MAXIMIZE)

While Winexists("Microsoft Update - ")

   Sleep(10000)

   Select
       Case WinExists("Installing Updates", "Some updates were not installed")
          _failure2()
       Case WinExists("Installing Updates", "Installation complete")
          _updateReboot2()
      Case Not WinExists("Microsoft Update - ")
          _r2()
    EndSelect

WEnd

EndFunc

Func _cleanup()

    ShellExecuteWait("CleanMgr.exe", "/sagerun:1", "", "", @SW_HIDE)

    While ProcessExists("CleanMgr.exe")
        Sleep(30000)
    WEnd

    ShellExecuteWait("C:\Windows\System32\defrag.exe", "c: -f", "", "", @SW_HIDE)

EndFunc

Func _clearEvents()

Local $logs[6]

$logs[0] = _EventLog__Open("", "Application")
$logs[1] = _EventLog__Open("", "Security")
$logs[2] = _EventLog__Open("", "System")

    For $element In $logs
        _EventLog__Clear($element, "")
        _EventLog__Close($element)
    Next

EndFunc

Func _deleteTemp()

$_dir1 = _FileListToArray(@TempDir, "*", 2)
$_dir2 = _FileListToArray(@TempDir, "*", 1)
$_dir3 = _FileListToArray("C:\Windows", "*", 2)

    If IsArray($_dir1) Then
        For $n = 1 To $_dir1[0]
            $sPath = @TempDir & "\" & $_dir1[$n]
            If FileExists($sPath & "\") Then
                DirRemove($sPath, 1)
            EndIf
        Next
    EndIf

    If IsArray($_dir2) Then
        For $n = 1 To $_dir2[0]
            $sPath = "C:\Temp\" & $_dir2[$n]
            If FileExists($sPath) Then
                FileDelete($sPath)
            EndIf
        Next
    EndIf

    If IsArray($_dir3) Then
        For $n = 1 To $_dir3[0]
            $sPath = "C:\Windows\" & $_dir3[$n]
                If StringInStr($sPath, "$NtUninstall") Then
                    DirRemove($sPath, 1)
                EndIf
        Next
    EndIf

EndFunc

Func _runMRU()

    $var = "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU"

    Local $val[27] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "MRUList"]

    For $element in $val
        RegDelete($var, $element)
    Next

EndFunc

Func _finish()

    MsgBox(0, "Disk Maintenance Complete", "Disk Maintenance for " & @ComputerName & " is complete.")

EndFunc

Func _failure()

WinClose("Installing Updates")
WinClose("Microsoft Update - ")

RegWrite("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /f1')
MsgBox(0, "Not all Updates Completed", "Some updates cannot be installed until after a restart. Click OK to reboot and run Microsoft Updates again.", 30)

Shutdown(6)
Exit

EndFunc

Func _failure2()

WinClose("Installing Updates")
WinClose("Microsoft Update - ")

RegWrite("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /f2')
RunWait('net stop "wuauserv"', "", @SW_HIDE)
ShellExecuteWait("sc.exe", 'config "wuauserv" start= disabled', "", "", @SW_HIDE)

MsgBox(0, "Not all Updates Completed", "Some updates could not be installed." & @CRLF & @CRLF & "Click OK to reboot and then continue Disk Maintenance.", 30)

Shutdown(6)
Exit

EndFunc

Func _updateReboot()

WinClose("Installing Updates")
WinClose("Microsoft Update - ")

RegWrite("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /r1')
MsgBox(0, "Reboot to complete installation", "Windows needs to perform a restart to complete the update installation." & @CRLF & @CRLF & "Cick OK to reboot and continue Disk Maintenance.", 30)

Shutdown(6)
Exit

EndFunc

Func _updateReboot2()

WinClose("Installing Updates")
WinClose("Microsoft Update - ")


RegWrite("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2", "REG_SZ", '"' & @ScriptFullPath & '" /r2')
RunWait('net stop "wuauserv"', "", @SW_HIDE)
ShellExecuteWait("sc.exe", 'config "wuauserv" start= disabled', "", "", @SW_HIDE)

MsgBox(0, "Reboot to complete installation", "Windows needs to perform a restart to complete the update installation." & @CRLF & @CRLF & "Cick OK to reboot and continue Disk Maintenance.", 30)

Shutdown(6)
Exit

EndFunc

Func _r1()

    RegDelete("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2")
    _updates2()

EndFunc

Func _r2()

    RegDelete("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2")
    _cleanup()
    _clearEvents()
    _deleteTemp()
    _runMRU()
    _finish()

EndFunc

Func _f1()

    RegDelete("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2")
    _updates2()

EndFunc

Func _f2()

    RegDelete("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run", "R2")
    _cleanup()
    _clearEvents()
    _deleteTemp()
    _runMRU()
    _finish()

EndFunc

Func _help()
    MsgBox(0, "Disk Maintenance", "Windows XP Disk Maintenance" & @CRLF & @CRLF & "© 2011, My Company" & @CRLF & @CRLF & "Info@MyCompany.com")
EndFunc

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

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...