Login | |
|
 |
RE: ProgressBar (pure VBS) - 11/3/2005 8:05:45 AM
|
|
 |
|
| |
Fredledingue
Posts: 326
Score: 0
Joined: 5/9/2005
From:
Status: offline
|
This new version allows you to stop your script by pressing the "cancel" button on the taskbar. Code for ProgressBarFast.vbs Note that's a second script file, separate from the main script, and that must be in the same directory as the main script. ____________________________________________ '--------set-------- Set fso = CreateObject("Scripting.fileSystemObject") Set WshShell = WScript.CreateObject("WScript.Shell") Title = "Progress" '--------create nul file--------- '-----this file if exists, will tell the main script to continue. '------This file contains no data. Only the existance of this file is important fso.CreateTextFile "ok.nul", True '--------read from file----------- Set f = fso.GetFile("ProgressBar.log") Set ts = f.OpenAsTextStream(1,0) Execute ts.ReadAll ts.Close d = FormatNumber( Done / (sNum/100) +1 ,0,0,0,0) Msg1 = String(d, "]") & String(100 - d, ".") Msg2 = Done & "/" & sNum Msg3 = "To stop the process clic ""Cancel""" Do Until Done >= sNum '---------this popup will refresh every 2 seconds BtnCode = WshShell.Popup(Msg1 & VbCrlf & Msg2 & VbCrlf & Msg3, 2, Title, 1) '------to change the refresh speed: change the number after "Msg3," Select Case BtnCode case 1 MsgBox "The progress bar will be back in one minutes.",,Title '------ok WScript.Sleep 60000 case 2 MsgBox "The operation will be stopped once the current item is finished.",,Title fso.GetFile("ok.nul").Delete '-----deleting the nul file to tell the main script to stop wscript.quit End Select Set ts = f.OpenAsTextStream(1,0) Execute ts.ReadAll ts.Close d = FormatNumber( Done / (sNum/100) +1 ,0,0,0,0) If d <= 100 Then Msg1 = String(d, "]") & String(100 - d, ".") End If Msg2 = Done & "/" & sNum '-----------check if progressing-------- Done1 = Done2 Done2 = Done If Done2 > Done1 Then Idletime = 0 Else Idletime = Idletime +1 If Idletime = 45 Then WshShell.Popup "Program not responding!",, Title, 0 + 16 End If End If Loop
< Message edited by Fredledingue -- 11/3/2005 8:19:55 AM >
_____________________________
Fred
|
|
| |
|
|
|
 |
RE: ProgressBar (pure VBS) - 11/3/2005 8:40:46 AM
|
|
 |
|
| |
Fredledingue
Posts: 326
Score: 0
Joined: 5/9/2005
From:
Status: offline
|
Here is the sample script for ProgressBarFast.vbs You will need to insert some the codes of this script into your main script. This sample script is just doing nothing except waiting ___________________ Set WshShell = WScript.CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") '---------number of items------------ sNum = 300 '------you will need to count how many items to process before. see (1) '--------write to progressbar report--------- Set Progress = fso.OpenTextFile("ProgressBar.log", 2) '---ForWriting Progress.WriteLine "sNum = " & sNum Progress.WriteLine "Done = 0 _" Progress.Write "+0" WshShell.Run "ProgressBarFast.vbs" WScript.Sleep 250 '---------------------------------------------------- For i=0 To sNum '-------process the items here. see (2) WScript.Sleep 50 '-------replace "WScript.Sleep 50 " by some operation here. see (3) Progress.Write "+1" '-------------stop script at user's request------------ '------by pressing the "cancel" button on the progressbar (the other script), the user '------will stop this script here. '------and the script of the progressbar will delete the "ok.nul" file '-------if "ok.nul" has been deleted te script will not continue----- If Fso.FileExists("ok.nul") = False Then Progress.Close '-----------re-write your termination/end-of-script codes here (or call a sub) MsgBox "Operation aborted by user",,Title wscript.quit End If '--------------------------------- Next '-----------write your termination/end-of-script codes here (or call a sub) '-------------------------------------------------- Progress.Close MsgBox "ok",,"Test" '------------end of script sample--------- (1) The number of items that the progressbar will report to be done. For example you want to process 300 text files, you will have to first know that there will be 300 files to process, then write sNum = 300. If you don't know how many items to process, it will be useless but there are many easy ways to count. For example File.Count or a quick loop that will read each lines in text file etc (2)The items will have to be processed in the For/Next loop. As the script know the exact number of items there should be no error such as "out of range" etc. For/Next can be replaced by Do/Loop or While/Wend if you prefer. I think it should work as well (3) WScript.Sleep 50 is NOT necessary, PLEASE REMOVE IT in your script, I just wrote it for the demo. In place there should be your code processing the item. This code can be as long as you want.
< Message edited by Snipah -- 11/5/2005 6:50:47 PM >
_____________________________
Fred
|
|
| |
|
|
|
| |
|
|
 |
|
 |
|
|