HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Making a script file to perform maintenance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptfilemaintenanceperformmaking

Problem

I wish to make a file to click on to do the 'usual' tasks I would do to clean/perform maintenance on my mother's computer when I'm not present. I made a script file and took a few command lines from Google. Am I on the right path?


Option Explicit
On Error Resume Next
Dim WshShell
Dim retVal
set WshShell=CreateObject("WScript.Shell")
WshShell.run "CCleaner.exe /AUTO"

WshShell.run "WiseRegCleaner.exe /AUTO"

WshShell.run "Cleanmgr /sagerun:1"

WshShell.run "Defrag volume c:"

wshshell.run "WiseDiskCleaner.exe /AUTO"

wshshell.run "mbam.exe /fullscanterminate"

WScript.Quit

Solution

This is awesome:

Option Explicit


By doing that you've forced yourself to declare every variable you use; making a typo in a variable's name will stop the code from being executable, which is a very good thing. Well done!

This is less awesome:

Dim retVal


Declaring variables you're not using is, well, useless. Use them or kill 'em.

Then there's this:

On Error Resume Next


It basically says "look, I don't care what happens, just run every single executable statement you see and whatever happens happens" - so if any command raises an error, you completely ignore it and move on: if CCleaner.exe doesn't exist or the /AUTO option isn't valid for that command, your script will never even know.

You could log it in a text file that you can consult and clean up when you check out mom's computer, or simply output a message that contains the description of the error, for example:

On Error GoTo ErrHandler

'...script...

WScript.Quit

ErrHandler:
WScript.Echo "Unexpected error: " & Err.Description
Resume Next


This is minimal error handling - it does pretty much exactly the same thing (it just keeps going), except if there's an error it will output an error message before resuming with the next instruction.

That "jumping around" isn't ideal though - when there's an error with a command, execution jumps to ErrHandler, prints the error, and the jumps back to the instruction immediately following the line that raised the error. There's a better way.

Make yourself a Function. The advantage of a function is that it makes the shell object short-lived (it's scoped to the function), and it can handle an error that not only could happen when passing the command string to the shell object's Run method, but also if Run returns a non-zero error code - if all goes well the function itself returns a success message, and if anything goes wrong it returns an error message. That level of abstraction also makes the actual script much less cluttered.

With this function, your entire script could look like this:


    Option Explicit

    Function Execute(command)    
        On Error GoTo CleanFail

        Dim shell
        Set shell = CreateObject("WScript.Shell")

        Dim code
        code = shell.Run(command)

        Dim result
        If code <> 0 Then
            result = "Non-zero exit code: " & CStr(code)
        Else
            result = "Command '" & command & "' completed successfully."
        End If

    CleanExit:
        Execute = result
        Exit Function
    CleanFail:
        result = "Unexpected error #" & CStr(Err.Number) & ": " & Err.Description
        Resume CleanExit
    End Function

    WScript.Echo Execute("CCleaner.exe /AUTO")
    WScript.Echo Execute("WiseRegCleaner.exe /AUTO")
    WScript.Echo Execute("Cleanmgr /sagerun:1")
    WScript.Echo Execute("Defrag volume c:")
    WScript.Echo Execute("WiseDiskCleaner.exe /AUTO")
    WScript.Echo Execute("mbam.exe /fullscanterminate")

    WScript.Echo "Completed."


Notice there's no need to declare any script-level ("global") variables, handle errors at script-level, or to explicitly Quit anything - it simply runs to completion and lets the script host deal with termination.

If all goes well then your output should look like this:

Command 'CCleaner.exe /AUTO' completed successfully.
Command 'WiseRegCleaner.exe /AUTO' completed successfully.
Command 'Cleanmgr /sagerun:1' completed successfully.
Command 'Defrag volume c:' completed successfully.
Command 'WiseDiskCleaner.exe /AUTO' completed successfully.
Command 'mbam.exe /fullscanterminate' completed successfully.
Completed.

Code Snippets

Option Explicit
On Error Resume Next
On Error GoTo ErrHandler

'...script...

WScript.Quit

ErrHandler:
WScript.Echo "Unexpected error: " & Err.Description
Resume Next
<job>
<script language="VBScript">
    Option Explicit

    Function Execute(command)    
        On Error GoTo CleanFail

        Dim shell
        Set shell = CreateObject("WScript.Shell")

        Dim code
        code = shell.Run(command)

        Dim result
        If code <> 0 Then
            result = "Non-zero exit code: " & CStr(code)
        Else
            result = "Command '" & command & "' completed successfully."
        End If

    CleanExit:
        Execute = result
        Exit Function
    CleanFail:
        result = "Unexpected error #" & CStr(Err.Number) & ": " & Err.Description
        Resume CleanExit
    End Function

    WScript.Echo Execute("CCleaner.exe /AUTO")
    WScript.Echo Execute("WiseRegCleaner.exe /AUTO")
    WScript.Echo Execute("Cleanmgr /sagerun:1")
    WScript.Echo Execute("Defrag volume c:")
    WScript.Echo Execute("WiseDiskCleaner.exe /AUTO")
    WScript.Echo Execute("mbam.exe /fullscanterminate")

    WScript.Echo "Completed."
</script>
</job>

Context

StackExchange Code Review Q#135149, answer score: 3

Revisions (0)

No revisions yet.