Подтвердить что ты не робот

VBScript script уведомление о ходе выполнения

Я новичок VBScript, пишу script, который будет синтаксический анализ больших входных файлов и, вероятно, займет несколько минут для завершения обработки. Мне нужен способ предупредить пользователей о том, что script работает без ошибок в течение этого длительного времени обработки. Моя первая мысль заключалась в том, чтобы представить msgbox для каждой обрабатываемой 1000-й записи (например, "script успешно обработал 1000 записей до сих пор".) Не совсем взломал правильный способ кодирования инкремента, который условно отключил бы msgbox каждую N-ю запись (или определяется, есть ли лучший способ достичь моей конечной цели). Любые идеи?

4b9b3361

Ответ 1

Если вы запускаете свой скрипт в окне консоли (через cscript.exe), вы можете отобразить строку с искусственным прогрессом прямо в окне/выводе, как показано ниже:

console window progress bar

Сначала объявите следующие функции в вашем VBS файле:

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

Затем в верхней части вашего сценария используйте следующий пример:

ForceConsole()

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

Ответ 2

Не используйте всплывающие сообщения для этого, если вы не хотите раздражать червь из ваших пользователей. Оберните свой код в HTA, в котором отображается индикатор прогресса, аналогичный показанному в this страница, например:

<html>
<head>
<title>Sample</title>
<hta:application
  applicationname="Sample"
  scroll="no"
  singleinstance="yes"
  windowstate="normal"
>

<script language="vbscript">
Sub Window_onLoad
  'your code here
End Sub
</script>

<style type="text/css">
* {
  font-size: 1px;
  margin: 1px;
}
div {
  position: absolute;
  left: 40%;
  top: 50%;
}
marquee {
  border: 1px solid;
  height: 15px;
  width: 200px;
}
marquee span {
  height: 11px;
  width: 8px;
  background: Highlight;
  float: left;
}
.handle-0 { filter: alpha(opacity=20); -moz-opacity: 0.20; }
.handle-1 { filter: alpha(opacity=40); -moz-opacity: 0.40; }
.handle-2 { filter: alpha(opacity=60); -moz-opacity: 0.6; }
.handle-3 { filter: alpha(opacity=80); -moz-opacity: 0.8; }
.handle-4 { filter: alpha(opacity=100); -moz-opacity: 1; }
</style>
</head>

<body>
<div>
<marquee direction="right" scrollamount="8" scrolldelay="100">
  <span class="handle-0"></span>
  <span class="handle-1"></span>
  <span class="handle-2"></span>
  <span class="handle-3"></span>
  <span class="handle-4"></span>
</marquee>
</div>
</body>
</html>

Если вы хотите предоставить более динамическую информацию, вы можете, например, добавить в тело абзац, подобный этому:

</div>
<p id="sline" style="visibility:hidden;">Processed 
<span id="rcount"></span>&nbsp;Records.</p>
</body>
</html>

и обновить его каждые 1000 записей:

...
If numRows Mod 1000 = 0 Then
  If sline.style.visibility = "hidden" Then sline.style.visibility = "visible"
  rcount.innerText = numRows
End If
...

Ответ 3

В таких случаях я хотел бы использовать метод WshShell.Popup для предоставления информации о текущем ходе.

Вот пример:

Dim WshShell, i
Set WshShell = CreateObject("WScript.Shell")

For i = 1 To 500
    'Do Something
    If i Mod 100 = 0 Then 'inform for every 100 process 
        WshShell.Popup i & " items processed", 1, "Progress" ' show message box for a second and close
    End If
Next

Ответ 4

Я нашел лучший способ показать прогресс при запуске длинного скрипта в VbScript.

enter image description here

Я нашел код в этом URL, взял и изменил его, чтобы он выглядел лучше. Проблема с другим кодом в том, что мы не можем изменить размер индикатора выполнения. Я исправил это в своем коде. Просто измените m_ProgressBar.width и height. Также измените поля в теле html. Вот оно.

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 "<body Scroll=no style='margin:100px;'><div style='text-align:center;padding:15px;'><span name='pc' id='pc'>0% Complete</span></div>"
        m_ProgressBar.document.write "<div id='statusbar' name='statusbar' style='border:1px solid blue;line-height:22px;height:30px;color:blue;'>" _
            & "<table width='100%' height='100%'><tr><td id='progress' style='width:1%' bgcolor='#0000FF'></td><td></td></tr></table></div>"
        m_ProgressBar.document.write "<div style='text-align:center;padding:15px;'><span id='text' name='text'></span></div>"
    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 = "<table width='100%' height='100%'><tr><td bgcolor='#0000FF'></td></tr></table>"
            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

Затем добавьте приведенный ниже код, чтобы отобразить индикатор выполнения и обновить текущее состояние выполнения.

'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