Avatar of Amadork
Amadork

asked on 

How do you capture the output of a DOS command like "ECHO" in VB.net?

The code below receives an error because the file "echo Hello" can not be found.

How do I run something like this in VB.net and capture the output?

I would like to run something like the following:

setlocal
SET MYVAR=ABCDEFG
SET MYVAR2=DEFGHIJ
RUNMYBAT.BAT
endlocal

I could put this in a batch file and execute the batch file but I am trying to avoid that. It is also important that all the command shares the same  "shell" since I will be setting temporary environmental variables.
Dim strCmd As String = "echo Hello"
        Dim strErr, strOut As String
 
        ' Set start information.
        Dim start_info As New ProcessStartInfo(strCmd)
        start_info.UseShellExecute = False
        start_info.CreateNoWindow = True
        start_info.RedirectStandardOutput = True
        start_info.RedirectStandardError = True
 
        ' Make the process and set its start information.
        Dim proc As New Process
        proc.StartInfo = start_info
 
 
        ' Start the process.
        proc.Start()
 
        ' Attach to stdout and stderr.
        Dim std_out As StreamReader = proc.StandardOutput()
        Dim std_err As StreamReader = proc.StandardError()
 
        ' Display the results.
        strErr = std_out.ReadToEnd()
        strOut = std_err.ReadToEnd()
 
        ' Clean up.
        std_out.Close()
        std_err.Close()
        proc.Close()
 
        MsgBox(strErr)
        MsgBox(strOut)

Open in new window

Visual Basic ClassicVisual Basic.NET

Avatar of undefined
Last Comment
Mi4night
Avatar of Serge Fournier
Serge Fournier
Flag of Canada image

maybe this can help you

it's capturing the error from cacls in vbscript

i dont know if it can capture a text, but it should get you on the way to find how to capture more

      a="CACLS """ & fol01(i) & """ /T /E /C /G """ & dat01 & ""
      err01 = objShe.Run("%COMSPEC% /c Echo Y| " & a, 2, True)
      If Err01 <> 0 Then
         err02=err02 & "ERROR assigning permissions for user " & dat01 & " to folder " & fol01(i) & vbcrlf
      else
         err02=err02 & "permission set for user " & dat01 & " to folder " & fol01(i) & vbcrlf
      End If

Open in new window

i can do some test, if you are too lazy ;P
Avatar of Amadork
Amadork

ASKER

If there isn't a way to capture the output in VB.net, then its not what I'm looking for.

If the run command in vbscript is anything like the run command in vb.net, you can't capture that ouput without redirecting the DOS prompt, which really won't accomplish what I am looking for.

And, no, I'm not "too lazy" to test it. I've been searching the net for a good 2 hours trying to find what I'm looking for.

In case someone knows how to convert it, here is a function in vbscript that does what I want.


Sub logExec(strCommand)
'INPUT: strCommand = A command to run at a DOS prompt
'OUTPUT: The output of the resulting command sent to both the screen and the log file.
'        This captures both the stdout stream and the stderr stream
   dim objShell, objExecObject, strOutput
   WScript.Echo strCommand
   writelog "logExec", "Command: " & strCommand
   Set objExecObject = oShell.Exec(strCommand)
   writelog "logExec", "-------- Start LogExec Output --------" 
   Do Until objExecObject.StdOut.AtEndOfStream
      strOutput = objExecObject.StdOut.ReadLine()
      writelog "logExec", strOutput
      WScript.Echo strOutput
   loop
   Do Until objExecObject.StdErr.AtEndOfStream
      strOutput = objExecObject.StdErr.ReadLine()
      writelog "logExec", strOutput
      WScript.Echo strOutput
   loop
   writelog "logExec", "-------- End LogExec Output --------" 
end sub

Open in new window

i always tought vbscript was really easy to convert in vb...
(i converted a lot of vba to vb and vb to vba and vb to vbscript)

just add a few dim for variables and for objects
add the variables types after the dim(s)
add the libraries before compiling (windows shell (maybe just windows scripts))

i cannot do it right now, my visual studio is not installed here, just at my job :(
ASKER CERTIFIED SOLUTION
Avatar of Doug
Doug
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Doug
Doug
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Mi4night
Mi4night

im not into vb.net but a good way would be to send the output to a text file and then call it

example:
dont know if you can use Shell in vb .net
shell "netstat -an > textfile.txt"

then you have to load the file and thats more easier i thing :D
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo