Discussion:
Redirecting VB script sub shell standard output
(too old to reply)
Vincent Belaïche
2011-10-09 20:12:53 UTC
Permalink
Hello,

I am creating some sub shell object in the Visual basic script by

Set objShell = WScript.CreateObject("WScript.Shell")

And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to find
how to do this.

Any idea is welcome.

Vincent.
Ron Weiner
2011-10-09 22:01:27 UTC
Permalink
Post by Vincent Belaïche
Hello,
I am creating some sub shell object in the Visual basic script by
Set objShell = WScript.CreateObject("WScript.Shell")
And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to find
how to do this.
Any idea is welcome.
Vincent.
Not sure exactly what you want to accomplish, but you might be able to
pipe or redirect the standard output of the console application to a
file on the disk, then read the file into your VB app whereupon you can
do anything you want with the contents.

Research the Pipe {|} or the redirect {>} command line functions with
your console application.

Rdub
Vincent Belaïche
2011-10-10 06:02:09 UTC
Permalink
Post by Ron Weiner
Post by Vincent Belaïche
Hello,
I am creating some sub shell object in the Visual basic script by
Set objShell = WScript.CreateObject("WScript.Shell")
And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to
find how to do this.
Any idea is welcome.
Vincent.
Not sure exactly what you want to accomplish, but you might be able to
pipe or redirect the standard output of the console application to a
file on the disk, then read the file into your VB app whereupon you
can do anything you want with the contents.
Research the Pipe {|} or the redirect {>} command line functions with
your console application.
Rdub
Redirecting to a file won't do it. My VB script is run itself from a
console with csscript, and I would like the output of the sub-shell run
console application to be run in the same console.


Console (A)
+-------- VB script (B)
+---- sub-shell with console application (C)


I would like the standard output of `B' and `C' to be displayed in the
same scrollable screen that is part of `A'.

Vincent.
ralph
2011-10-10 06:50:00 UTC
Permalink
Post by Vincent Belaïche
Post by Ron Weiner
Post by Vincent Belaïche
Hello,
I am creating some sub shell object in the Visual basic script by
Set objShell = WScript.CreateObject("WScript.Shell")
And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to
find how to do this.
Any idea is welcome.
Vincent.
Not sure exactly what you want to accomplish, but you might be able to
pipe or redirect the standard output of the console application to a
file on the disk, then read the file into your VB app whereupon you
can do anything you want with the contents.
Research the Pipe {|} or the redirect {>} command line functions with
your console application.
Rdub
Redirecting to a file won't do it. My VB script is run itself from a
console with csscript, and I would like the output of the sub-shell run
console application to be run in the same console.
Console (A)
+-------- VB script (B)
+---- sub-shell with console application (C)
I would like the standard output of `B' and `C' to be displayed in the
same scrollable screen that is part of `A'.
Still not clear to me, but browse for "Shell and Wait". You'll find
examples for VB as well as VBS.

If necessary have each subsquent app write to a file, then 'echo' that
output from A to the screen.

-ralph
Vincent Belaïche
2011-10-11 05:27:17 UTC
Permalink
[...]
Post by ralph
Post by Vincent Belaïche
Rdub
Redirecting to a file won't do it. My VB script is run itself from a
console with csscript, and I would like the output of the sub-shell
run console application to be run in the same console.
Console (A)
+-------- VB script (B)
+---- sub-shell with console application (C)
I would like the standard output of `B' and `C' to be displayed in the
same scrollable screen that is part of `A'.
Still not clear to me, but browse for "Shell and Wait". You'll find
examples for VB as well as VBS.
If necessary have each subsquent app write to a file, then 'echo' that
output from A to the screen.
-ralph
Hello Ralph,

Actually I do not need some specific code to achieve "Shell And Wait",
this is already supported by WshShell with the 3rd argument of the Run
method.

Set http://msdn.microsoft.com/en-us/library/aew9yb99(v=VS.85).aspx

see also: http://msdn.microsoft.com/en-us/library/d5fk67ky(v=VS.85).aspx#CommunityContent

Here is an example:
'---------------------------- myscript.vbs ----------------------------
Dim oShell, iReturn
Set oShell = WScript.CreateObject("WScript.Shell")

Const iACTIVATE_AND_DISPLAY = 1

iReturn = oShell.Run("calc.exe",iACTIVATE_AND_DISPLAY, True)

WScript.Echo "The end!"
'----------------------------------------------------------------------

In this example you will have the echo "The end!" only after exiting
calc.exe, so I do have a wait.

What I would like is that if I launch this script (let's call it
myscript.vbs) with the DOS command:

Rem -------------------------------------------------------------------
cscript myscript.vbs
Rem -------------------------------------------------------------------

Then any output made to the standard output by the application (C) goes
to the same DOS window (A) under which I launched cscript (B), just
like "The end!" is output by the script (B) to this DOS window (A).

Note that in the example above the application (C) is is just calc.exe
but in reality that would be some console application.

Hopefully my question is clear now...

Vincent.
Vincent Belaïche
2011-10-11 20:32:03 UTC
Permalink
Post by Vincent Belaïche
[...]
Post by ralph
Post by Vincent Belaïche
Rdub
Redirecting to a file won't do it. My VB script is run itself from a
console with csscript, and I would like the output of the sub-shell
run console application to be run in the same console.
Console (A)
+-------- VB script (B)
+---- sub-shell with console application (C)
I would like the standard output of `B' and `C' to be displayed in the
same scrollable screen that is part of `A'.
Still not clear to me, but browse for "Shell and Wait". You'll find
examples for VB as well as VBS.
If necessary have each subsquent app write to a file, then 'echo' that
output from A to the screen.
-ralph
Hello Ralph,
Actually I do not need some specific code to achieve "Shell And Wait",
this is already supported by WshShell with the 3rd argument of the Run
method.
Set http://msdn.microsoft.com/en-us/library/aew9yb99(v=VS.85).aspx
see also: http://msdn.microsoft.com/en-us/library/d5fk67ky(v=VS.85).aspx#CommunityContent
'---------------------------- myscript.vbs ----------------------------
Dim oShell, iReturn
Set oShell = WScript.CreateObject("WScript.Shell")
Const iACTIVATE_AND_DISPLAY = 1
iReturn = oShell.Run("calc.exe",iACTIVATE_AND_DISPLAY, True)
WScript.Echo "The end!"
'----------------------------------------------------------------------
In this example you will have the echo "The end!" only after exiting
calc.exe, so I do have a wait.
What I would like is that if I launch this script (let's call it
Rem -------------------------------------------------------------------
cscript myscript.vbs
Rem -------------------------------------------------------------------
Then any output made to the standard output by the application (C) goes
to the same DOS window (A) under which I launched cscript (B), just
like "The end!" is output by the script (B) to this DOS window (A).
Note that in the example above the application (C) is is just calc.exe
but in reality that would be some console application.
Hopefully my question is clear now...
Vincent.
Hello,

I have found a way to allmost achieve what I am seeking for (in the
example below the console application is TREE.COM):

'-----------------temp.vbs --------------------------------------------
Dim oShell
Set oShell = WScript.CreateObject("WScript.Shell")

Dim oExec
Set oExec = oShell.Exec("TREE.COM")
Dim sLine
Do While True

Do While Not oExec.StdOut.AtEndOfStream
sLine = oExec.StdOut.ReadLine
WScript.Echo sLine
Loop
If oExec.Status = 1 Then
Exit Do
Else
WScript.Sleep 100
End if
Loop

WScript.Echo "That's all!"
'----------------------------------------------------------------------

But that does not work properly with non ASCII characters. You can try
in a DOS console to type TREE, and then CSCRIPT temp.vbs and see the
difference.

Any idea how to solve this ?

Vincent.
Ron Weiner
2011-10-12 01:04:55 UTC
Permalink
Post by Vincent Belaïche
Post by Vincent Belaïche
[...]
Post by ralph
Post by Vincent Belaïche
Rdub
Redirecting to a file won't do it. My VB script is run itself from a
console with csscript, and I would like the output of the sub-shell
run console application to be run in the same console.
Console (A)
+-------- VB script (B)
+---- sub-shell with console application (C)
I would like the standard output of `B' and `C' to be displayed in the
same scrollable screen that is part of `A'.
Still not clear to me, but browse for "Shell and Wait". You'll find
examples for VB as well as VBS.
If necessary have each subsquent app write to a file, then 'echo' that
output from A to the screen.
-ralph
Hello Ralph,
Actually I do not need some specific code to achieve "Shell And Wait",
this is already supported by WshShell with the 3rd argument of the Run
method.
Set http://msdn.microsoft.com/en-us/library/aew9yb99(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/d5fk67ky(v=VS.85).aspx#CommunityContent
'---------------------------- myscript.vbs ----------------------------
Dim oShell, iReturn
Set oShell = WScript.CreateObject("WScript.Shell")
Const iACTIVATE_AND_DISPLAY = 1
iReturn = oShell.Run("calc.exe",iACTIVATE_AND_DISPLAY, True)
WScript.Echo "The end!"
'----------------------------------------------------------------------
In this example you will have the echo "The end!" only after exiting
calc.exe, so I do have a wait.
What I would like is that if I launch this script (let's call it
Rem -------------------------------------------------------------------
cscript myscript.vbs
Rem -------------------------------------------------------------------
Then any output made to the standard output by the application (C) goes
to the same DOS window (A) under which I launched cscript (B), just
like "The end!" is output by the script (B) to this DOS window (A).
Note that in the example above the application (C) is is just calc.exe
but in reality that would be some console application.
Hopefully my question is clear now...
Vincent.
Hello,
I have found a way to allmost achieve what I am seeking for (in the
'-----------------temp.vbs --------------------------------------------
Dim oShell
Set oShell = WScript.CreateObject("WScript.Shell")
Dim oExec
Set oExec = oShell.Exec("TREE.COM")
Dim sLine
Do While True
Do While Not oExec.StdOut.AtEndOfStream
sLine = oExec.StdOut.ReadLine
WScript.Echo sLine
Loop
If oExec.Status = 1 Then
Exit Do
Else
WScript.Sleep 100
End if
Loop
WScript.Echo "That's all!"
'----------------------------------------------------------------------
But that does not work properly with non ASCII characters. You can try
in a DOS console to type TREE, and then CSCRIPT temp.vbs and see the
difference.
Any idea how to solve this ?
Vincent.
Yup!

First stop with the wscript. You can do it all in classic VB.

Create a new form and put a command button and a text box in it.

Change the Multi line prop of the text box to true.

Change the font prop of the text box to one that has the old DOS line
drawing characters. I used the Windows Terminal Font.

Paste in this code (Watch for line wraping)

-=-=-=-=-=-=-=-=-=-=-= Code starts -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long

Private Const STATUS_PENDING = &H103&
Private Const PROCESS_QUERY_INFORMATION = &H400

Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long = 0, _
Optional WindowStyle As VbAppWinStyle = vbMinimizedNoFocus) As Boolean

Dim lInst As Long
Dim lStart As Long
Dim lTimeToQuit As Long
Dim sExeName As String
Dim lProcessId As Long
Dim lExitCode As Long
Dim bPastMidnight As Boolean

On Error GoTo ErrorHandler

lStart = CLng(Timer)
sExeName = ExeFullPath

'Deal with timeout being reset at Midnight
If TimeOutValue > 0 Then
If lStart + TimeOutValue < 86400 Then
lTimeToQuit = lStart + TimeOutValue
Else
lTimeToQuit = (lStart - 86400) + TimeOutValue
bPastMidnight = True
End If
End If

lInst = Shell(sExeName, WindowStyle)

lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)

Do
Call GetExitCodeProcess(lProcessId, lExitCode)
DoEvents
If TimeOutValue And Timer > lTimeToQuit Then
If bPastMidnight Then
If Timer < lStart Then Exit Do
Else
Exit Do
End If
End If
Loop While lExitCode = STATUS_PENDING

ShellandWait = True
Exit Function
ErrorHandler:
ShellandWait = False
End Function

Private Sub Command1_Click()

Dim fh As Integer

If Not ShellandWait("cmd /c Tree c:\dell > c:\dell\rw.txt", , vbHide) Then
MsgBox "Error attempting to start tree.com!", vbExclamation, "Error"
End If

fh = FreeFile
Open "c:\dell\rw.txt" For Input As #fh
Text1.Text = Input(LOF(fh), #fh)
Close #fh
End Sub

-=-=-=-=-=-=-=-=-=-=-= Code ends -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Fix up all of the string literals I used in this example to stuff that
works on your system.

Rdub
Vincent Belaïche
2011-10-14 18:37:34 UTC
Permalink
Ron Weiner <***@NoWare.NutThing> writes:


[...]
Post by Ron Weiner
Fix up all of the string literals I used in this example to stuff that
works on your system.
Rdub
Thanks for the sample code. Maybe I should have made more clear that I
do not launch the VBS script from a VB application, but from a Java
application. The reason why I am interested in VBS is that I used to
launch some MSDOS script before, but MSDOS seems less futureproof.

Vincent.
Thorsten Albers
2011-10-14 20:55:31 UTC
Permalink
...that I used to
launch some MSDOS script before, but MSDOS seems less futureproof.
It depends on what you are talking about:
- MS-DOS as an operating system has already lost its future a longer time
ago now.
- But presumably you are talking about the command line of Windows XP and
later versions of Windows which is _not_ the operating system MS-DOS but
simply a command shell which consist of a console (without graphical user
interface) with a command line and a command interpreter which supports
certain DOS-like commands. There is no reason to think that this one has no
future.
--
Thorsten Albers

gudea at gmx.de
Vincent Belaïche
2011-10-19 17:31:47 UTC
Permalink
Post by Thorsten Albers
...that I used to
launch some MSDOS script before, but MSDOS seems less futureproof.
- MS-DOS as an operating system has already lost its future a longer time
ago now.
- But presumably you are talking about the command line of Windows XP and
later versions of Windows which is _not_ the operating system MS-DOS but
simply a command shell which consist of a console (without graphical user
interface) with a command line and a command interpreter which supports
certain DOS-like commands. There is no reason to think that this one has no
future.
Yes, I was not referring to command.com, but to cmd.exe, the command
line shell.

I do not think that cmd.exe will suddenly disappear, however I don't
think that it will get any new features and evolve. Anyhow VBS is more
flexible and easy to use, even though the syntax is less concise.

Vincent

ralph
2011-10-10 02:45:15 UTC
Permalink
Post by Vincent Belaïche
Hello,
I am creating some sub shell object in the Visual basic script by
Set objShell = WScript.CreateObject("WScript.Shell")
And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to find
how to do this.
Echoing Mr. Weiner it is not clear who (and what kind of app) is
trying to exchange information with who, or when?

There are various ways to provide interprocess communication. It will
be very helpful if you could provide specific details. Perhaps some
kind of flow chart?

-ralph
Eric Coleman
2011-10-14 00:45:59 UTC
Permalink
Post by Vincent Belaïche
Hello,
I am creating some sub shell object in the Visual basic script by
Set objShell = WScript.CreateObject("WScript.Shell")
And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to find
how to do this.
Any idea is welcome.
Vincent.
There is some code on planetsourcecode.com that shows you how to read
data from the console. Just search for 'console' in the vb section, it's
easy to find.
Vincent Belaïche
2011-10-14 18:14:28 UTC
Permalink
Post by Vincent Belaïche
Hello,
I am creating some sub shell object in the Visual basic script by
Set objShell = WScript.CreateObject("WScript.Shell")
And then I am executing some console application. I would like to
redirect the standard out of this console application to the standard
output of the calling visual basic script, but I am desesperate to find
how to do this.
Any idea is welcome.
Vincent.
There is some code on planetsourcecode.com that shows you how to read data from the console. Just search for
console' in the vb section, it's easy to find.
Thanks for the reference, I will have a look at it.

Vincent.
Loading...