VBScript script progress notification

前端 未结 4 1593
花落未央
花落未央 2021-01-04 12:25

I\'m a VBScript novice, writing a script that will be parsing large input file(s) and will likely take several minutes run time to complete processing. I need a way to aler

4条回答
  •  别那么骄傲
    2021-01-04 12:47

    If you're running your script in a console window (via cscript.exe) then you can display a faux progress bar directly in the window/output like this:



    First declare the following functions in your VBS file:

    Function printi(txt)
        WScript.StdOut.Write txt
    End Function    
    
    Function printr(txt)
        back(Len(txt))
        printi txt
    End Function
    
    Function back(n)
        Dim i
        For i = 1 To n
            printi chr(08)
        Next
    End Function   
    
    Function percent(x, y, d)
        percent = FormatNumber((x / y) * 100, d) & "%"
    End Function
    
    Function progress(x, y)
        Dim intLen, strPer, intPer, intProg, intCont
        intLen  = 22
        strPer  = percent(x, y, 1)
        intPer  = FormatNumber(Replace(strPer, "%", ""), 0)
        intProg = intLen * (intPer / 100)
        intCont = intLen - intProg
        printr String(intProg, ChrW(9608)) & String(intCont, ChrW(9618)) & " " & strPer
    End Function
    
    Function ForceConsole()
        Set oWSH = CreateObject("WScript.Shell")
        vbsInterpreter = "cscript.exe"
    
        If InStr(LCase(WScript.FullName), vbsInterpreter) = 0 Then
            oWSH.Run vbsInterpreter & " //NoLogo " & Chr(34) & WScript.ScriptFullName & Chr(34)
            WScript.Quit
        End If
    End Function
    

    Then at the top of your script use the following example:

    ForceConsole()
    
    For i = 1 To 100
        progress(i, 100)
    Next
    

提交回复
热议问题