473,396 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reading Output from Shell Command

Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that this program returns. I'm just missing the stepshere. I know that I can set the Shell command to an integer,but this only returns a 0 to me telling me that it executed, notwhat is being returned to the console by that application. Isthere a way to find out this information?

Thanks!

--------------------------------
From: Kevin Mansel

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>HMrdIkohokioRR4woWbZSQ==</Id>
Nov 21 '05 #1
4 15029
LOL! For the past few months, I've been developing a VB.NET app that acts as
a front end to several PERL apps. I use the shell command (in a thread) to
start a PERL app and in one command pass all of the command line arguments
(values from Textboxes, comboboxes, etc.) to the PERL App.

Then, using the sledgehammer approach, I setup a TCPListener in a thread to
read the output from the perl app(Of course, I had to add the TCPClient
functionality to all of my PERL apps!!)

For some reason, I just never thought about simply redirecting the output of
the command window itself.

I'll have to make a small test app to see if redirecting the output (as you
stated) works as well as the TCPListener/TCPclient setup.

In some of my initial attempts at controlling the PERL app from a VB App, I
noticed that SendKeys does not work if the command window is hidden. Will
redirecting the output work if the command window is hidden?

regards,
Lee

"Joseph MCAD" <an*******@microsoft.discussions.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
April 7, 2005

You have to set the Process.Startinfo.RedirectStandardOutput property.
You can then access the Process.StandardOutput to retrieve a Streamreader
that has the output....

dim p as new process
p.startinfo.filename = "..."
p.startinfo.redirectstandardoutput = true
p.start
dim reader as new streamreader = p.standardoutput
messagebox.show(reader.readtoend)

Hope this helps! :-)

Joseph MCAD

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
"Kevin Mansel via .NET 247" <an*******@dotnet247.com> schrieb:
Ok, basically this is my problem. I'm building a console app to call a
dos program. So i'm using the Shell command to call the program, now
depending on what happens, I want to read the output that this program
returns.


<URL:http://dotnet.mvps.org/dotnet/samples/misc/RedirectConsole.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Nov 21 '05 #2
April 7, 2005

I think you are asking whether redirecting the output will still work if
you specify for the black window to not show up visible. If so, the answer
is yes. To not make the window show use...

p.Startinfo.UseShellExecute = false
p.startinfo.createnowindow = true

I forgot to mention to call p.WaitForExit before showing the output or
closing the output stream reader. So...

p.Start
dim reader as streamreader = p.standardoutput
p.waitforexit
messagebox.show(reader.readtoend)

In case you don't know you specify the parameters to the .exe using the
arguments property...

p.filename = "....exe"
p.Arguments = "-n -r /e flags"

Glad to hear this helped!

Joseph MCAD

"lgbjr" <lg***@online.nospam> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
LOL! For the past few months, I've been developing a VB.NET app that acts
as a front end to several PERL apps. I use the shell command (in a thread)
to start a PERL app and in one command pass all of the command line
arguments (values from Textboxes, comboboxes, etc.) to the PERL App.

Then, using the sledgehammer approach, I setup a TCPListener in a thread
to read the output from the perl app(Of course, I had to add the TCPClient
functionality to all of my PERL apps!!)

For some reason, I just never thought about simply redirecting the output
of the command window itself.

I'll have to make a small test app to see if redirecting the output (as
you stated) works as well as the TCPListener/TCPclient setup.

In some of my initial attempts at controlling the PERL app from a VB App,
I noticed that SendKeys does not work if the command window is hidden.
Will redirecting the output work if the command window is hidden?

regards,
Lee

"Joseph MCAD" <an*******@microsoft.discussions.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
April 7, 2005

You have to set the Process.Startinfo.RedirectStandardOutput property.
You can then access the Process.StandardOutput to retrieve a Streamreader
that has the output....

dim p as new process
p.startinfo.filename = "..."
p.startinfo.redirectstandardoutput = true
p.start
dim reader as new streamreader = p.standardoutput
messagebox.show(reader.readtoend)

Hope this helps! :-)

Joseph MCAD

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
"Kevin Mansel via .NET 247" <an*******@dotnet247.com> schrieb:
Ok, basically this is my problem. I'm building a console app to call a
dos program. So i'm using the Shell command to call the program, now
depending on what happens, I want to read the output that this program
returns.

<URL:http://dotnet.mvps.org/dotnet/samples/misc/RedirectConsole.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>



Nov 21 '05 #3
Joseph,

Understood. while I have your attention, I think I'll pick your brain a bit,
if that's ok. Before I start creating test apps and possible changing my
main app, let me tell you a bit about what the app does, and see if you
think switching to a redirect and streamreader is the way to go.

1. There are six different PERL apps that are controlled by the VB app, plus
some other funtionality in the VB app. And, each of the PERL apps is run in
a seperate thread, so all apps can be run simultaneously if desired. Some of
the PERL apps can take up to an hour to run depending on the amount of input
data.

