Join 3,370 readers in helping fund MetaFilter (Hide)


vbscript printing to command line
December 26, 2007 11:55 AM   Subscribe

How does one make a VBScript print to the command line without invoking Cscript before the script.vbs?

Typically, one prints in VBscript using a Wscript.echo("Hello, world!") line or some variant thereof. If you do not invoke with a cscript hello-world.vbs, you get a GUI/pop-up MsgBox, which I wish to avoid. I just want something to go right to the command line for output, without putting cscript at the beginning.

Wscript.StdOut.WriteLine dies, of course, if not also invoked with a Cscript.

I do not want to permanently set my default scripting host to Cscript, either. This isn't "my" environment, so I wish to tread lightly.
posted by adipocere to Computers & Internet (9 answers total)
I'm not sure this is possible; wscript doesn't have a console attached to the process. From msdn:

The difference is only in the output — WScript generates windowed output, while CScript sends its output to the command window in which it was started.

In short, console output requires cscript. Maybe there's a workaround if you tell us why you want console output.
posted by Horselover Fat at 12:16 PM on December 26, 2007

I want console output because the process I'm starting should be unattended, rather than clicking through a bunch of MsgBoxen.

I think it's doable because I've seen iiscnfg.vbs do it, but I haven't been able to take it apart far enough to figure out how it does it. I think it may have something to do with the undocumented IISScriptHelper, but I cannot test that here. It seems a bit odd that this particular object would be required.
posted by adipocere at 12:23 PM on December 26, 2007

If it's unattended why not write to a logfile?

Not sure of the vagaries of vbscript, but could you have a cscript program launch the main non-cscripted vbs program passing some kind of logging callback?
posted by fleacircus at 12:53 PM on December 26, 2007

Writing to a log file instead of using .echo() is probably best. But if you really want console output, and you'll be running this on a machine you're free to make this sort of change to, you can make cscript the default with the following command:

cscript //H:cscript
posted by CrayDrygu at 1:02 PM on December 26, 2007

Yeah, I'm trying to avoid making Cscript the default, since this isn't an environment I own.
posted by adipocere at 1:07 PM on December 26, 2007

I generally invoke stuff from a .cmd script when I want to do stuff like this. If I'm invoking a .vbs script multiple times from inside my .cmd script, I will do it something like

set cscript="%windir%\system32\cscript.exe"
set prnmngr=%cscript% /nologo "%windir%\system32\prnmngr.vbs"

after which I can invoke prnmngr with something like

%prnmngr% -ac \\server\printer

This keeps the scripts fairly tidy, and doesn't require messing with default file associations.
posted by flabdablet at 5:40 PM on December 26, 2007

Ooh! Ooh! You can favorite this, it's the right answer. :)

What you want is a simple stub in your script that will be called when it starts, which detects how the script was started, and re-starts it explicitly using cscript.exe if needed. It makes use of the wscript.fullname property (which is the path to the running host executable, either c:\windows\system32\cscript.exe or wscript.exe). If your script is running as wscript.exe, it will simply re-launch the script using cscript.exe and exit. This way, if the local machine has wscript as the default host, it will immediately launch, detect that it was launched via wscript, and re-launch itself using as a cscript. The local host doesn't need to be reconfigured for this to work.

'this is at the start of your script

' This is somewhere else in your script
Sub CheckStartMode
     ' Returns the running executable as upper case from the last \ symbol
     strStartExe = UCase( Mid( wscript.fullname, instrRev(wscript.fullname, "\") + 1 ) )

     If Not strStartExe = "CSCRIPT.EXE" Then
          ' This wasn't launched with cscript.exe, so relaunch using cscript.exe explicitly!
          ' wscript.scriptfullname is the full path to the actual script

          set           oSh = CreateObject("")
          oSh.Run "cscript.exe """ & wscript.scriptfullname & """"

     End If
End Sub
posted by hincandenza at 12:17 AM on December 27, 2007

hincandenza, that's pretty interesting, but it closes the console window it opens as soon as the script is finished. I went through all of the options for intWindowStyle in the Run method of the WshShell object and none of them kept the spawned console open for more than a flash. Is there a hack for that, too?
posted by adipocere at 8:52 AM on December 27, 2007

Any Windows console app, including cscript, will close the console on exit if it wasn't launched from inside a console. If you launched hincandenza's solution from a cmd prompt by typing its name, and wscript was the default script handler, cscript would end up being "insulated" from the original console by the existence of the non-console wscript app that called it.

If you want to run a console-output app and keep the console open after the app has finished, the easiest way I know to do that is to run it via cmd /k. That makes it run inside a cmd shell, which you then have to terminate yourself (either by typing exit or closing the console window. This technique should work OK when combined with hincandenza's.

Another option is to run your real script from inside a .cmd wrapper script, allowing you to invoke cscript explicitly as I outlined earlier. This completely gets rid of the intermediate non-console app, so if you launch your .cmd by name from a console window, your .vbs output will end up directed to the same console window.
posted by flabdablet at 7:51 PM on December 27, 2007

« Older It's time to replace our deskt...   |  I'd like to know how multi-dim... Newer »
This thread is closed to new comments.