WSF/CONTROL - PROGRESSBAR
SCRIPT: ProgressBar.wsf

<package>
<job id="ProgressBar">
<?job error="true" debug="true" ?>
<script language="VBScript">
'----------------------------------------------------------------------
' PROGRESSBAR.WSF - CLI PROGRESSBAR
' (c) 2016 Cormatron Ghost Ltd. All Rights Reserved.
'----------------------------------------------------------------------

Function PrintBack(txt)
   wscript.StdOut.write string(Len(txt),chr(08))
   wscript.StdOut.write txt
End Function

Function IntPercent(x, y)
   strPerc = FormatNumber((x / y) * 100, 1) & "%"
   IntPercent=FormatNumber(Replace(strPerc, "%", ""), 0)
End Function

Function CLI_ProgressBar(i, Max_N,Frase)
   nBlock = 22
   intPerc = IntPercent(i,Max_N)
   nWhiteBlock = nBlock * (intPerc / 100)
   nBlackBlock = nBlock - nWhiteBlock
   txtToPrint=String(nWhiteBlock, ChrW(9608)) & String(nBlackBlock, ChrW(9618))
   txtToPrint=txtToPrint & space(4-len(cstr(intPerc))) & intPerc & "% - " & Frase
   PrintBack txtToPrint
End Function

' Sezione di TEST
For i = 1 To 100
   WScript.Sleep 100
   call CLI_ProgressBar(i, 100,"completato")
Next

</script>
</job>
</package>

ESEMPIO ESECUZIONE

Questo script wsf produce il seguente output:

VBScript: ProgressBar.vbs

Ecco l'equivalente script nella forma VBScript

'----------------------------------------------------------------------
' PROGRESSBAR.VBS - CLI PROGRESSBAR
' (c) 2016 Cormatron Ghost Ltd. All Rights Reserved.
'----------------------------------------------------------------------

Function IntPercent(x, y)
   strPerc=FormatNumber((x / y) * 100, 1) & "%"
   IntPercent=FormatNumber(Replace(strPerc, "%", ""), 0)
End Function

Function PrintBack(txt)
   Wscript.StdOut.write(string(Len(txt),chr(8)))
   Wscript.StdOut.write(txt)
End Function

Function CLI_ProgressBar(i, Max_N,Frase)
   nBlock = 22
   intPerc = IntPercent(i,Max_N)
   nWhiteBlock = nBlock * (intPerc / 100)
   nBlackBlock = nBlock - nWhiteBlock
   txtToPrint=String(nWhiteBlock, ChrW(9608)) & String(nBlackBlock, ChrW(9618))
   txtToPrint=txtToPrint & space(4-len(cstr(intPerc))) & intPerc & "% - " & Frase
   PrintBack txtToPrint
End Function

' La funzione ForceConsole() forza l'esecuzione dello script vbs con Cscript.exe
Function ForceConsole()
   Set oWSH = CreateObject("WScript.Shell")
   vbsInterpreter = "cscript.exe"
   If InStr(LCase(WScript.FullName), vbsInterpreter) = 0 Then
      oWSH.Run "cmd /C " & vbsInterpreter & " //NoLogo " & Chr(34) & WScript.ScriptFullName & Chr(34)
      WScript.Quit
   End If
End Function

Call ForceConsole()
' Sezione di TEST
For i = 1 To 100
   WScript.Sleep 100
   call CLI_ProgressBar(i, 100,"completato")
Next

I files ".wsf" e vbs vengono eseguiti di default da WScript. Pertanto per poter utilizzare l'istruzione Wscript.StdOut.write è necessario specificare sulla linea di comando quale interprete utilizzare. In alternativa si può inserire la funzione ForceConsole che controlla se lo script è stato lanciato con cscript.exe (InStr(LCase(WScript.FullName), vbsInterpreter) = 0) e in caso contrario rilancia lo script (oWSH.Run "cmd /C " & vbsInterpreter & ...) utilizzando Cscript.exe.

ESEMPIO ESECUZIONE

Questo script vbs produce il seguente output: