Guy’s Scripting Ezine 88 – Progress Bar

Contents for Ezine 88 – Progress Bar

This Week’s Secret

I am torn between wanting to create a consistent format for the ezine, so that you know what to expect, and wanting to provide fresh material.  This week I have erred on the side of giving you something different.

In response to readers’ requests for a progress bar, I researched a script at JSWare. For once, I am truly putting myself in your shoes.   I admit that at first I did not understand the JSWare code.  (Is that how you feel about my scripts?)  Then as I examined the methods and traced the sub routines, it began to make sense.  (Is that what happens when you read my code?)  If there was one thing that surprised me, it was that the Progress bar relies on Internet Explorer for its shell.  Finally, I got bolder and started altering values to slow down or speed up the progress bar.  (It’s my greatest wish that you amend my scripts.)

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Scenario: You want to Script a Progress Bar…

We have all seen progress bars that lie.  NT 4.0 had a classic bar that would whiz up to 99% then you had to wait half an hour for the last 1%.  Having dissected a few of these progress bar scripts, I can see that they all include a timing variable, which is just a best guess as to how long the event will take to complete. 

From a scripting point of view, you would probably add this code at the beginning of a script that took a long time to execute.  The good news is that you can adjust the timer variable in your progress bar script.

IEProgBar- Progress Bar Class

VBScript samples from JSWare – www.jsware.net

 

JSWare
www.jsware.net
jsware@jsware.net

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

JSWare Progress Bar Script

Prerequisites

This script should run on any Windows machine

Instructions for Creating JSWare Progress Bar Script

  1. Copy and paste the example script below into notepad or use a VBScript editor.
  2. One advantage of a good script editor such as OnScript is that you can see the line numbers. (Free download of OnScript Button Top Right)
  3. Save the file with a .vbs extension, for example: ProgressBar.vbs 
  4. Double click ProgressBar.vbs and check the message box.
 

‘ ProgressBar.vbs
‘ VBScript to display a progress bar
‘ JSWare
‘ ——————————————————————‘
Dim bar, i
Set bar = new IEProgBar
With bar
.Move -1, -1, 500, -1
.Units = 30
.Show
For i = 0 to 28
WScript.Sleep 500
.Advance
Next
End With
Set bar = Nothing

‘——– Start Progress bar Class ———————————-
Class IEProgBar
Private FSO, IE, BCol, TCol, ProgCol, ProgNum, ProgCaption, Pic, Q2, sTemp, iProg, ProgTitle

Private Sub Class_Initialize()
On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
sTemp = FSO.GetSpecialFolder(2)
Set IE = CreateObject("InternetExplorer.Application")
With IE
.AddressBar = False
.menubar = False
.ToolBar = False
.StatusBar = False
.width = 400
.height = 120
.resizable = True
End With
BCol = "E0E0E4" ‘–background color.
TCol = "000000" ‘–caption text color.
ProgCol = "0000A0" ‘–progress color.
ProgNum = 19 ‘number of progress units.
ProgCaption = "Progress. . ."
ProgTitle = "Progress"
Q2 = chr(34)
iProg = 0 ‘–to track progress.
End Sub

Private Sub Class_Terminate()
On Error Resume Next
IE.Quit
Set IE = Nothing
Set FSO = Nothing
End Sub

Public Sub Show()
Dim s, i, TS
On Error Resume Next
s = "<HTML><HEAD><TITLE>" & ProgTitle & "</TITLE></HEAD>"
s = s & "<BODY SCROLL=" & Q2 & "NO" & Q2 & " BGCOLOR=" & Q2 _
& "#" & BCol & Q2 & " TEXT=" & Q2 & "#" & TCol & Q2 & ">"
If (Pic <> "") Then
s = s & "<IMG SRC=" & Q2 & Pic & Q2 & " ALIGN=" & Q2 & "Left" & Q2 & ">"
End If
If (ProgCaption <> "") Then
s = s & "<FONT FACE=" & Q2 & "arial" & Q2 & " SIZE=2>" _
& ProgCaption & "</FONT><BR><BR>"
Else
s = s & "<BR>"
End If
s = s & "<TABLE BORDER=1><TR><TD><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR>"
For i = 1 to ProgNum
s = s & "<TD WIDTH=16 HEIGHT=16 ID=" & Q2 & "P" & Q2 & ">"
Next
s = s & "</TR></TABLE></TD></TR></TABLE><BR><BR></BODY></HTML>"
Set TS = FSO.CreateTextFile(sTemp & "\iebar1.html", True)
TS.Write s
TS.Close
Set TS = Nothing
IE.Navigate "file:///" & sTemp & "\iebar1.html"
IE.visible = True
End Sub

‘– Advance method colors one progress unit.
‘ iProg variable tracks how many
‘– units have been colored.
‘ Each progress unit is a <TD> with ID="P". They can be
‘– accessed in sequence through Document.All.Item.

Public Sub Advance()
On Error Resume Next
If (iProg < ProgNum) and (IE.Visible = True) Then
IE.Document.All.Item("P", (iProg)).bgcolor = Q2 & "#" _
& ProgCol & Q2
iProg = iProg + 1
End If
End Sub

‘–resize and/or position window. Use -1 For any value Not being Set.
Public Sub Move(PixLeft, PixTop, PixWidth, PixHeight)
On Error Resume Next
If (PixLeft > -1) Then IE.Left = PixLeft
If (PixTop > -1) Then IE.Top = PixTop
If (PixWidth > 0) Then IE.Width = PixWidth
If (PixHeight > 0) Then IE.Height = PixHeight
End Sub

‘–remove Registry settings that display advertising in the IE title bar.
‘– This change won’t show up the first time it’s used because the IE
‘– instance has already been created when the method is called.

Public Sub CleanIETitle()
Dim sR1, sR2, SH
On Error Resume Next
sR1 = "HKLM\Software\Microsoft\Internet Explorer\Main\Window Title"
sR2 = "HKCU\Software\Microsoft\Internet Explorer\Main\Window Title"
Set SH = CreateObject("WScript.Shell")
SH.RegWrite sR1, "", "REG_SZ"
SH.RegWrite sR2, "", "REG_SZ"
Set SH = Nothing
End Sub

‘————- Set background color: ———————

Public Property Let BackColor(sCol)
If (TestColor(sCol) = True) Then BCol = sCol
End Property

‘————- Set caption color: ———————

Public Property Let TextColor(sCol)
If (TestColor(sCol) = True) Then TCol = sCol
End Property

‘————- Set progress color: ———————

Public Property Let ProgressColor(sCol)
If (TestColor(sCol) = True) Then ProgCol = sCol
End Property

‘————- Set icon: ———————

Public Property Let Icon(sPath)
If (FSO.FileExists(sPath) = True) Then Pic = sPath
End Property

‘————- Set title text: ———————

Public Property Let Title(sCap)
ProgTitle = sCap
End Property

‘————- Set caption text: ———————

Public Property Let Caption(sCap)
ProgCaption = sCap
End Property

‘————- Set number of progress units: ———————

Public Property Let Units(iNum)
ProgNum = iNum
End Property

‘–confirm that color variables are valid 6-character hex color codes:
‘– If Not 6 characters Then TestColor = False
‘– If any character is Not 0-9 or A-F Then TestColor = False

Private Function TestColor(Col6)
Dim iB, sB, iB2, Boo1
On Error Resume Next
TestColor = False
If (Len(Col6) <> 6) Then Exit Function
For iB = 1 to 6
sB = Mid(Col6, iB, 1)
iB2 = Asc(UCase(sB))
If ((iB2 > 47) and (iB2 < 58)) or ((iB2 > 64) and (iB2 < 71)) Then
Boo1 = True
Else
Boo1 = False
Exit For
End If
Next
If (Boo1 = True) Then TestColor = True
End Function

End Class

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Guy Learning Points

Note 1: Probably the easiest way to adjust the speed is to change line 13:
WScript.Sleep 500.  For example to speed up try WScript.Sleep 80.  (80/1000s of a second)

Note 2: A different tactic is to change these two values on lines 10 and 12.:
.Units = 30
.Show
For i = 0 to 28

For example change them each to equal 100.  Or you could do something silly and just change one of the pair to = 5.

Note 3: Other challenges you could try are resizing the progress bar, or even changing to read something other than Progress bar… See Line 41 ProgCaption.

JSWare Notes – Well worth studying

Usage:To create Progress Bar: Dim ob

Set ob = New IEProgBar

To close the bar window just set the object to nothing.

Methods and Properties:

Methods –

* Show – displays progress bar by writing file, causing IE to open it and setting IE visible.
* Advance – advances progress by 1 unit.
* Move(Left, Top, Width, Height) – moves and/or resizes window. All parameters must be used. use -1 For any dimension Not being changed: ob.Move 10, 10, -1, -1 default size is 400 W x 120 H. default position is Windows default.

* CleanIETitle – removes Registry settings that append advertising to the page title in the IE title bar so that only the specified Title property will be displayed. (This is a general change to IE and is not reversible with this script as written. It does not work the first time the method is used because the IE instance has already been created when the method is called.

Properties –

* BackColor – 6-character hex code to specify background color. default is "E0E0E4".
* TextColor – 6-character hex code to specify caption text color. default is "000000".
* ProgressColor – 6-character hex code to specify progress color. default is "0000A0".
* Title – window title text. default is "Progress"
* Caption – text caption in window. default is "Progress. . ."
* Units – number of progress units to use. default is 20.
* Icon – path of any image file that can be used as an icon. (JPG, GIF, BMP or ICO) default is no icon. If an icon is specified it appears to left of caption.

Note about Progress window title:

The Title property works by using the Title property value within the <TITLE> tag in the HTML page that creates the progress bar.  The default title caption includes the words, "Microsoft Internet Explorer".  The title caption may also include other advertising text, depending upon where you got your copy of Internet Explorer.  The CleanIETitle method will remove all advertising from the IE title bar but be aware that it’s a change to IE rather than just a setting in the progress bar.

Summary of Progress Bar

If you are looking for a progress bar, then here is an elegant script to produce a moving bar.  In reality, you may wish to to add this to regular scripts, particularly those that install software or read or write intensively.   Take the time to study the code and try and make adjustments, particularly with WScript.Sleep.

See more about VBScript

VBScripts  • Ezines  • WMI  • Logon Scripts  • Tool Kit

Ezine 26 SendKeys  • Ezine 34 Scriptomatic  • Ezine 47 .put  •Ezine 51 Sleep  • Ezine 52 OS

Ezine 77 Scriptomatic  • Ezine 84 AcctInfo  • Ezine 88 Progress bar  • Ezine 89 SendKeys

Ezine 97 Net Time  • Ezine 98 W32 Time  • Ezine 99 Time services  • Ezine 120 Sendkeys