Discussion:
what object can I use to output to console
(too old to reply)
Big D
2009-03-31 14:16:26 UTC
Permalink
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for writing
messages to the console and not caring if the scripting engine is wscript or
cscript?(stdout) I don't want to worry about putting cscript in front of all
my scripts to specify.
T Lavedas
2009-03-31 15:36:21 UTC
Permalink
Post by Big D
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for writing
messages to the console and not caring if the scripting engine is wscript or
cscript?(stdout) I don't want to worry about putting cscript in front of all
my scripts to specify.
Short answer - NO. The script must be running in the console to
output to it. The default script host can be changed using the //H
command line switch (type cscript /? at a command prompt for more
info).

I don't like this approach, because I prefer most of my scripts to use
the Wscript.exe host. Instead, I installed an association to another
extension that marks a console only script. This is associated with
cscript.exe as the host. For example, I just use a VBC extension to
indicate a script is a console application.

To do this from the command prompt ...

C:\> assoc .vbc=VBCFile
.vbc=VBCFile

C:\>ftype vbcfile=%SystemRoot%\System32\CScript.exe "%1" %*

Then add that extension to the PATHEXT variable in the System/Advanced/
Environment Variables dialog.

Then whenever a script that ends with a .VBC extension is invoked
(even from a GUI window) it will use the cscript.exe host. If
invoked by double-click in a GUI window, the script will run in a
console window - but it will disappear immediately after it finishes
(unless the script has a WSH.STDIN.READALL statement to create a pause
until Enter condition - or a WSH.SLEEP 5000). That's up to you. But,
basically, the script becomes a console only script.

Tom Lavedas
***********
http://there.is.no.more/tglbatch/
Pegasus [MVP]
2009-03-31 15:39:11 UTC
Permalink
Post by Big D
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for writing
messages to the console and not caring if the scripting engine is wscript
or cscript?(stdout) I don't want to worry about putting cscript in front of
all my scripts to specify.
The function wscript.echo is not available under wscript.exe and it
automatically defaults to msgbox. You could can get around the issue by
forcing cscript.exe to be the default interpreter. Type cscript /? to see
how it's done.
ekkehard.horner
2009-03-31 16:32:16 UTC
Permalink
Post by Big D
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for writing
messages to the console and not caring if the scripting engine is wscript or
cscript?(stdout) I don't want to worry about putting cscript in front of all
my scripts to specify.
As there is no console for a script started with wscript.exe, there is no
easy way to display the output of a wscript-hosted program (other than
in many MsgBoxes). Setting the default script host to cscript.exe (cf.
cscript /?) and/or putting 'catch wscript' code at the start of each script -

Pseudo code:
If scripthost is wscript
start process: cscript.exe script.vbs with all arguments!
exit
End If
... script ...

- are ways to make sure, that your scripts are executed by cscript.exe.

If you want to write the output of a script started with wscript.exe into
a log file, you have to replace all your WScript.Echo calls by calls to
a method of an intelligent logger object that uses the console when executed
under cscript.exe resp. a log file otherwise. One problem of this approach
is that you can't write a method equivalent to WScript.Echo x, ..., z; coming
up with a scheme for the name and setting the mode (append/rewrite) of the
file won't be easy either.
Todd Vargo
2009-04-01 04:47:20 UTC
Permalink
Post by Big D
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for writing
messages to the console and not caring if the scripting engine is wscript or
cscript?(stdout) I don't want to worry about putting cscript in front of all
my scripts to specify.
I use this code to force using cscript.exe host in case of accidental
clicking in Explorer.

Set WshShell = CreateObject("WScript.Shell")

If InStr(Ucase(WScript.FullName), "WSCRIPT.EXE") Then
'Restart using CSCRIPT.EXE
WshShell.Run "CSCRIPT.EXE //nologo " & _
Chr(34) & Wscript.ScriptFullName & Chr(34)
Wscript.Quit
End If

'These lines are for demo only
Wscript.Echo "Host is " & WScript.FullName
WshShell.Popup "Script will close in 5 seconds.",5,"Done"
--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)
Al Dunbar
2009-04-01 04:45:07 UTC
Permalink
Post by Big D
Post by Big D
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for
writing
Post by Big D
messages to the console and not caring if the scripting engine is wscript
or
Post by Big D
cscript?(stdout) I don't want to worry about putting cscript in front of
all
Post by Big D
my scripts to specify.
I use this code to force using cscript.exe host in case of accidental
clicking in Explorer.
Set WshShell = CreateObject("WScript.Shell")
If InStr(Ucase(WScript.FullName), "WSCRIPT.EXE") Then
'Restart using CSCRIPT.EXE
WshShell.Run "CSCRIPT.EXE //nologo " & _
Chr(34) & Wscript.ScriptFullName & Chr(34)
Wscript.Quit
End If
'These lines are for demo only
Wscript.Echo "Host is " & WScript.FullName
WshShell.Popup "Script will close in 5 seconds.",5,"Done"
Some helpful comments all around, one of which might seem appropriate to the
OP.

But if he wants try "console" output, to see what is going on when it is
going on, rather than reading a log file after the fact, or re-launching a
wscript launched script with cscript, another option would be to open an IE
or Word window, and write to it.

/Al
unknown
2009-04-01 05:45:55 UTC
Permalink
There is one other technique which is time-honored in multiple scripting
environments for ensuring that specific scripts run as console scripts. It
also resolves the problem of broken console input pipelines on Win32. It
does require an extra script file, but it's straightforward to do.

Assume you want to ensure that the script MyScript.vbs always runs from a
console, and MyScript.vbs is located in c:\apps\bin. What you do is create a
new file named MyScript.cmd and give it the following single-line content:

@cscript %~dpn0.vbs %*

Then you can just run the command "MyScript" if the folder c:\apps\bin is in
your search path or if c:\apps\bin is your current working directory. You
can also specify the path to the batch script instead, even just using
C:\apps\bin\MyScript. Here's what happens.

The command processor automatically matches .cmd files before other script
extensions since .cmd precedes .vbs, etc. in the %pathext% shell variable.
It then runs MyScript.cmd. The initial @ suppresses console output, and the
%~dpn0 gets expanded to the full path of MyScript.cmd, minus the final
extension. The %* becomes any commandline arguments you passed in. So it
looks like this:

@cscript c:\apps\bin\MyScript.vbs <your arguments>

You can use the same content for every wrapper script; just save a .cmd file
with that content in the same folder as your VBScript, with the same base
name as the VBScript. If you're writing a wsf, you would change the content
to read

@cscript %~dpn0.wsf %*
Post by Big D
I use Wscript.Echo in many of my scripts and it works fine when the
environment default is cscript. Is there is a command I can use for
writing messages to the console and not caring if the scripting engine is
wscript or cscript?(stdout) I don't want to worry about putting cscript in
front of all my scripts to specify.
Loading...