Capturing the output of a DOS program that is launched?

Is there a way that I can start a DOS program and capture it's output so I can process it when it completes?

I am using VB6.0 SP3 and running on Windows 95/98 systems (may be on NT as well later). I need to use ShellExecute, Shell or something to execute the DOS program (got this part working) and wait for it to finish (got this working too) then read all the output it created and process it.

I know that the output can be redirected when the program is started from the command line (ie. C:\Something.exe > capture.txt) but is there a way to get this to happen when the program is started using say the SHELL command?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Which DOS commands do you want to execute ?
option Explicit

private Sub Form_Click()
    Dim sCommand as string, i as Integer
    ' execute command
    sCommand = "Ping "
    Shell " /c " & sCommand & " > c:\info.txt"
    ' wait
    for i = 1 to 10000
    ' show file
    Shell "notepad c:\info.txt", vbNormalFocus
End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
schworakAuthor Commented:
Can you tell me why that works but just doing

Shell "Ping > myfile.txt"

Doesn't work?

I faked the same type of result by creating a dummy batch file on the fly that runs the program I am after. But it seemed so bulky.
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

schworakAuthor Commented:
Oh, I am going to leave this question open just a bit longer in case someone has another answer. If not you will get the points AMEBA.
>why that works
You are not starting normal Windows program and you need '' or other interpreter - Environ("COMSPEC")

You can find out what is '':
   sCommand = "Ping "
replace with:
   sCommand = "command /?"
Hi schworak
Try this code.
One important thing - change property
of dummy "mybat.bat" batch file on "Exit" when finish (for Win95/98 only).

Option Explicit
'Wait Program Termination DLL Declarations
 Declare Function OpenProcess Lib "kernel32" _
                 (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
                  ByVal dwProcessId As Long) As Long
 Declare Function WaitForSingleObject Lib "kernel32" _
                  (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Command1_Click
 Dim sSendCommand As String
 Dim sBatFilePath As String
sSendCommand = "xcopy test1.txt test.txt > logfile.txt"
 sBatFilePath = App.Path & "\mybat.bat"
 Open sBatFilePath For Output As #1
     Print #1, sSendCommand
 Close #1
 Call fShellTerminated(sBatFilePath)
End Sub

Public Sub fShellTerminated(ByVal sCommand As String)
'Start Foreign App and Wait Termination
 Dim hprog, hProc, RetVal As Long
 Const SYNCHRONIZE = &H100000
 On Error GoTo Err_fShellTerminated

'Start foreign program
 hprog = Shell(sCommand, vbHide) 'returns taskID

'Get process handle
 hProc = OpenProcess(SYNCHRONIZE, False, hprog)

'Wait until the process terminates
 If hProc <> 0 Then
   RetVal = WaitForSingleObject(hProc, INFINITE)
   CloseHandle hProc
 End If

'Normal Exit
 Exit Sub
'Error Handler
End Sub

schworakAuthor Commented:
I already had the code to make me wait. I am using the /c method. Thanks though
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.