473,373 Members | 1,138 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,373 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 15022
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.