我是 VBScript 新手,正在编写一个解析大型输入文件的脚本,并且可能需要几分钟的运行时间才能完成处理。我需要一种方法来提醒用户脚本在这么长的处理时间内运行没有错误。我的第一个想法是为每处理的第 1000 条记录显示一个 msgbox(例如,“到目前为止,脚本已成功处理 1000 条记录。”)还没有完全破解编码增量器的正确方法,该增量器将有条件地每隔 N 条记录触发一个 msgbox(或确定是否有更好的方法来实现我的最终目标)。有任何想法吗?
7 回答
如果您在控制台窗口(通过 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
然后在脚本的顶部使用以下示例:
ForceConsole()
For i = 1 To 100
progress(i, 100)
Next
在这种情况下,我想使用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
除非您想惹恼您的用户,否则不要为此使用弹出消息。将您的代码包装在HTA中,该 HTA会显示类似于此页面中的进度指示器,例如:
<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> 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
...
下面使用的HTML .HTA 文件(由 VBS 脚本在 temp 目录中创建和启动)可用于通过使用“g”字符更新的 Webdings 文本字符串来显示连续的方形块。
HTML .HTA 从临时文本文件中动态读取,从第一行获取提示,从第二行获取进度条的长度。HTML 转义序列可以添加到这两行。
Option Explicit
Dim fso,wsh,temppath,tempname,temphta,fhta,z,result,info,progress,aFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set wsh = wscript.CreateObject("wscript.Shell")
temppath = fso.GetSpecialFolder(2).ShortPath & "\"
tempname = fso.GetTempName
temphta = tempname & ".hta"
Call CreateHTAFile
'CREATE THE INFO FILE
'********************
Set fhta = fso.OpenTextFile(temppath & tempname,2,True)
fhta.WriteLine "<i>Loading..."
fhta.WriteLine "g"
fhta.Close
'START THE HTML.HTA
'******************
wsh.run (temppath & temphta),0,false
'PUT YOUR PROCESSES HERE THAT UPDATE THE PROGRESS BAR VIA THE UPDATE SUB
'***********************************************************************
Randomize
for z= 1 to 20
Update "<i>Loading files...",replace(space(z), " ", "g")
wscript.sleep(int(rnd*500) + 1)
next
for z= 1 to 20
Update "<i>Checking disks...",replace(space(z), " ", "g")
wscript.sleep(int(rnd*500) + 1)
next
for z= 1 to 20
Update "<i>Looking at pictures of your wife!", "<b><font style=""color:yellow; font-family:Wingdings;"">" & replace(space(z), " ", "J")
wscript.sleep(int(rnd*500) + 1)
next
'KILL THE HTML SESSION BY GIVING IT A SINGLE "X"
'***********************************************
Update "x",""
'TIDY-UP
'*******
do while fso.FileExists(temppath & temphta)
Set aFile = fso.GetFile(temppath & temphta)
aFile.Delete
loop
wscript.sleep(200)
do while fso.FileExists(temppath & tempname)
Set aFile = fso.GetFile(temppath & tempname)
aFile.Delete
loop
wscript.quit
'***********************************
Sub Update(info,progress)
Set fhta = fso.OpenTextFile(temppath & tempname,2)
fhta.WriteLine info
fhta.WriteLine progress
fhta.Close
End Sub
'***********************************
Sub CreateHTAFile
Set fhta = fso.OpenTextFile(temppath & temphta,2,True)
fhta.WriteLine "<html>"
fhta.WriteLine "<body bgcolor=red style=""overflow:hidden;"">"
fhta.WriteLine "<div style=""color:aqua; font-family:Arial;"" id=""info""></div>"
fhta.WriteLine "<div style=""color:yellow; font-family:Webdings;"" id=""progressbar""></div>"
fhta.WriteLine ""
fhta.WriteLine "<script language=""VBScript"">"
fhta.WriteLine ""
fhta.WriteLine "Sub Update"
fhta.WriteLine ""
fhta.WriteLine " On Error Resume Next"
fhta.WriteLine ""
fhta.WriteLine " Dim objFSO, infoFile, progressbarFile"
fhta.WriteLine ""
fhta.WriteLine " Set objFSO = CreateObject(""Scripting.FileSystemObject"")"
fhta.WriteLine ""
fhta.WriteLine " Set infoFile = objFSO.OpenTextFile( """ & temppath & tempname & """,1,1)"
fhta.WriteLine ""
fhta.WriteLine " document.getElementById(""info"").innerHTML = infoFile.ReadLine"
fhta.WriteLine " document.getElementById(""progressbar"").innerHTML = infoFile.ReadLine"
fhta.WriteLine ""
fhta.WriteLine " width = 420 : height = 80"
fhta.WriteLine " window.resizeTo width, height"
fhta.WriteLine " window.moveTo screen.availWidth\2 - width\2, screen.availHeight\2 - height\2"
fhta.WriteLine ""
fhta.WriteLine " if LCase(document.getElementById(""info"").innerHTML) =""x"" then"
fhta.WriteLine " Window.Close"
fhta.WriteLine " end if"
fhta.WriteLine ""
fhta.WriteLine " window.setTimeout ""Update()"", 100, ""VBScript"""
fhta.WriteLine ""
fhta.WriteLine " If Err.Number <> 0 Then"
fhta.WriteLine " Window.Close"
fhta.WriteLine " End If"
fhta.WriteLine ""
fhta.WriteLine "End Sub"
fhta.WriteLine ""
fhta.WriteLine "Sub Window_OnLoad"
fhta.WriteLine " window.resizeTo 0, 0"
fhta.WriteLine " window.setTimeout ""Update()"", 100, ""VBScript"""
fhta.WriteLine "End Sub"
fhta.WriteLine ""
fhta.WriteLine "</script>"
fhta.WriteLine ""
fhta.WriteLine "<hta:application id=""oHTA"""
fhta.WriteLine " border=""none"""
fhta.WriteLine " innerborder=""yes"""
fhta.WriteLine " caption=""no"""
fhta.WriteLine " sysmenu=""no"""
fhta.WriteLine " maximizebutton=""no"""
fhta.WriteLine " minimizebutton=""no"""
fhta.WriteLine " scroll=""no"""
fhta.WriteLine " scrollflat=""yes"""
fhta.WriteLine " singleinstance=""yes"""
fhta.WriteLine " showintaskbar=""no"""
fhta.WriteLine " contextmenu=""no"""
fhta.WriteLine " selection=""no"""
fhta.WriteLine "/>"
fhta.WriteLine "</html>"
fhta.close
End Sub
'***********************************
但是,我通常使用 HTML-HTA 来使用这个 base64 编码的 gif 来显示菊花轮时钟:
Option Explicit
Dim fso,wsh,temphtml,temppath,fhta,objWMIService,objProcess,strComputer,colProcesses
Set fso = CreateObject("Scripting.FileSystemObject")
Set wsh = wscript.CreateObject("wscript.Shell")
Call Clock
wscript.sleep(5000) '...REPLACE THIS WITH YOUR LENGTHY PROCESS
Call KillClock(temphtml)
wscript.quit
'***********************************
Sub Clock
temppath = fso.GetSpecialFolder(2).ShortPath & "\"
temphtml = fso.GetTempName & ".hta"
Set fhta = fso.OpenTextFile(temppath & temphtml,2,True)
Call CreateHTA
wsh.run (temppath & temphtml),0,false
End Sub
'***********************************
Sub KillClock(FileName)
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
For Each objProcess in colProcesses
If InStr(objProcess.CommandLine,FileName) > 0 Then
objProcess.Terminate(0)
End If
Next
wsh.run ("cmd /c del " & temppath & temphtml),0,false
End Sub
'***********************************
Sub CreateHTA
fhta.WriteLine "<html>"
fhta.WriteLine "<script language=""VBScript"">"
fhta.WriteLine "window.resizeTo 0, 0"
fhta.WriteLine "Sub Window_OnLoad"
fhta.WriteLine "width = 75 : height = 75"
fhta.WriteLine "window.resizeTo width, height"
fhta.WriteLine "window.moveTo screen.availWidth\2 - width\2, screen.availHeight\2 - height\2"
fhta.WriteLine "End Sub"
fhta.WriteLine "</script>"
fhta.WriteLine "<hta:application id=""oHTA"""
fhta.WriteLine "border=""none"""
fhta.WriteLine "caption=""no"""
fhta.WriteLine "contextmenu=""no"""
fhta.WriteLine "innerborder=""yes"""
fhta.WriteLine "scroll=""no"""
fhta.WriteLine "showintaskbar=""no"""
fhta.WriteLine "/>"
fhta.WriteLine "<img src=""data:image/gif;base64,R0lGODlhPAA8APcfAAAAACQAAEgAAGwAAJAAALQAANgAAPwAAAAkACQkAEgkAGwkAJAkALQkANgkAPwkAABIACRIAEhIAGxIAJBIALRIANhIAPxIAABsACRsAEhsAGxsAJBsALRsANhsAPxsAACQACSQAEiQAGyQAJCQALSQANiQAPyQAAC0ACS0AEi0AGy0AJC0ALS0ANi0APy0AADYACTYA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27YsVIb/rppi6jBXxKRM7R3tC3iil4XB2gsVaqkp6RmA5CiGaEt5jmNT/82TKovwcucqR5vJRaA+7E2LX63KPN2q4/mG5Iim8Bgqy0JuCfgdsvNZ89NB95WkYINbSHFZUAFOKB7KUVkYADg5cRgRn3Z89uEACQhFEYHlvjdTRtmRIoEEg44lkUZ0QReO7bQmB9JZAAAxIRJkHKZSCdieONKLYa4xY8rnYjkSu28V9VgBw42FWqpXWXPklVmKZJtM1X5z5dgfjkTlwMpNN2ZEwXQTpWF4TQdRUt9Z1yNZ1FEo4lYFsXclfIt1A6eSgqEIJyCVhmlQHdOtKeM0zFX0Yl05smSQnRiqOZAQQZAlHklNjqkZIcOJKmWpJbqWYqmQiTRVqBK1ideNzXUWlRhjVKEqkaVdkmSiXe6VdOeA9F460AS7QmjRcNipGSQrEL03XQ2LmScckXRaRGNS234LKKwiSdkshxe+amoNVp6ZkELpRnuuI8mapOl2yIEY0LCFQtbgY42JC5EJfqq4IHYsnsurGsW5K5Bo+IVqq5CPvvjgRbZaiqNBf2CrXcdmlruTwtDay24IukGHsIY+orpoQmzpOi4F/pX0cierdkpY8A6pGiq/gH8qUIyM3yVxSbGN7NBgZ6ak76CwricRDgbdHDTHKoZL9T8Nuot1VVLFhAAOw=="" style=""position:absolute;left:5;top:5;"">"
fhta.WriteLine "</html>"
End Sub
'***********************************
尽管 HTA 解决方案很有效,但将窗口保持在顶部可能会很痛苦。当我需要使用 VBS 并真正让它跳舞时,我会使用 VBS+Powershell+C# 脚本。然后我可以直接将状态消息“飞溅”到屏幕的左上角:
这是 Powershell+C# 脚本:
Add-Type -TypeDefinition @'
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public static class Progress
{
public static int previouswidth_ = 0;
public static int previousprogressBarYOffset_ = 0;
public static Boolean previousshowProgressBar_ = true;
public static void Monitor(string filename_)
{
while(true)
{
try{
using (FileStream fs = new FileStream(@filename_, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{ //...FileStream > StreamReader SO WE DON'T LOCK THE FILE
using (StreamReader streamReader = new StreamReader(fs))
{
string info_ = streamReader.ReadLine();
streamReader.Close();
if(info_.Length<=0)
{
try{ File.Delete(@filename_);}catch{} //AN EMPTY FILE WILL END THE SESSION SO IT WILL BE DELETED
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
System.Environment.Exit(1);
}
if( info_.Trim().Substring(0,1) =="-") //A SINGLE - OR -/0 CLEARS THE SCREEN
{
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
//Thread.Sleep(500);
}
else
{
Splash(info_);
}
}
}
}
catch
{ //...OR CLOSE IF THE FILE IS DELETED
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
System.Environment.Exit(1);
}
Thread.Sleep(100);
}
}
public static void Splash(string infoMSG_)
{
try
{
Boolean showProgressBar_ = true;
string progressMSG_ = "";
string denominator_ = "";
try{ progressMSG_ = new String('g',int.Parse(infoMSG_.Split(' ')[0].Split('/')[0])); } catch{ progressMSG_ = ""; }
try{ denominator_ = new String('g',int.Parse(infoMSG_.Split(' ')[0].Split('/')[1])); }catch{}
if(denominator_=="" || denominator_.Length==0) showProgressBar_ = false;
if(showProgressBar_ != previousshowProgressBar_) SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero); //Thread.Sleep(500);
previousshowProgressBar_= showProgressBar_;
try{ infoMSG_ = infoMSG_.Substring(infoMSG_.IndexOf(' ',infoMSG_.IndexOf(' '))+1,infoMSG_.Length-infoMSG_.IndexOf(' ')-1); } catch{ infoMSG_ = ""; }
int progressBarYOffset_ = 40;
if( infoMSG_.Length <=0)
{
progressBarYOffset_ = 0;
//IF THE IMAGE DECREASES IN WIDTH, REDRAW THE SCREEN BEFORE DISPLAYING THE SHORTER IMAGE
//...THE PAUSE RELIEVES THE FLASH EFFECT
if(progressBarYOffset_ < previousprogressBarYOffset_) SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero); //Thread.Sleep(500);
}
previousprogressBarYOffset_ = progressBarYOffset_;
// YOU CAN CENTRE THE DISPLAY, BUT IT'S MORE WORK AND THE TOP-LEFT DISPLAY
// IS LESS ANNOYING AND IS HANDY FOR GENERAL ALERTS
//int x_ = Screen.PrimaryScreen.WorkingArea.Width;
//int y_ = Screen.PrimaryScreen.WorkingArea.Height;
//DETERMINE HOW WIDE THE WINDOW SHOULD BE
var zero = new Bitmap(1, 1);
Graphics g_ = Graphics.FromImage(zero);
Font stringFont = new Font( "Webdings", 14 );
SizeF stringSize = new SizeF();
stringSize = g_.MeasureString(denominator_, stringFont);
int bmpWIDTH_ = (int)(stringSize.Width)+20;
stringFont = new Font( "Arial", 18, FontStyle.Italic );
try{stringSize = g_.MeasureString(infoMSG_, stringFont);}catch{stringSize = g_.MeasureString("", stringFont);}
int infoMSGWIDTH_ = (int)(stringSize.Width)+20;
if(!showProgressBar_) { bmpWIDTH_ = infoMSGWIDTH_; }
else { if(infoMSGWIDTH_ >= bmpWIDTH_) bmpWIDTH_ = infoMSGWIDTH_; }
if( bmpWIDTH_==20 ) bmpWIDTH_=0; //IF WE ONLY HAVE THE OFFSET THEN REMOVE IT
int bmpHEIGHT_ = 40;
//IF WE SWITCH BACK TO <=20 AFTER MORE THEN REPAINT THE SCREEN TO CLEAR THE LAST IMAGE (AND ADD A PAUSE TO REDUCE THE FLASH EFFECT)
if( bmpWIDTH_ < previouswidth_ && previouswidth_ != 0 /*I.E NOT STARTUP*/) SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero); //Thread.Sleep(500);
previouswidth_ = bmpWIDTH_;
//CREATE THE INFO BAR BITMAP
var BMP = new Bitmap(bmpWIDTH_, bmpHEIGHT_);
Graphics g = Graphics.FromImage(BMP);
g.Clear(Color.Red);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
Font font = new Font("Arial", 18, FontStyle.Italic);
try{ g.DrawString(infoMSG_,font,new SolidBrush(Color.White),8,10);
} catch{}
//DISPLAY IT
IntPtr hbm = BMP.GetHbitmap();
IntPtr sdc = GetDC(IntPtr.Zero);
IntPtr hdc = CreateCompatibleDC(sdc);
SelectObject(hdc,hbm);
BitBlt(sdc, 0, 0, BMP.Width, BMP.Height, hdc, 0, 0, SRCCOPY);
if(showProgressBar_)
{
//CREATE THE PROGRESS BAR BITMAP
g.Clear(Color.Red);
font = new Font("Webdings", 14, FontStyle.Regular);
g.DrawString(progressMSG_,font,new SolidBrush(System.Drawing.Color.Yellow),10,7);
//DISPLAY IT
hbm = BMP.GetHbitmap();
SelectObject(hdc,hbm);
BitBlt(sdc, 0, progressBarYOffset_, BMP.Width, BMP.Height, hdc, 0, 0, SRCCOPY);
}
//TIDY UP
DeleteDC(hdc);
ReleaseDC(IntPtr.Zero,sdc);
DeleteObject(hbm);
}catch{return;}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult);
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
private const int WM_SETTINGCHANGE = 0x1a;
private const int SMTO_ABORTIFHUNG = 0x0002;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern int BitBlt(IntPtr hdcDst, int xDst, int yDst, int w, int h, IntPtr hdcSrc, int xSrc, int ySrc, int rop);
static int SRCCOPY = 0x00CC0020;
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern int DeleteDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
}
'@ -Language CSharp -ReferencedAssemblies system.drawing, system.windows.forms
[Progress]::Monitor($args[0]);
现在,我们可以在创建临时 .ps1 文件时将其逐行嵌入到 VBS 文件中。但是,如果您想使用大型 C# 脚本,这将成为另一个痛苦。
因此,我将 Powershell+C# 脚本拖放到此 VBS 文件 (Bin2Txt.vbs) 中以创建压缩的 base64 编码版本:
Dim wsh, fso
Set wsh = wscript.CreateObject("wscript.Shell")
Set fso = CreateObject("Scripting.fileSystemObject")
If WScript.Arguments.Count > 0 Then
For each arg in WScript.Arguments
path_and_filename = path_and_filename & arg
Next
tokens = Split(path_and_filename, "\")
infilename = tokens(UBound(tokens))
For i=0 To UBound(tokens)-1
path = path & tokens(i) & "\"
Next
else
WScript.Quit
End If
outfilename = infilename & ".txt"
tempfile = fso.GetTempName
'MAKECAB NEEDS THE TRAILING "\" REMOVED FROM path
'*****************************************************
wsh.run ("cmd /c makecab /L """ & left(path,len(path)-1) & """ """ & WScript.Arguments(0) & """ " & tempfile ),0,True
bytes_ = readBytes(path & tempfile)
'MS PUTS A LF ( 0Ah , CHR(10) ) AFTER 72 BYTES (SPEC SAYS 76) ...SO WE'LL TAKE THEM OUT
'*****************************************************
base64_ = """" & Replace(encodeBase64(bytes_), vblf, "") & """"
tempfile_ = fso.GetTempName & ".txt"
set objOutputFile = fso.CreateTextFile(path & outfilename, TRUE)
objOutputFile.WriteLine(base64_)
objOutputFile.Close
if fso.FileExists(path & tempfile) then
Set aFile = fso.GetFile(path & tempfile)
aFile.Delete
end if
wscript.quit
'*****************************************************
private function readBytes(file)
dim inStream
' ADODB stream object used
set inStream = WScript.CreateObject("ADODB.Stream")
' open with no arguments makes the stream an empty container
inStream.Open
inStream.type= 1 'TypeBinary
inStream.LoadFromFile(file)
readBytes = inStream.Read()
end function
'*****************************************************
private function encodeBase64(bytes)
dim DM, EL
Set DM = CreateObject("Microsoft.XMLDOM")
' Create temporary node with Base64 data type
Set EL = DM.createElement("tmp")
EL.DataType = "bin.base64"
' Set bytes, get encoded String
EL.NodeTypedValue = bytes
encodeBase64 = EL.Text
end function
'*****************************************************
private Sub writeBytes(file, bytes)
Dim binaryStream
Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = 1 'adTypeBinary
'Open the stream and write binary data
binaryStream.Open
binaryStream.Write bytes
'Save binary data to disk
binaryStream.SaveToFile file, 1 'adSaveCreateOverWrite
end Sub
然后将 base64 文本添加到目标 VBS 脚本(例如“Splash.vbs”)中,该脚本对其进行扩展并运行它以创建进度或状态消息:
Dim wsh, fso, PS1file_, PROGESSfile_ , Base64file_
Set wsh = wscript.CreateObject("wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
'CREATE THE POWERSHELL SCRIPT IN THE TEMP DIRECTORY
Call CreatePS1file
'CREATE THE FIRST PROGESS FILE IN THE TEMP DIRECTORY
Call Update("1/20 Loading...")
'START THE POWERSHELL SCRIPT IN THE BACKGROUND
'wsh.Run ( "powershell -NoLogo -Command ""& '" & PS1file_ & "' '" & PROGESSfile_ & "'"" "),0,false
'***************************************************************************
'PUT YOUR LENGTHY PROCESSES HERE, CALLING THE Update SUB AS THEY PROGRESS:
' 0/0 TO DISPLAY ONLY THE TEXT
' ----------------------------
Update("0/0 Show a single line of text...")
wscript.sleep(3000)
Update("0/0 Or just show the progress bar...")
wscript.sleep(2000)
' x/yy TO DISPLAY ONLY THE PROGRESS BAR
' -------------------------------------
for x = 1 to 20
Update(x & "/20")
wscript.sleep(200)
next
' "x/yy your message here" TO DISPLAY INFO AND PROGRESS
' -----------------------------------------------------
for x = 0 to 20
Update(x & "/20 Downloading files...")
wscript.sleep(200)
next
for x = 0 to 20
Update(x & "/20 Installing files and updating the registry...")
wscript.sleep(200)
next
Update("0/0 Done!")
wscript.sleep(2000)
Update("0/0 Formatting your PC...")
wscript.sleep(2000)
Update("0/0 Don't worry - Only kidding!")
wscript.sleep(2000)
'A SINGLE - OR -/0 CLEARS THE SCREEN
'-----------------------------------
Update("-/0")
wscript.sleep(500)
'KILL THE SPLASH APP BY FEEDING IT A BLANK FILE (THE PS1 APP WILL DELETE THE PROGRESS FILE)
'------------------------------------------------------------------------------------------
Update("")
'***************************************************************************
'DELETE THE SPLASH APP FROM THE TEMP DIRECTORY
if fso.FileExists(PS1file_) then
'Set aFile = fso.GetFile(PS1file_)
'aFile.Delete
end if
wscript.quit
'*****************************************************
Sub Update(text_)
Set fhta = fso.OpenTextFile(PROGESSfile_,2,True)
fhta.WriteLine text_
fhta.close
End Sub
'*****************************************************
Sub CreatePS1file
tempfolder = fso.GetSpecialFolder(2).ShortPath & "\"
PS1filename = Split(fso.GetTempName,".")(0)
PS1file_ = tempfolder & PS1filename & ".ps1"
PROGESSfile_ = tempfolder & PS1filename & ".tmp"
'FILL THE VARIABLE WITH THE BASE64 CODE AT THE BOTTOM OF THIS SCRIPT
Call FillBase64file_
'GET THE COMPRESSED FILE FROM THE BASE64 TEXT
base64_ = Base64file_
tempfile = fso.GetSpecialFolder(2).ShortPath & "\" & fso.GetTempName
bytes_ = decodeBase64(base64_)
writeBytes tempfile, bytes_
'DECOMPRESS THE FILE
wsh.run ("cmd /c expand """ & tempfile & """ """ & PS1file_ & """" ),0,True
if fso.FileExists(tempfile) then
Set aFile = fso.GetFile(tempfile)
aFile.Delete
end if
Do while not FSO.FileExists(PS1file_)
WScript.Sleep 100
Loop
End Sub
'*****************************************************
private function decodeBase64(base64)
dim DM, EL
Set DM = CreateObject("Microsoft.XMLDOM")
Set EL = DM.createElement("tmp")
EL.DataType = "bin.base64"
EL.Text = base64
decodeBase64 = EL.NodeTypedValue
end function
'*****************************************************
private Sub writeBytes(file, bytes)
Dim binaryStream
Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = 1
binaryStream.Open
binaryStream.Write bytes
binaryStream.SaveToFile file, 1
end Sub
'*****************************************************
Sub FillBase64file_
'CONTAINS THE POWERSHELL + C# SCRIPT, COMPRESSED AND
'BASE64 ENCODED BY DROPPING THE filename.ps1 FILE INTO Bin2Txt.vbs
'(CREATES filename.ps1.TXT)
Base64file_ = "TVNDRgAAAAAqCQAAAAAAACwAAAAAAAAAAwEBAAEAAAAAAAAASQAAAAEAAQDUGwAAAAAAAAAANFKgbCAAcmFkQ0RGNTcucHMxAG/VbG7ZCNQbQ0vNWXtv4sYW/5uV9jtMo6vGbh3jbFvdqpRqjTFgFWxkO4tyV6vI4AHca2w0NiHZbb77PfPwM4RuVe1t0W6YGZ/XnHPmd84YPQyv/Mc9RuzvEK+jJMqjNEFvL1+/ev3qkEXJBnmPWY53veZUHZLgCNP2suW0V/wtwUF4gnQRJWF6zNRRSnZZ+6F7SPJoh1UryTFJ9x4m99EKU7LXrzr7wzKOVijLgxy+VnGQZWhO0g3BWfb6FRKfT0DZIo2SHO0Jvo/SQ3aMwnx7h/pI650n3AvJg4DcOut1hvOXuAZpGuMgKTmzbXqcV9yULScHzDfR4r1PoxDNUvB/SqQsJ9QX6yjGSbDDdzKlZ/vpHLewKFExbFGsdnLyKEYd7kdpBHReDr7foXUGmhN8RNWa9LYUrrDlWRpi1dnjhE/1FXg7U10IHV/wtgHBbL4gUY5lWWj71Ol2VVWtafsF8QGlxQR5DlqYaOjYlz6aOsavyJ+YaGRNTSGgsLfBlNUn3Pb6c2mdlQYUHoCPcFuUrFPq605dCjN9GiVYknt1horAiNOMPy2fR2uJCVOnONnk25/7Wqm1ppY5nzlJHeIY57jmXLn3tAry1fbTE+p2dR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hZsw+mx9J4CuAsIvrLtxiCutZZpsQVi6/O1LWUHDLHCiUj7Mcp7qD+XosRwd+de2sjBceWQlGMrRYzkC8zh41H7EEKAiEsowIKe0L7jDEpv+Ulg/L4epvjbosSSuO+xLhXOZpjFqwGmhk09ZIBiEXr5FV9Mg2RwA6pBBf0bZoysXrzHByQqHepbhHSjAGcq4iSEvYEoxP4rfydb0dzIq8X1RqD/89FPxi9G/ArLJ6FvD3v8A"
End Sub
'*****************************************************
消息显示在屏幕的左上方。您可以将它们居中,但这有点麻烦,我发现用户更喜欢左上角的显示,尤其是对于单行状态消息。
Splash.vbs“更新”子程序写入一个文本文件,Powershell+C# 每 100 毫秒读取一次。如果文件为空白,Powershell+C# 将删除文本文件并退出。如果删除文本文件,则 Powershell+C# 退出。Splash.vbs 脚本然后删除临时目录中的 .ps1 文件。
文本文件选项是:
提示和进度条:
15/20 Your message...
仅在一行中提示:
0/0 Your message...
进度条仅在一行中:
5/30
要刷新屏幕以删除启动消息,只需将文件中的第一个(或唯一一个)字符加上减号(“-”)。
要结束 .ps1 应用程序,只需清空该文件。显然,如果您希望其他 VBS、powershell 或批处理文件显示消息,您可以让它继续运行。
运行 Splash.vbs,一切都会变得明显。
该技术可用于将其他 C# 应用程序封装到 VBS 脚本中,可以直接放置在网页上。然后,最终用户可以将它们复制并粘贴到他们自己的 VBS 脚本中,而许多防病毒软件包通常会忽略这些脚本。
Bin2Txt.vbs 可以压缩任何二进制文件(.exe、.png、.mp4 等),因此您的 VBS 脚本也可以重新创建这些文件并即时使用它们,前提是防病毒包没有首先删除它们。
笔记:
对于那些不信任的人,我在 Splash.vbs 文件中注释掉了 run 命令:'wsh.Run ( "powershell -NoLogo -Command ""& '" & PS1file_ & "' '" & PROGESSfile_ & "'"" "),0,false
...连同删除例程:
'DELETE THE SPLASH APP FROM THE TEMP DIRECTORY
if fso.FileExists(PS1file_) then
'Set aFile = fso.GetFile(PS1file_)
'aFile.Delete
end if
因此,运行 Splash.vbs 文件,查看在 temp 目录中创建的 .ps1 文件并检查它是否与上面的 Powershell+C# 脚本匹配。如果您满意,请取消注释“wsh.Run”和“aFile.Delete”命令并重新运行它。
我找到了一种在 VbScript 中运行长脚本时显示进度的更好方法。
我在这个 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
如果您的脚本在控制台中运行并且没有向其输出文本,这是一个非常简单的解决方案。你可以发出
wscript.stdout.write "*"
每 x 条记录。它很简单但没有限制,你可以用几行 *. 如果您向控制台发出文本,请在它们之前和结尾加上 vbcrlf,这样它们就不会与星号混淆。