关于Windows:将vbscript输出到控制台 |
您所在的位置:网站首页 › vbs输入文字 › 关于Windows:将vbscript输出到控制台 |
使用vbscript将结果输出到控制台的命令或最快方法是什么? 相关讨论 尝试这样:WSH.Echo"Hello World"你的意思是: 1Wscript.Echo"Like this?"如果在wscript.exe(。vbs扩展名的默认处理程序,因此双击脚本将得到的结果)下运行该命令,则将看到一个" MessageBox"对话框,其中包含文本。如果在cscript.exe下运行该命令,则会在控制台窗口中获得输出。 相关讨论 您可以直接在wscript.exe上使用函数MsgBox("text")或MsgBox(object.property),但是Wscript.Echo更易于编写。谢谢。 对我而言,Wscript.Echo必须用于通过WScript或CScript运行。也就是说,没有CScript.Echo,以防将来的Google员工感到奇怪。 (但是,非常高兴msgbox消失了[当使用CScript运行时];谢谢。) 是否可以使此消息框在1或2秒后自动关闭?我希望它弹出来通知用户正在发生的事情,但是要在1秒钟后自动关闭。 @GabrielStaples-不使用股票Wscript.Echo。我想,如果您想完全呆在WScript中,则可以执行一些令人毛骨悚然的操作,例如执行另一个进程以对父进程执行" SendKeys"以关闭MessageBox。 实际上,我只是找到了此popup方法。与echo非常相似,但是允许您指定一个超时时间,在此之后它将自动关闭弹出框。非常方便且易于使用:technet.microsoft.com/en-us/library/ee156593.aspx我知道这是前一阵子,但这也许会对其他人有所帮助。它在Dragon-IT脚本和代码存储库中找到。 您可以执行以下操作,远离cscript / wscript差异,并允许您获得与批处理文件相同的控制台输出。如果从批处理文件调用VBS并需要使其看起来无缝,这将有所帮助。 12345Set fso = CreateObject ("Scripting.FileSystemObject") Set stdout = fso.GetStandardStream (1) Set stderr = fso.GetStandardStream (2) stdout.WriteLine"This will go to standard output." stderr.WriteLine"This will go to error output." 相关讨论 如果脚本是通过双击启动的,并因此使用wscript打开,则该脚本将产生错误消息:"无效的句柄"。 @Bernhard:如果使用wscript.exe运行脚本,则会出现此错误。 Wscript是面向Windows的,没有控制台流。请改用cscript.exe:technet.microsoft.com/zh-CN/library/bb490816.aspx @BernhardHiller有一个正确的观点。这个答案的重点是直接使用stdout可以避免CScript / WScript的差异。那是不对的。此解决方案仍然仅在CScript.exe下有效,因此与仅使用Wscript.Echo相比似乎没有太多好处。实际上,差异被放大了,因为该脚本将不再在WScript下运行。它是一种有效的技术,有其用途,例如,如果有人需要写StdErr,但是在此答案的背景下,这是一种误导。 我只想展示此方法相对于Wscript.Echo的好处:cscript b foobar.vbs在没有任何控制台输出的情况下运行foobar.vbs,但是通过Robs方法,即使将\\b传递给cscript.exe,也可以具有输出。您只需要强制使用cscript而不是wscript。我总是使用此模板。函数ForceConsole()会将您的vbs执行到cscript中,并且您还有漂亮的别名来打印和扫描文本。 1234567891011121314151617181920212223242526272829303132333435363738394041424344 Set oWSH = CreateObject("WScript.Shell") vbsInterpreter ="cscript.exe" Call ForceConsole() Function printf(txt) WScript.StdOut.WriteLine txt End Function Function printl(txt) WScript.StdOut.Write txt End Function Function scanf() scanf = LCase(WScript.StdIn.ReadLine) End Function Function wait(n) WScript.Sleep Int(n * 1000) End Function Function ForceConsole() If InStr(LCase(WScript.FullName), vbsInterpreter) = 0 Then oWSH.Run vbsInterpreter &" //NoLogo" & Chr(34) & WScript.ScriptFullName & Chr(34) WScript.Quit End If End Function Function cls() For i = 1 To 50 printf"" Next End Function printf" _____ _ _ _____ _ _____ _ _ " printf"| _ |_| |_ ___ ___| |_ _ _ _| | | __|___ ___|_|___| |_" printf"| | | '_| . | | --| | | | . | |__ | _| _| | . | _|" printf"|__|__|_|_,_|___|_|_|_____|_____|___| |_____|___|_| |_| _|_| " printf" |_| v1.0" printl" Enter your name:" MyVar = scanf cls printf"Your name is:" & MyVar wait(5) 相关讨论 您确定可以回答实际问题吗? 是的,仅调用ForceConsole(),然后使用printf()在输出控制台中打印文本。另外,您还有其他别名可以清除屏幕,扫描文本并等待(睡眠)我遇到了这篇文章,回到了我前一段时间使用的一种类似于@MadAntrax的方法。 主要区别在于,它使用VBScript用户定义的类来包装用于切换到CScript并将文本输出到控制台的所有逻辑,因此使主脚本更加简洁。 假定您的目标是将输出流式传输到控制台,而不是将输出转到消息框。 下面是cCONSOLE类。若要使用它,请在脚本末尾包含完整的类,然后在脚本开始处实例化它。这是一个例子: 1234567891011121314151617181920212223242526272829303132333435 Option Explicit '// Instantiate the console object, this automatically switches to CSCript if required Dim CONS: Set CONS = New cCONSOLE '// Now we can use the Consol object to write to and read from the console With CONS '// Simply write a line .print"CSCRIPT Console demo script" '// Arguments are passed through correctly, if present .Print"Arg count=" & wscript.arguments.count '// List all the arguments on the console log dim ix for ix = 0 to wscript.arguments.count -1 .print"Arg(" & ix &")=" & wscript.arguments(ix) next '// Prompt for some text from the user dim sMsg : sMsg = .prompt("Enter any text:" ) '// Write out the text in a box .Box sMsg '// Pause with the message"Hit enter to continue" .Pause End With '= =========== End of script - the cCONSOLE class code follows here这是cCONSOLE类的代码 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 CLASS cCONSOLE '= ================================================================= '= '= This class provides automatic switch to CScript and has methods '= to write to and read from the CSCript console. It transparently '= switches to CScript if the script has been started in WScript. '= '= ================================================================= Private oOUT Private oIN Private Sub Class_Initialize() '= Run on creation of the cCONSOLE object, checks for cScript operation '= Check to make sure we are running under CScript, if not restart '= then run using CScript and terminate this instance. dim oShell set oShell = CreateObject("WScript.Shell") If InStr( LCase( WScript.FullName ),"cscript.exe" ) = 0 Then '= Not running under CSCRIPT '= Get the arguments on the command line and build an argument list dim ArgList, IX ArgList ="" For IX = 0 to wscript.arguments.count - 1 '= Add the argument to the list, enclosing it in quotes argList = argList &"""" & wscript.arguments.item(IX) &"""" next '= Now restart with CScript and terminate this instance oShell.Run"cscript.exe //NoLogo""" & WScript.ScriptName &"""" & arglist WScript.Quit End If '= Running under CScript so OK to continue set oShell = Nothing '= Save references to stdout and stdin for use with Print, Read and Prompt set oOUT = WScript.StdOut set oIN = WScript.StdIn '= Print out the startup box StartBox BoxLine Wscript.ScriptName BoxLine"Started at" & Now() EndBox End Sub '= Utility methods for writing a box to the console with text in it Public Sub StartBox() Print" " & String(73,"_") Print" |" & Space(73) &"|" End Sub Public Sub BoxLine(sText) Print Left(" |" & Centre( sText, 74) , 75) &"|" End Sub Public Sub EndBox() Print" |" & String(73,"_") &"|" Print"" End Sub Public Sub Box(sMsg) StartBox BoxLine sMsg EndBox End Sub '= END OF Box utility methods '= Utility to center given text padded out to a certain width of text '= assuming font is monospaced Public Function Centre(sText, nWidth) dim iLen iLen = len(sText) '= Check for overflow if ilen > nwidth then Centre = sText : exit Function '= Calculate padding either side iLen = ( nWidth - iLen ) / 2 '= Generate text with padding Centre = left( space(iLen) & sText & space(ilen), nWidth ) End Function '= Method to write a line of text to the console Public Sub Print( sText ) oOUT.WriteLine sText End Sub '= Method to prompt user input from the console with a message Public Function Prompt( sText ) oOUT.Write sText Prompt = Read() End Function '= Method to read input from the console with no prompting Public Function Read() Read = oIN.ReadLine End Function '= Method to provide wait for n seconds Public Sub Wait(nSeconds) WScript.Sleep nSeconds * 1000 End Sub '= Method to pause for user to continue Public Sub Pause Prompt"Hit enter to continue..." End Sub END CLASS有五种方法可以将文本输出到控制台: 1234567Dim StdOut : Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1) WScript.Echo"Hello" WScript.StdOut.Write"Hello" WScript.StdOut.WriteLine"Hello" Stdout.WriteLine"Hello" Stdout.Write"Hello"WScript.Echo将输出到控制台,但仅当使用cscript.exe启动脚本时。如果使用wscript.exe启动,它将输出到消息框。 WScript.StdOut.Write和WScript.StdOut.WriteLine将始终输出到控制台。 StdOut.Write和StdOut.WriteLine也会始终输出到控制台。它需要额外的对象创建,但比WScript.Echo快约10%。 相关讨论 ...并且如对先前答案的评论中所述,使用wscript.exe执行时,此方法不起作用:stackoverflow.com/questions/4388879/ 还找到了有关GetStandardStream()与WScript.StdIn / .StdOut / .StdErr的解释:" VBScript简而言之:桌面快速参考(第二版)" books.google.fr/books?id=NLpuZSatG3QC第298页说:"功能等效"。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |