streaming the output of a batch file to a text box?

  • Thread starter Thread starter ed
  • Start date Start date
E

ed

Hi all,
I'm very new to vb (2nd day) and I need to create a small app that will
replace my old batch file with a flashy gui. I had some experience
with access 2.0 which helps ;)

What I would like is to get the output of the batch file to display on
the gui as the batch file is running.

if some of you understand unix... I want this: tail -f
/var/log/messages

If it's not possible to stream it, I'd like to find a way to output the
result at the end of the batch file execution.

Last but not least, how do I hide the batch window when it executes so
that the user only sees the gui?

Thank you all,
-Ed
 
Herfried said:

Hi Herfried,
Like I said, I'm pretty new to VB but I don't think this does what I'm
looking for.
From what I can tell it opens a text file with notepad in the bin
directory.

What I am looking for is a way to stream the output of a batch file
into the gui so say my batch had 2 lines

echo Hello
echo World

It would do this:

[batch] "echo Hello" -stream-> [gui] "Hello"
[batch] "echo World" -stream-> [gui] "World"
then the batch closes and the rest of the commands are launched...

Anyone else got an idea?

thanks
 
ed said:
Like I said, I'm pretty new to VB but I don't think this does what I'm
looking for.
directory.

What I am looking for is a way to stream the output of a batch file
into the gui so say my batch had 2 lines

Batch files are run by the command processor (cmd.exe). In order to do
what you want, you need use Process.Start to run cmd.exe and pass it
the appropriate parameters to execute your batch file. The Process
class has a property called RedirectStandardOutput. You would set that
to True and then in your program, you would have to open the standard
output stream to get the output from the batch file.

The RedirectConsole example that Herfried posted shows how to do that.
Although his comments are in german, the code is fairly straight
forward.

Here is another example that executes a batch file and captures its
output:

Using m_Process As New Process()
With m_Process.StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardOutput = True
.Arguments = "/C c:\test\test.bat"
End With

m_Process.Start()
m_Process.WaitForExit(5000)

Dim output As String = m_Process.StandardOutput.ReadLine()
While output <> Nothing
TextBox1.Text &= output & vbCrLf
output = m_Process.StandardOutput.ReadLine()
End While
End Using

If the batch file has errors, they may be displayed on the
StandardError stream instead of the StandardOutput stream. Be sure to
read the docs on the RedirectStandardError property because there is
the possibility of a deadlock if you try to read both the standard
output and error streams at the same time. The docs tell how to avoid
that.

Chris
 
Chris said:
Using m_Process As New Process()
With m_Process.StartInfo
.FileName = "cmd.exe"
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardOutput = True
.Arguments = "/C c:\test\test.bat"
End With

m_Process.Start()
m_Process.WaitForExit(5000)

Dim output As String = m_Process.StandardOutput.ReadLine()
While output <> Nothing
TextBox1.Text &= output & vbCrLf
output = m_Process.StandardOutput.ReadLine()
End While
End Using

If the batch file has errors, they may be displayed on the
StandardError stream instead of the StandardOutput stream. Be sure to
read the docs on the RedirectStandardError property because there is
the possibility of a deadlock if you try to read both the standard
output and error streams at the same time. The docs tell how to avoid
that.

Chris

Many thanks for your help chris... german + no experiance kinda
confised me...

I will do as you say and read the manuals. Thanks a lot for the
example code!

Regards,
-Ed
 
Back
Top