2010-12-07 289 views

回答

246

你的意思是:

Wscript.Echo "Like this?" 

如果運行wscript.exe下(爲.vbs擴展名的默認處理程序,所以你如果您雙擊該腳本會得到什麼),你會得到一個「 MessageBox「對話框,其中包含文字。如果您在cscript.exe下運行,您將在控制檯窗口中獲得輸出。

+18

+1的代碼之間的WScript ABD CSCRIPT – KalenGi 2014-03-15 14:27:06

+0

的區別您可以在WScript.exe的直接使用功能`MSGBOX( 「text」)`或`MsgBox(object.property)`,但`Wscript.Echo`更容易編寫。謝謝。 – erm3nda 2015-02-24 00:04:26

+11

對我而言,不管你是通過`WScript`還是`CScript`運行``WScript.Echo` *都必須被使用。也就是說,*不是`CScript.Echo`,以防未來的谷歌玩家想知道。 (*很高興msgboxes消失了(當用`cscript`運行時,但是,謝謝。) – ruffin 2015-05-11 14:10:01

49

我知道這是前一陣子,但也許這會幫助其他人。它在Dragon-IT Scripts and Code Repository上找到。

你可以用下面的方法做到這一點,並遠離cscript/wscript的差異,並允許你獲得批處理文件可能具有的相同控制檯輸出。如果您從批處理文件調用VBS並且需要使其看起來無縫,這可以提供幫助。

Set 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." 
13

你只需要強制cscript而不是wscript。我總是使用這個模板。 ForceConsole()函數將執行你的vbs到cscript中,你也有很好的別名來打印和掃描文本。

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) 
4

我遇到了這篇文章,並回到了我前段時間使用的類似於@MadAntrax的方法。

主要區別在於它使用VBScript用戶定義的類來包裝切換到CScript和輸出文本到控制檯的所有邏輯,因此它使主腳本更清潔一些。

這假設您的目標是將輸出流傳輸到控制檯,而不是輸出到消息框。

cCONSOLE類如下。要使用它,請將完整的類包含在腳本的末尾,然後在腳本的開始處將其實例化。這裏有一個例子:

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類

 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