2. When the TCPListener receives a string, it passes it to a function that
uses several regex expressions to determine where the string should be
displayed in the VB form. Most of the strings are sequentially added to a
listbox. some of the strings are used to trigger progress on a progress bar,
and some of the strings go to different labels telling the user more
specifically what tasks are actually being completed as the PERL app runs.

3. Occasionally, due to faulty input data, the PERL app will die, and in
this case the last string that is passed to the Listener provides
information regarding the error cause. In this case, the VB app terminates
the PERL process and informs the user of the specific error. another
complication to this part is that some of the PERL apps use OLE to write
data to an Excel spreadsheet. If the PERL app dies, I clean up not only the
PERL app, but the Excel process as well.

So, based on this, do you think I can just pass the streamreader output to
my regex function? Do you think that there is any speed or stability
difference between using a socket to transfer the data rather than the
streamreader?

TIA
Lee


"Joseph MCAD" <an*******@microsoft.discussions.com> wrote in message
news:ue**************@TK2MSFTNGP09.phx.gbl...
April 7, 2005

I think you are asking whether redirecting the output will still work if
you specify for the black window to not show up visible. If so, the answer
is yes. To not make the window show use...

p.Startinfo.UseShellExecute = false
p.startinfo.createnowindow = true

I forgot to mention to call p.WaitForExit before showing the output or
closing the output stream reader. So...

p.Start
dim reader as streamreader = p.standardoutput
p.waitforexit
messagebox.show(reader.readtoend)

In case you don't know you specify the parameters to the .exe using the
arguments property...

p.filename = "....exe"
p.Arguments = "-n -r /e flags"

Glad to hear this helped!

Joseph MCAD

"lgbjr" <lg***@online.nospam> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
LOL! For the past few months, I've been developing a VB.NET app that acts
as a front end to several PERL apps. I use the shell command (in a
thread) to start a PERL app and in one command pass all of the command
line arguments (values from Textboxes, comboboxes, etc.) to the PERL App.

Then, using the sledgehammer approach, I setup a TCPListener in a thread
to read the output from the perl app(Of course, I had to add the
TCPClient functionality to all of my PERL apps!!)

For some reason, I just never thought about simply redirecting the output
of the command window itself.

I'll have to make a small test app to see if redirecting the output (as
you stated) works as well as the TCPListener/TCPclient setup.

In some of my initial attempts at controlling the PERL app from a VB App,
I noticed that SendKeys does not work if the command window is hidden.
Will redirecting the output work if the command window is hidden?

regards,
Lee

"Joseph MCAD" <an*******@microsoft.discussions.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
April 7, 2005

You have to set the Process.Startinfo.RedirectStandardOutput property.
You can then access the Process.StandardOutput to retrieve a
Streamreader that has the output....

dim p as new process
p.startinfo.filename = "..."
p.startinfo.redirectstandardoutput = true
p.start
dim reader as new streamreader = p.standardoutput
messagebox.show(reader.readtoend)

Hope this helps! :-)

Joseph MCAD

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
"Kevin Mansel via .NET 247" <an*******@dotnet247.com> schrieb:
>Ok, basically this is my problem. I'm building a console app to call a
>dos program. So i'm using the Shell command to call the program, now
>depending on what happens, I want to read the output that this program
>returns.

<URL:http://dotnet.mvps.org/dotnet/samples/misc/RedirectConsole.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>



Nov 21 '05 #4
April 8, 2005

First, I don't know anything about Perl. Second, you can definitely pass
the streamreader output to the regex function, BUT you have to make sure
that the output has come back from the program before you do so. If the
string comes from the output, say about in 1/2 hour, then you might have to
continue to test whether the output you need has come back yet. As far as a
difference in performance, my view is that with sockets you are actually
sending data through a network (even if it is just on the same machine) and
therefore negiotiating connections possibly. Therefore I think it is much
better to do it with streamreaders that don't go over a network. Even if
there is no negiotiating, your program is still having to open ports,
connect, send responses back and forth... This is just my opinion and I
like having my brain picked! :-)

Joseph MCAD


"lgbjr" <lg***@online.nospam> wrote in message
news:OI*************@TK2MSFTNGP15.phx.gbl...
Joseph,

Understood. while I have your attention, I think I'll pick your brain a
bit, if that's ok. Before I start creating test apps and possible changing
my main app, let me tell you a bit about what the app does, and see if you
think switching to a redirect and streamreader is the way to go.

1. There are six different PERL apps that are controlled by the VB app,
plus some other funtionality in the VB app. And, each of the PERL apps is
run in a seperate thread, so all apps can be run simultaneously if
desired. Some of the PERL apps can take up to an hour to run depending on
the amount of input data.

