2009-06-17

VBscript: progress bar function

I am from time to time writing small VBscripts that doe some kind of repeating task and wanted to make some nice progress bar to show the progress. I came up with this function:
' Copyright (c) 2009 Rune Nordbøe Skillingstad 
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; version 2 dated June, 1991.
'
' This program is distributed in the hope that it will be useful, but
' WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
' USA.


Option Explicit

On Error Resume Next

Dim
i

For i = 1 To 1000
Progress 1000, i
Next

Function Progress(max,prog)
Dim length, t, x
length = 68
t = int(length * (prog/max))
WScript.StdOut.Write vbCr & "|"
For x = 0 To t
WScript.StdOut.Write "-"
Next
If
t < length Then
For
x = (t + 1) To length
WScript.StdOut.Write " "
Next
End If

WScript.StdOut.Write "| " & FormatPercent(prog/max)
If t = length Then
Wscript.StdOut.WriteLine
End If
End Function


The code includes a "sample" for 1000 iterations:

video

If you are running a high number of iterations, you might want to reduce the frequency of the redrawing of the progress bar, as it will consume unwanted CPU cycles. A quick way of doing this, is to use a a modulo on the iterator counter. This example will only redraw the progress for each 10th iteration (1000/100 = 10)
max = 1000
modulo = int(max / 100)
For
i = 1 To max
If (i Mod modulo) = 0 Then
Progress max, i
End If
Next

2009-06-02

Respawn a process

I have one program that is somewhat unstable and dies from time to time (not a service).

To make sure that the program is running most of the time, I made a small VBScript using WMI to check if the process is running, and if not, respawn it.

Here is my code:
' Respawns a process if it is not running
' Can be used in scheduled tasks.
'
' Known issuses: If the program is a GUI application, the user running the
' scheduled task should be logged in.
'
' Copyright (c) 2009 Rune Nordbøe Skillingstad <rune.skillingstad@ntnu.no>
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; version 2 dated June, 1991.
'
' This program is distributed in the hope that it will be useful, but
' WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
' General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
' USA.
'
Option Explicit

On Error Resume Next

Dim ProcName, ProcPath
Dim objWMIService, colItems, objShell

' Change this to your program
ProcName = "Program.exe"
ProcPath = "C:\Path"

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & ProcName & "'")

If colItems.Count = 0 Then
Set objShell = CreateObject("WScript.Shell")
objShell.Run ProcPath & "/" & ProcName, 1, false
Set objShell = Nothing
End If

Set colItems = Nothing
Set objWMIService = Nothing


I added this as a scheduled task. Just follow the wizard, finish, edit and click advanced schedule like this screen shot: