Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can using Wscript.StdOut.Write ?

2,221 views
Skip to first unread message

moonhk

unread,
Oct 20, 2009, 3:20:01 AM10/20/09
to
Hi All

Does wscript object can using Wscript.StdOut.Write to dos prompt
directory using using wscript ?

C:\wscript bar.vbs


Option Explicit
dim strComputer
dim objWMIService, colServices, objService

Wscript.Echo "Processing information. This might take several
minutes."

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root
\cimv2")

Set colServices = objWMIService.ExecQuery("Select * from
Win32_Service")

For Each objService in colServices
'Wscript.StdOut.Write(".")
'wscript.echo "."
wscript.echo objService.name
Next

'Wscript.StdOut.WriteLine
Wscript.Echo "Service information processed."

Pegasus [MVP]

unread,
Oct 20, 2009, 3:26:37 AM10/20/09
to

"moonhk" <moo...@gmail.com> wrote in message
news:b9bd00a5-b518-47f6...@u16g2000pru.googlegroups.com...

Cscript.exe will write to the console, wscript.exe will write to a dialog
box.


Alexander Mueller

unread,
Oct 20, 2009, 4:09:55 AM10/20/09
to
moonhk schrieb:

>
> Does wscript object can using Wscript.StdOut.Write to dos prompt
> directory using using wscript ?
>
> C:\wscript bar.vbs
>
> 'Wscript.StdOut.WriteLine
> Wscript.Echo "Service information processed."


'WScript.StdOut.WriteLine' will raise an error 'invalid file handle'
if the script is not hosted by cscript.exe.
That's because Standard Streams (StdOut, StdIn, StdErr) are something
that refers to consoles by design, WScript doesn't support them.
So if you launch your script by 'wscript bar.vbs' it won't work,
use 'cscript bar.vbs' instead.


MfG
Alex

moonhk

unread,
Oct 20, 2009, 5:33:58 AM10/20/09
to

But cscript.exe can not accept paramter input ? Some of the vbs accpt
parameter input

Any suggestion ?

' MsgBox WScript.Arguments.Count
cnt_arg = WScript.Arguments.Count
xfn = ""
'MsgBox cnt_arg
If cnt_arg > 0 Then
For cnt = 0 To cnt_arg - 1
If WScript.Arguments(cnt) = "-h" Then
Call help
WScript.Quit
ElseIf WScript.Arguments(cnt) = "-f" Then
xfn = WScript.Arguments(cnt + 1)
End If
Next
End If

Pegasus [MVP]

unread,
Oct 20, 2009, 5:43:58 AM10/20/09
to

"moonhk" <moo...@gmail.com> wrote in message
news:7545b78e-4001-49bc...@x6g2000prc.googlegroups.com...

Any suggestion ?

========

Yes, cscript will process command line parameters. Your script works - what
is the problem?


Alexander Mueller

unread,
Oct 20, 2009, 6:26:18 AM10/20/09
to
moonhk schrieb:

> On 10月20日, 下午4時09分, Alexander Mueller <mille...@hotmail.com> wrote:
>> moonhk schrieb:
>>
>>> Does wscript object can using Wscript.StdOut.Write to dos prompt
>>> directory using using wscript ?

>>> C:\wscript bar.vbs
>>> 'Wscript.StdOut.WriteLine

>>> Wscript.Echo "Service information processed."
>> 'WScript.StdOut.WriteLine' will raise an error 'invalid file handle'
>> if the script is not hosted by cscript.exe.
>> That's because Standard Streams (StdOut, StdIn, StdErr) are something
>> that refers to consoles by design, WScript doesn't support them.
>> So if you launch your script by 'wscript bar.vbs' it won't work,
>> use 'cscript bar.vbs' instead.

>

> But cscript.exe can not accept paramter input ? Some of the vbs accpt
> parameter input
>
> Any suggestion ?

Sure it can. Try it. It doesn't depend on the hosting app.
Seems to me you're mislead by the term 'WScript'.

1.st, 'WScript' as keyword in Windows-script - be it hosted by
Wscript.exe or Cscript.exe - is an
intrinsic automated singleton object whose existence you can
rely on throughout any scripts runtime - as long they're hosted
by one of these two 'offical' Microsoft Windows host apps
- you don't have the WScript-object in HTA, HTML, ASP or
3rd-party hosted scripts - also independendly of the scripting
language.

WScript-object always exposes an Argument-property, that
as of WSH 5.6 is devided into a named and an unnamed
collection of commandline-args.

An alias for the WScript-object is btw 'WSH'
That's why the shorthand 'WSH.Echo WSH.FullName, WSH.Version'
returns the host path and the major version of WSH in VBS.


2.nd, 'WScript' on the cmd-line denotes an executable
name (lacking the extension) that you can run ("host")
a script with - instead of hosting it with cscript.exe.


3.rd, 'WScript' as part of the progids of some scriptable
COM-interfaces ('WScript.Shell', WScript.Network')
refers to different kind of objects that you have to
explictly create inside your scripts, like

set shell = createobject("Wscript.Shell")

"Wscript.Shell" refers btw to another COM-interface, technical
names are 'IWshShell' to 'IWshShell3' - then the interface exposed
by WScript-object, whose technical name is 'IHost' and which has
no progid since it's non-createable.


So now you probably done with WScript ;-)


MfG,
Alex

moonhk

unread,
Oct 20, 2009, 11:53:52 AM10/20/09
to

when using cscript , when input -h , will not call help procedure.
Any example for using cscript can allow arguments input ?
the cnt_arg return 0 when using cscript.

Tom Lavedas

unread,
Oct 20, 2009, 1:23:47 PM10/20/09
to

I don't have a problem whith your code. I tested this slightly
modified version at the command prompt and it worked fine ...

cnt_arg = WScript.Arguments.Count
xfn = ""

wsh.echo cnt_arg


If cnt_arg > 0 Then
For cnt = 0 To cnt_arg - 1

select case WScript.Arguments(cnt)
case "-h"
help
WScript.Quit 0
case "-a"
wsh.echo "Option a"
case "-f"


xfn = WScript.Arguments(cnt + 1)

case else
wsh.echo "Internal error!" ' actually unrecognized argument
wsh.quit 1
end select
Next
End If
wsh.echo "File:", xfn

sub help
wsh.echo "Syntax is ..."
end sub

The command line should look like this for the -h option ...

cscript //nologo yourscript.vbs -h

Or for inputting a file name ...

cscript //nologo yourscript.vbs -f somename.txt

etc.

I think this comes close to answering your other question (about Unix
bash getopt) as well. In VBS it is the Select Case construct.
_____________________
Tom Lavedas

Alexander Mueller

unread,
Oct 22, 2009, 11:38:29 AM10/22/09
to
moonhk schrieb:
>> moonhk schrieb:

>>
>>> ' MsgBox WScript.Arguments.Count
>>> cnt_arg = WScript.Arguments.Count
>>> xfn = ""
>>> 'MsgBox cnt_arg
>>> If cnt_arg > 0 Then
>>> For cnt = 0 To cnt_arg - 1
>>> If WScript.Arguments(cnt) = "-h" Then
>>> Call help
>>> WScript.Quit
>>> ElseIf WScript.Arguments(cnt) = "-f" Then
>>> xfn = WScript.Arguments(cnt + 1)
>>> End If
>>> Next
>>> End If
>
> when using cscript , when input -h , will not call help procedure.
> Any example for using cscript can allow arguments input ?
> the cnt_arg return 0 when using cscript.
>

Cscript handles arguments entirely the same way as wscript does.
So if "Sub Help" is never called then the problem is elsewhere.

Here is an example that calls a vbs with cscript and -option "-h"
which trigger Sub Help:

'ch.vbs
Set args = WScript.Arguments
If args.Count <> 1 Then
Call StartWithHelp
ElseIf args(0) <> "-h" Then
Call StartWithHelp
ElseIf args(0) = "-h" Then
Call Help
End if

Sub StartWithHelp
CreateObject("Wscript.Shell").Run "cscript //NOLOGO """ _
& WScript.ScriptFullName & """ -h", 1, False

End Sub

Sub Help
WSH.Echo "******************************************"
WSH.Echo " HELP: *"
WSH.Echo " *"
WSH.Echo "Help: Run me with or without option ""-h"" *"
WSH.Echo " *"
WSH.Echo "******************************************"
WSH.Echo "Press ENTER to quit...."
WSH.StdIn.ReadLine
End Sub
'EOF

MfG
Alex

Todd Vargo

unread,
Oct 22, 2009, 5:23:58 PM10/22/09
to
moonhk wrote:

> when using cscript , when input -h , will not call help procedure.
> Any example for using cscript can allow arguments input ?
> the cnt_arg return 0 when using cscript.

This example will display help regardless of which host runs the script. The
script will display the arguments that you provide.

If WScript.Arguments.Count = 0 Then
Help
ElseIf WScript.Arguments(0) = "-h" Then
Help
End if

For I = 0 to WScript.Arguments.Count - 1
args = args & WScript.Arguments(I) & " " 'collect args
Next

Wscript.Echo "Script started with these options..." _
& vbNewline & args ' display args

Sub Help
msg = "Script requiring arguments." & vbNewline _
& vbNewline & "Syntax: " & WScript.ScriptName & " args" _
& vbNewline & " args - Required arguments" _
& vbNewline & " -h - Help"
Wscript.Echo msg
Wscript.Quit
End Sub

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Alexander Mueller

unread,
Oct 24, 2009, 7:59:58 AM10/24/09
to
Todd Vargo schrieb:

> For I = 0 to WScript.Arguments.Count - 1
> args = args & WScript.Arguments(I) & " " 'collect args

args = args & """" & WScript.Arguments(I) & """ " 'collect args

will guarantee that any arg containing spaces is passed to 2nd
call as one arg again

sry 4 nitpicking ;-) ,
Alex

> Next

0 new messages