VBScript script progress notification

前端 未结 4 1630
花落未央
花落未央 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:42

    I found a better way to display progress while running a long script in VbScript.

    I found some code in this url took it and modified it to make it look better. The problem with the other code is we can't change the size of the progress bar. I fixed it in my code. Just change m_ProgressBar.width and height. Also change margin in the html body. That's it.

    Class ProgressBar
        Private m_PercentComplete
        Private m_CurrentStep
        Private m_ProgressBar
        Private m_Title
        Private m_Text
        Private m_Top
        Private m_Left
    
        'Initialize defaults
        Private Sub Class_Initialize()
            m_PercentComplete = 1
            m_CurrentStep = 0
            m_Title = "Progress"
            m_Text = ""
            m_Top = 100
            m_Left = 150
        End Sub
    
        Public Function SetTitle(pTitle)
            m_Title = pTitle
            if IsObject(m_ProgressBar) then
                m_ProgressBar.Document.title = m_PercentComplete & "% Complete : " & m_Title
                m_ProgressBar.Document.GetElementById("pc").InnerHtml = m_PercentComplete & "% Complete : " & m_Title
            end if
        End Function
    
        Public Function SetText(pText)
            m_Text = pText
            if IsObject(m_ProgressBar) then m_ProgressBar.Document.GetElementById("text").InnerHtml = m_Text
        End Function
    
        Public Function SetTop(pTop)
            m_Top = pTop
        End Function
    
        Public Function SetLeft(pLeft)
            m_Left = pLeft
        End Function
    
        Public Function GetTop()
            GetTop = m_ProgressBar.top
        End Function
    
        Public Function GetLeft()
            GetLeft = m_ProgressBar.left
        End Function
    
        Public Function Update(percentComplete)
            If percentComplete > 100 Then
                m_PercentComplete = 100
            elseif percentComplete < 1 then
                m_PercentComplete = 1
            else
                m_PercentComplete = percentComplete 
            end if
            UpdateProgressBar()
        End Function
    
        Public Function Show()
            Set m_ProgressBar = CreateObject("InternetExplorer.Application")
            'in code, the colon acts as a line feed
            m_ProgressBar.navigate2 "about:blank" : m_ProgressBar.width = 800 : m_ProgressBar.height = 380 : m_ProgressBar.toolbar = false : m_ProgressBar.menubar = false : m_ProgressBar.statusbar = false : m_ProgressBar.visible = True : m_ProgressBar.Resizable = False : m_ProgressBar.top = m_Top : m_ProgressBar.left = m_Left
            m_ProgressBar.document.write "
    0% Complete
    " m_ProgressBar.document.write "
    " _ & "
    " m_ProgressBar.document.write "
    " End Function Public Function Close() m_ProgressBar.quit End Function Private Function UpdateProgressBar() if m_CurrentStep <> m_PercentComplete then If m_PercentComplete = 100 Then m_ProgressBar.Document.GetElementById("statusbar").InnerHtml = "
    " else m_ProgressBar.Document.GetElementById("progress").style.width = m_PercentComplete & "%" end if m_ProgressBar.Document.title = m_PercentComplete & "% Complete : " & m_Title m_ProgressBar.Document.GetElementById("pc").InnerHtml = m_PercentComplete & "% Complete : " & m_Title m_ProgressBar.Document.GetElementById("text").InnerHtml = m_Text m_CurrentStep = m_PercentComplete end if End Function End Class

    Then you add the below code to display the progress bar and update the current status of progress.

    'Declare progressbar and percentage complete
    Dim pb
    Dim percentComplete
    'Setup the initial progress bar
    Set pb = New ProgressBar
    percentComplete = 0
    pb.SetTitle("Step 1 of 5")
    pb.SetText("Copying bin/Debug Folder")
    pb.SetTop(150) ' These are optional
    pb.SetLeft(300) ' These are optional
    pb.Show()
    
    'Loop to update the percent complete of the progress bar
    'Just add the pb.Update in your code to update the bar
    'Text can be updated as well by pb.SetText
    Do While percentComplete <= 100
        wscript.sleep 500
        pb.Update(percentComplete)
        percentComplete = percentComplete + 10
    Loop
    wscript.sleep 2000
    pb.Close()
    
    'This shows how you can use the code for multiple steps
    Set pb = New ProgressBar
    percentComplete = 0
    pb.SetTitle("Step 2 of 5")
    pb.SetText("Copying bin/Release Folder")
    pb.Show()
    pb.Update(percentComplete)
    Do While percentComplete <= 100
        wscript.sleep 500
        pb.Update(percentComplete)
        percentComplete = percentComplete + 10
    Loop
    msgbox "Completed", vbSystemModal
    pb.Close()
    wscript.quit
    

提交回复
热议问题