Thread: Gedt reult from Command line exe.

1. Gedt reult from Command line exe.

I have a DOS program that runs from the Comamnd line and processes the file added to the name e.g
D:\Flacs>aucdtect "Track01 (9).wav"
I could Shell to this from VB6 but would need to wait for the DOS program to finish then somehow capture it's result (which is printed to the console window) and repeat the process for a batch of files.
Is such a thing possible in VB6 without being overly complicated?

2. Re: Gedt reult from Command line exe.

You can call shell and wait for it to finish, do a quick search for ShellAndWait.

You could also redirect ( > ) your results to a file instead of on the DOS window, for example:
Code:
D:\Flacs>aucdtect "Track01 (9).wav" > "C\some folder\Results.txt"
Then after the ShellAndWait informs you it's done, read the text file and delete it. Just thinking out loud for an easy-ish solution.

If running a batch, you can use >> vs > in the redirection. That should append vs create new file.

Testing the redirection can be done easy enough, before you even look for ShellAndWait examples.

3. Re: Gedt reult from Command line exe.

Thanks again LaVolpe that's a brilliant idea. I'll look for Shell&Wait and see how I go...

4. Re: Gedt reult from Command line exe.

I found this on a search here but it doesn't work (yet?)
Code:
Private Function ShellAndWait(CmdLine As String) As Long
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret&

'
' Initialize the STARTUPINFO structure:
'
start.cb = Len(start)

'
' Start the shelled application:
'
ret& = CreateProcessA(0&, CmdLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

If ret = 0 Then
ShellAndWait = 0
Else
'
' Wait for the shelled application to finish:
'
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)

ShellAndWait = 42
End If
End Function
The problem is Line 2 Dim proc As PROCESS_INFORMATION gives UserDefined Type not defined.
I'm guessing this wants a reference and I've struck this before. Apart from Google, is there a better way to find what reference something wants?
Also the parameter passed is called "CmdLine". Is this function all you need, and no need for SHELL ?

5. Re: Gedt reult from Command line exe.

On this forum, an example with a short discussion:

6. Re: Gedt reult from Command line exe.

May have struck a glitch... in that link (which I've changed to) it runs Notepad and continues when notepad is click closed. The program I want isn't closed by clicking, it just ends. Will vb6 know that's happened ?

Couple of other things, the amendments by Wokawidget uses a boolean in Form_Unload. I don't have a Form. Will DoEvents cause a problem as he describes ? I haven't compiled it yet. Mainly as Results.txt isn't being written.
So far I have:
Code:
 Sub test5()

Dim nStart As Date
Dim a As String
a = "D:\flac\aucdtect.exe test.wav > C:\Temp\Results.txt"
nStart = Now

ShellAndWait a, vbNormalFocus, True

MsgBox "You spent " & DateDiff("s", nStart, Now) & _

End Sub
I did have some errors with the quotes in a (and they may still be wrong).

7. Re: Gedt reult from Command line exe.

You can try this ShellAndReturnOutput function:

thinBasic Code:
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Click()
Print ShellAndReturnOutput("cmd /c dir")
End Sub
Private Function ShellAndReturnOutput(sCommand As String, Optional ByVal UseDoEvents As Boolean) As String
With CreateObject("WScript.Shell").Exec(sCommand)
Do While .Status = 0 '--- running
If UseDoEvents Then
DoEvents
Else
Call Sleep(1)
End If
Loop
End With
End Function
Unfortunately there is no way to hide the black console of the spawned process using WshScriptExec object. For that you'll need a WshScriptExec replacement class like this cExec.cls which implements StartHidden parameter on its Run method.

cheers,
</wqw>

9. Re: Gedt reult from Command line exe.

wqweto, thank you for that code. It certainly nicely returns the directory but I cannot get it to run my file.
If I go to the DOS window, at the "D:/flac" prompt I type "aucdtect test.wav" and it processes the file and reports a result.
But I can't emulate that from vb6. aucdtect runs and returns Usage options (like you entered the ext name with no file to process)
My results are
Debug.Print ShellAndReturnOutput("D:\Flac aucdtect test.wav") => Access is denied error
Debug.Print ShellAndReturnOutput("D:\Flac\aucdtect test.wav") => info screen only. Same as ShellAndReturnOutput("D:\Flac\aucdtect")

Am I doing something wrong?

10. Re: Gedt reult from Command line exe.

Try this

ChDir "D:\path\to\test_wav_folder"
ShellAndReturnOutput("D:\flac\aucdtect.exe test.wav")

or even better pass the full pathname

ShellAndReturnOutput("D:\flac\aucdtect.exe D:\path\to\test.wav")

cheers,
</wqw>

11. Re: Gedt reult from Command line exe.

Yes that worked. But the program now takes several minutes to work, as opposed to a few seconds.
That's if it does finish at all, as the DOS window stayed on screen with an hourglass and I had to close it manually. Then it printed the results
to the debug window over multiple lines with every line twice). Tried 3 tests of varying durations. None finished or returned to vb.
So close! Maybe it can be corrected, I don't know. But thanks for your input.

12. Re: Gedt reult from Command line exe.

Did you try setting the UseDoEvents parameter like this

ShellAndReturnOutput("D:\flac\aucdtect.exe D:\path\to\test.wav", UseDoEvents:=True)

cheers,
</wqw>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•