Mombu the Microsoft Forum

Go Back   Mombu the Microsoft Forum > Microsoft > Can using Wscript.StdOut.Write ?
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 25th October 16:20
moonhk
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?



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."
  Reply With Quote


 


2 25th October 16:20
pegasus [mvp]
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?



Cscript.exe will write to the console, wscript.exe will write to a dialog
box.
  Reply With Quote
3 25th October 16:20
alexander mueller
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


moonhk schrieb:


'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
  Reply With Quote
4 25th October 16:20
moonhk
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


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
  Reply With Quote
5 25th October 16:20
pegasus [mvp]
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


On 10?20?, ??4?09?, Alexander Mueller <mille...@hotmail.com> wrote:


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

========

Yes, cscript will process command line parameters. Your script works - what
is the problem?
  Reply With Quote
6 25th October 16:20
alexander mueller
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


moonhk schrieb:

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
  Reply With Quote
7 25th October 16:20
moonhk
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


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.
  Reply With Quote
8 25th October 16:21
tom lavedas
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


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
  Reply With Quote
9 25th October 16:22
alexander mueller
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


moonhk schrieb:

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
  Reply With Quote
10 25th October 16:22
todd vargo
External User
 
Posts: 1
Default Can using Wscript.StdOut.Write ?


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)
  Reply With Quote
Reply


Thread Tools
Display Modes




666