VBScript 脚本进度通知 [英] VBScript script progress notification

查看:37
本文介绍了VBScript 脚本进度通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VBScript 新手,正在编写一个脚本来解析大型输入文件,并且可能需要几分钟的运行时间来完成处理.我需要一种方法来提醒用户脚本在这么长的处理时间内运行没有错误.我的第一个想法是为处理的每 1000 条记录显示一个 msgbox(例如到目前为止脚本已成功处理了 1000 条记录.")还没有完全破解编码增量器的正确方法,该增量器将有条件地每第 N 条记录触发一个 msgbox(或确定是否有更好的方法来实现我的最终目标).有什么想法吗?

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 alert users that the script is running without error during this long processing time. My first thought was to present a msgbox for every 1000th record processed (e.g. "script has successfully processed 1000 records so far.") Haven't quite cracked the proper way to code an incrementer that will conditionally trip a msgbox every Nth record (or determined if there's a better way to achieve my end goal). Any ideas?

推荐答案

如果您在控制台窗口中运行脚本(通过 cscript.exe),那么您可以像这样直接在窗口/输出中显示一个人造进度条:

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:



首先在 VBS 文件中声明以下函数:



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

这篇关于VBScript 脚本进度通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