Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of a Batch ECHO Command using VBScript?

Tags:

vbscript

We currently use Windows Batch (DOS) command files to control our process flow. To display messages to the Console, we would use the ECHO command. These messages would show up in our Scheduler software, which used to be Tivoli and now is CA WA Workstation\ ESP.

I would like to start using VBS files instead of CMD\BAT files and am trying to figure out how to do the equivalent of an ECHO to the console.

When I try to use either the WScript.Echo command or write to Standard Out, the messages are displayed in dialog boxes for both and they require the OK button to be pushed to continue. Not surprisingly, when I run unattended though a scheduler, the job hits one of these commands and just hangs since there is no one to OK the messagebox.

SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write("Test 1")

WScript.echo("Test 2")

I realize I could write the messages to a Log file using the Scripting object, but this could fail if an invalid path is provided or because of insufficient permissions. Besides, being able to see feedback write within the Scheduler is awfully convenient.

How do I write to the Console using VBScript? I’ve seen other posts here that suggest that the above methods which didn't work for the reason describe above were the way to do it.

like image 220
Chad Avatar asked Nov 02 '12 18:11

Chad


People also ask

What is echo in VBS?

The Echo method concatenates its arguments into a space-separated string and displays this string in a Dialog Box (if invoked under wscript), or on the standard output stream (if invoked under cscript). Under cscript, a newline character is appended to the output.

How do I echo a variable in VBScript?

You can just use WScript. Echo (text or variable name to print its content). Example of code: Dim x = "Hello" WScript. Echo x.

How do I create a batch file in VBScript?

Answers. Try this. Dim objFSO, outFile Set objFSO = CreateObject("Scripting. FileSystemObject") 'Open write stream Set outFile = objFSO.

What is echoing in batch file?

The ECHO command in Windows CMD is used to print out text to the screen, display the actual setting of the Command-line which means when you do: @echo off. The "C:\Users[User]" line before your command input will disappear.


1 Answers

wscript.echo is the correct command - but to output to console rather than dialogue you need to run the script with cscript instead of wscript.

You can resolve this by

  • running your script from command line like so:

    cscript myscript.vbs
    
  • changing the default file association (or creating a new file extension and association for those scripts you want to run with cscript).

  • change the engine via the script host option (i.e. as per http://support.microsoft.com/kb/245254)

    cscript //h:cscript //s
    
  • Or you can add a few lines to the start of your script to force it to switch "engine" from wscript to cscript - see http://www.robvanderwoude.com/vbstech_engine_force.php (copied below):

    RunMeAsCScript
    
    'do whatever you want; anything after the above line you can gaurentee you'll be in cscript 
    
    Sub RunMeAsCScript()
            Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
            Set wshShell = CreateObject( "WScript.Shell" )
            strEngine = UCase( Right( WScript.FullName, 12 ) )
            If strEngine <> "\CSCRIPT.EXE" Then
                    ' Recreate the list of command line arguments
                    strArgs = ""
                    If WScript.Arguments.Count > 0 Then
                            For i = 0 To WScript.Arguments.Count
                                    strArgs = strArgs & " " & QuoteIt(WScript.Arguments(i)) 
                            Next
                    End If
                    ' Create the complete command line to rerun this script in CSCRIPT
                    strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs
                    ' Rerun the script in CSCRIPT
                    Set objDebug = wshShell.Exec( strCmd )
                    ' Wait until the script exits
                    Do While objDebug.Status = 0
                            WScript.Sleep 100
                    Loop
                    ' Exit with CSCRIPT's return code
                    WScript.Quit objDebug.ExitCode
            End If
    End Sub
    
    'per Tomasz Gandor's comment, this will ensure parameters in quotes are covered:
    function QuoteIt(strTemp) 
            if instr(strTemp," ") then
                    strTemp = """" & replace(strTemp,"""","""""") & """"
            end if
            QuoteIt = strTemp
    end function
    
like image 94
JohnLBevan Avatar answered Nov 08 '22 14:11

JohnLBevan