Results 1 to 11 of 11

Thread: [RESOLVED] Need to grab output from the command line

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Resolved [RESOLVED] Need to grab output from the command line

    I have a VB6 program that needs info that I could find by running a command line command. Is there a way I could shell the command, then grab the output (other than writing the output to a file)?

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Need to grab output from the command line

    Shell the program like this:
    Code:
      '
      '
     Shell "path-to-program\program.exe data" '<-- space between .exe and the data
      '
      '
    Then have the other program get the data like this:
    Code:
    Private Form_Load()
     Dim InputData As String
    
     InputData = Command()
    
     If InputData <> "" Then
       '
       ' Process the data
       '
     End If
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to grab output from the command line

    Ok, I think I need to clarify. First of all, I'm not looking to pass data from one executable to another. What I have is a command (command line command) that produces output. For example, if I type "date /t" at the command line, the date is outputted at the command line. How can I do this within a vb6 program and grab the output?

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Need to grab output from the command line

    there are examples posted in code bank for doing this, using named pipe, but the code is quite complex, i have a simple example using scripting that also works,

    are you sure you can not get the information using a windows function or api?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: Need to grab output from the command line


  6. #6

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to grab output from the command line

    I would be very much willing to use an API. There seems to be an API called ReadConsoleOutput, but I can't find any good VB6 examples of how to use it. I see examples done in C++. Any help would be appreciated. I also found this...

    http://www.vbforums.com/showthread.php?t=364219

    But I can't seem to make the code work for me. Also, I have only one line of code to grab. In fact, the command only produces a one word output on the command line. I'm sure there is an easy way to grab it, but I can't figure out how.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Need to grab output from the command line

    i meant an api to get the value, instead of a commandline program or command, but as you have not specified what you are trying to return, it is hard to suggest a method
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Need to grab output from the command line

    you can test this to see if it will work for you
    vb Code:
    1. Dim myobj As Object, strtxt As String
    2. Set myobj = CreateObject("wscript.shell")
    3. Set myobj = myobj.exec("ping www.google.com")  'your shell command
    4. Do
    5.     With myobj
    6.         With .StdOut
    7.             If Not .AtEndOfStream Then
    8.                 strtxt = strtxt & .readall()
    9.             End If
    10.         End With
    11.     End With
    12. DoEvents
    13. Loop While myobj.Status = "WshRunning"
    14. Set myobj = Nothing
    15. MsgBox strtxt  ' ping results displayed in messagebox
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to grab output from the command line

    Quote Originally Posted by westconn1
    you can test this to see if it will work for you
    Westconn1, that is awesome! It works great! I did have to add "cmd /c" before listing my executable... like this:

    Set myobj = myobj.exec("cmd /c c:\myDosProgram.exe")

    Thank you so much for your help!

  10. #10

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Need to grab output from the command line

    Westconn1, is there a way to hide the command window? If not, it's not a big deal, but if it's doable, please let me know.

    Thanks!

  11. #11
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Need to grab output from the command line

    dunno you could set your form to topmost or appactivte may be hide the dos box, immediately after myob.exec, never really tried
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width