2. When the TCPListener receives a string, it passes it to a function that
uses several regex expressions to determine where the string should be
displayed in the VB form. Most of the strings are sequentially added to a
listbox. some of the strings are used to trigger progress on a progress
bar, and some of the strings go to different labels telling the user more
specifically what tasks are actually being completed as the PERL app runs.

3. Occasionally, due to faulty input data, the PERL app will die, and in
this case the last string that is passed to the Listener provides
information regarding the error cause. In this case, the VB app terminates
the PERL process and informs the user of the specific error. another
complication to this part is that some of the PERL apps use OLE to write
data to an Excel spreadsheet. If the PERL app dies, I clean up not only
the PERL app, but the Excel process as well.

So, based on this, do you think I can just pass the streamreader output to
my regex function? Do you think that there is any speed or stability
difference between using a socket to transfer the data rather than the
streamreader?

TIA
Lee


"Joseph MCAD" <an*******@microsoft.discussions.com> wrote in message
news:ue**************@TK2MSFTNGP09.phx.gbl...
April 7, 2005

I think you are asking whether redirecting the output will still work
if you specify for the black window to not show up visible. If so, the
answer is yes. To not make the window show use...

p.Startinfo.UseShellExecute = false
p.startinfo.createnowindow = true

I forgot to mention to call p.WaitForExit before showing the output or
closing the output stream reader. So...

p.Start
dim reader as streamreader = p.standardoutput
p.waitforexit
messagebox.show(reader.readtoend)

In case you don't know you specify the parameters to the .exe using the
arguments property...

p.filename = "....exe"
p.Arguments = "-n -r /e flags"

Glad to hear this helped!

Joseph MCAD

"lgbjr" <lg***@online.nospam> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
LOL! For the past few months, I've been developing a VB.NET app that
acts as a front end to several PERL apps. I use the shell command (in a
thread) to start a PERL app and in one command pass all of the command
line arguments (values from Textboxes, comboboxes, etc.) to the PERL
App.

Then, using the sledgehammer approach, I setup a TCPListener in a thread
to read the output from the perl app(Of course, I had to add the
TCPClient functionality to all of my PERL apps!!)

For some reason, I just never thought about simply redirecting the
output of the command window itself.

I'll have to make a small test app to see if redirecting the output (as
you stated) works as well as the TCPListener/TCPclient setup.

In some of my initial attempts at controlling the PERL app from a VB
App, I noticed that SendKeys does not work if the command window is
hidden. Will redirecting the output work if the command window is
hidden?

regards,
Lee

"Joseph MCAD" <an*******@microsoft.discussions.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
April 7, 2005

You have to set the Process.Startinfo.RedirectStandardOutput property.
You can then access the Process.StandardOutput to retrieve a
Streamreader that has the output....

dim p as new process
p.startinfo.filename = "..."
p.startinfo.redirectstandardoutput = true
p.start
dim reader as new streamreader = p.standardoutput
messagebox.show(reader.readtoend)

Hope this helps! :-)

Joseph MCAD

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ee**************@TK2MSFTNGP15.phx.gbl...
> "Kevin Mansel via .NET 247" <an*******@dotnet247.com> schrieb:
>>Ok, basically this is my problem. I'm building a console app to call a
>>dos program. So i'm using the Shell command to call the program, now
>>depending on what happens, I want to read the output that this program
>>returns.
>
> <URL:http://dotnet.mvps.org/dotnet/samples/misc/RedirectConsole.zip>
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lecichy | last post by:
Hello. As in the topic, I use www to execute shell command, in this case 'ls -l' and i get output like: total 8 drwx------ 11 lecichy staff 4096 Oct 15 18:18 Maildir drwx---r-x 3...
3
by: Andreas Paasch | last post by:
According to the manual for PHP, I should be able to run a shell command within php. I'm trying to copy some php files from one location to another one using exec() but fail. ...
4
by: Yann.K | last post by:
Hello. Using Tkinter, i would create a widget which display a shell command return. This return is long, and i would display a real time display (like with the tail -f commande on Linux) I...
4
by: rkoida | last post by:
Hello evryone I am a newbie to python. I have a makefile which i can compile in UNIX/LINUX, But i I am planning to write a python script which actually does what my MAKEFILE does. The make file...
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
8
by: zhiwei wang | last post by:
I remember that there is a function that could invoke shell command such as "rm" "cp", directly in .c file. But I could not recall its name, and I googled with nothing meaningful. I vaguely...
9
by: Tommy Lu | last post by:
Hi, wondering if there is a way to interact the shell command with the C# program? For example, if I type c:\>ver it then suppose to return the version of the OS I am currently using... or ...
0
by: floele | last post by:
Hi. I try to create a small program to handle the FLAC <http://flac.sourceforge.net/> (commandline) encoder or decoder and intend to read its command line output during the process like that: ...
3
by: Donald Duck | last post by:
I'm a little bit confused about what is the best way to run a shell command, if I want to run a command like xxxxxx -a -b yyyyyy where I'm not interested in the output, I only want to make...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.