我是一个VBScript新手,编写一个将解析大型输入文件的脚本,可能需要几分钟的运行时间才能完成处理.在这漫长的处理时间内,我需要一种方法来提醒用户脚本正在运行而没有错误.我的第一个想法是为处理的每1000条记录提供一个msgbox(例如"脚本到目前为止已经成功处理了1000条记录.")还没有完全破解编码增量器的正确方法,该增量器将有条件地每隔N条记录一次msgbox(或者确定是否有更好的方法来实现我的最终目标).有任何想法吗?
如果您在控制台窗口中运行脚本(通过cscript.exe),那么您可以直接在窗口/输出中显示一个虚拟进度条,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
然后在脚本的顶部使用以下示例:
ForceConsole()
For i = 1 To 100
progress(i, 100)
Next
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想使用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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22950 次 |
| 最近记录: |