redirect output of a CMD to file

Reply

Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

redirect output of a CMD to file

 
0
  #1
Nov 18th, 2008
Hi There,

I have a third party EXE file (on which I have no control of any modification) which invokes a new CMD window in order to execute itself. In other words it a windows console application.

Now it executes with some switches for runtime options

<exe_name> /opt1 "param" /op2
All I want is if by any means I can capture the New Console output forked by this exe. Right now simple redirect as

<exe_name> /opt1 "param" /op2 > File.txt
cannot work.

Please help me out in order to obtain the output of the new CMD window as explained above.
Last edited by shouvik.d; Nov 18th, 2008 at 3:21 am. Reason: Inserting Inline Code instead of Code wrapper
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: redirect output of a CMD to file

 
0
  #2
Nov 18th, 2008
How does this work. Does the exe open up an xterm window and just run a command in there or are we looking at a full-blown gui?

Thanks,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: redirect output of a CMD to file

 
0
  #3
Nov 19th, 2008
Originally Posted by eggi View Post
Does the exe open up an xterm window and just run a command in there or are we looking at a full-blown gui?
Actually this is a Windows Console application so no question of xterm. And to answer your question it is an exe which will create in runtime a batch file and call the same. I cannot call the batch separately because it gets created in runtime only when the exe is called and thereafter deleted. The exe takes care to create it with the user provided options and then call it, complete the run and then delete this batch file.

Conventionally I can redirect an output of the batch but since the creation is only with the help of that of the exe hence I cannot do the same.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: redirect output of a CMD to file

 
0
  #4
Nov 20th, 2008
Hey there,

I found this on the web - it's specific to perl, but you can use the /C flag to capture that output from the exe file, I believe. If it works, thank the guy who figured it out

http://community.activestate.com/for...ut-system-call

Bug in Windows shell redirection and file associations
mmmann | Tue, 2008-08-12 08:14

You cannot redirect or pipe output using cmd.exe when the Perl program is started using the Windows' file association(s) and the Perl program executes programs using open3 or system. Using a test.pl program that merely does a system(qw(netstat -an)), try this:

C:> perl test.pl > out # works
C:> test.pl > out # broken, and leaves "zombie" netstat executable

The work-around is to alter the file association:

Windows-E to bring up an explorer window
Tools -> Folder Options
Go to the [File Types] tab
Type 'PL' to move list box to PL extension
Click [Advanced]
Click [Edit] to edit the Open command

The "application used to perform action" reads, by default:

"C:\Perl\bin\perl.exe" "%1" %*

Overwrite it with the following:

C:\WINDOWS\System32\command.com /C C:\Perl\bin\perl.exe "%1" %*

...i.e. add command.com /C at the beginning and remove the double quotes around the perl executable. Yes, command.com, not cmd.exe.

This appears to be fixed in Vista.

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: redirect output of a CMD to file

 
0
  #5
Nov 20th, 2008
Thanks Mike,

I'll have a try and let you know. Thanks a lot for digging it out for me
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: redirect output of a CMD to file

 
0
  #6
Nov 21st, 2008
Happy to help

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: redirect output of a CMD to file

 
0
  #7
Dec 31st, 2008
Hi Mike,

This thing wont work as the EXE i am using does not provide any output. It in turn creates a .bat file with necessary contents from the command line options provided to the EXE and then calls it. After that the batch file is deleted. So it is only through the EXE i can have the batch being run.

Hence you see a new CMD window opens by this EXE which runs the batch file content. Now I want to capture the contents of the batch run from that particular bat file.

In the above example provided by you, netstat.exe itself will provide an output. It does not call any other batch file hence the output is captured but it is not the case with me.

Let me know if you find out the solution to my problem
Last edited by shouvik.d; Dec 31st, 2008 at 4:13 am. Reason: Providing more clarity to the problem
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 200
Reputation: shouvik.d is an unknown quantity at this point 
Solved Threads: 6
shouvik.d's Avatar
shouvik.d shouvik.d is offline Offline
Posting Whiz in Training

Re: redirect output of a CMD to file

 
0
  #8
Jan 1st, 2009
Hi Mike,

It seems it cannot be done. When an application is using a bat file to run a set of DOS commands windows will use a named pipe for the CMD.exe window. Now even if you can get the handle of the window by ensuring that the PPID of the CMD.exe is the calling application's PID, you still cannot intrude in the pipe. This is the inherent characteristic of any OS. They would not allow you to hook to the handle one the process is created. So I think it is impossible to do what I was thinking of.

Correct me if I am wrong.
Regards
Shouvik
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: redirect output of a CMD to file

 
0
  #9
Jan 2nd, 2009
The only thing I can think to try is to find exactly where the .bat file is created and remove 'DELETE' privilege from that directory for all users (via Properties, Security tab and Advanced button. That *might* prevent the program from deleting the .bat script.

Or rename cmd.exe to cmd-real.exe and create cmd.bat that makes a copy of the script or program to be run (if specified) before calling cmd-real.exe to execute the program or script.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC