-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
I needed to do some cmd stuff today and found this thread ;) useful. I ended up embedding the example in this post into a class which makes it easy to use. At least I think so...
Code:
Class CMDThread
Public Param As String
Public isFinished As Boolean = False
Private tr As Thread = Nothing
Private results As String = ""
Private myprocess As New Process
Private StartInfo As New System.Diagnostics.ProcessStartInfo
Public Function Start() As Thread
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
tr = New Thread(AddressOf Me.work)
tr.Start()
Return tr
End Function
Public Sub Join()
tr.Join()
End Sub
Public Function GetOutput() As String
GetOutput = results
results = "" 'Not sure if this is safe while thread is executing... seem to work
End Function
Private Sub work()
myprocess.StandardInput.WriteLine(Param & vbCrLf & "exit") 'the command you wish to run, with an exit at the end to terminate process after run
While myprocess.StandardOutput.EndOfStream = False
results += myprocess.StandardOutput.ReadLine() & vbCrLf
End While
isFinished = True
End Sub
End Class
To launch a couple of threads and use join to wait for the last one:
Code:
Dim threadA, threadB As New CMDThread
Dim res As String = ""
threadA.Param = "ping -n 2 127.0.0.1"
threadB.Param = "tracert -h 10 -d -w 30 vbforums.com"
threadA.Start()
threadB.Start()
While Not threadA.isFinished
res = threadA.GetOutput
...do stuff...
End While
'A is finished
If Not threadB.isFinished Then
threadB.Join() 'wait until B is finished
End If
res = threadB.GetOutput
Cheers!
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Hello,
This thread has been a huge help to me. I wanted to do something very simple with the CMD prompt that would save me a ton of time. I busted out my 10 year old VB book and felt a bit overwhelmed about how much I had forgotten.
Thanks for the code, it rules. I am having one small snytax issue which I'm sure you guys could knock out of the park.
I am trying to use the txt box to define a variable, then run some commands using that variable. This will be used to check SRV records for SIP domains.
So "nslookup -quertype=srv _h323ls._udp.SIPDOMAIN" Where SIPDOMAIN is pulled from the txtcommand box.
Code:
Private Sub CMDAutomate()
Dim sipdomain As TextBox = txtCommand
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("nslookup -querytype=srv _h323ls._udp.", &sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _h323cs._tcp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _sip._tcp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _sip._udp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("nslookup -querytype=srv _sips._tcp.", sipdomain) 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
Also it seems that it hangs after one run and I have to restart to run a 2nd command each time.
Thanks for your time!
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
ok Fixed my issue with the help of Teycho0 on #VB on DALnet, thanks bud.
vb Code:
Private Sub CMDAutomate()
'Dim sipdomain As TextBox = txtCommand
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("nslookup -querytype=srv _h323ls._udp." & txtCommand.Text) 'Checks the H323ls SRV record
SW.WriteLine("nslookup -querytype=srv _h323cs._tcp." & txtCommand.Text) 'Checks the H323cs SRV record
SW.WriteLine("nslookup -querytype=srv _sip._tcp." & txtCommand.Text) 'Checks the sip.tcp SRV record
SW.WriteLine("nslookup -querytype=srv _sip._udp." & txtCommand.Text) 'Checks the sip.udp SRV record
SW.WriteLine("nslookup -querytype=srv _sips._tcp." & txtCommand.Text) 'Checks the sips.tcp SRV record
SW.WriteLine("exit") 'exits command prompt window
Results = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
-
1 Attachment(s)
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Thank you for the great little piece of code I have a project that currently pops the cmd and bugs the crap out of me. I took the code for VS10 and created a new project for VS11 for what ever reason the VS10 example would not open the solution or project :ehh: I tested it with dir, netdom, and a couple others all seem to work as the code was written for. It should be attached to this post.
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application
Quote:
Originally Posted by
gigemboy
The below code adds one line, with the "CreateNoWindow" property of the StartInfo object set to "True", this way the command window doesnt open up at all... (Thanks to jmcilhinney)
VB Code:
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True '<---- creates no window, obviously
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
SW.WriteLine("exit") 'exits command prompt window
txtResults.Text = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
i'm nt able to get the result of the command window on my form
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application
Quote:
Originally Posted by
sanjeev467
i'm nt able to get the result of the command window on my form
Plz someone answer it
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
i'm nt able to get the output of command window to my form plz sm1 help
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
You may want to create your own thread and reference this one. Also may want to actually describe your problem vs staating "it doesnt work" ;)
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Quote:
Originally Posted by
RobDog888
You may want to create your own thread and reference this one. Also may want to actually describe your problem vs stating "it doesnt work" ;)
yes it doesn't work, i've tried many times
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
i have tried the following code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
path = "javac " + TextBox1.Text + ".java"
runpath = "java " + TextBox1.Text
TextBox2.Text = "C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes\" + TextBox1.Text
Dim sf As New System.IO.StreamWriter(TextBox2.Text & ".java")
sf.Write(RichTextBox1.Text)
sf.Close()
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CheckForIllegalCrossThreadCalls = False
CMDThread.Start()
End Sub
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("set path=C:\Program Files\Java\jdk1.7.0\bin") 'the command you wish to run.....
SW.WriteLine("cd C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes\") 'the command you wish to run.....
SW.WriteLine(path) 'the command you wish to run.....
MsgBox(path)
StartInfo.FileName = "cmd" 'starts cmd window
SW.WriteLine(runpath)
'RichTextBox2.Text = SR.ReadToEnd 'returns results of the command window
SW.WriteLine("exit")
RichTextBox2.Text = SR.ReadToEnd 'returns results of the command window
SW.Close()
SR.Close()
End Sub
while debugging i wrote a java code and saved it and have run it
and i got the following output:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\sanjeev\Documents\Visual Studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug>set path=C:\Program Files\Java\jdk1.7.0\bin
C:\Users\sanjeev\Documents\Visual Studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug>cd C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes\
C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes>javac H.java
C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes>java H
Hello to all
C:\Users\sanjeev\Desktop\sanju\SEM-VII\Cloud\javacodes>exit
but i want only "Hello to all" as the output and nothing else so wat shall i do
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Hey, I am having the same issue as bvrider1 and Sanjeev467.
No matter what I do, even when I just download the zip files and run them without changing them at all. I can enter commands into the box and press the button, but nothing happens at all. The text box remains blank.
I have another thread here, where I referenced my issue with slightly more clarity:
http://www.vbforums.com/showthread.p...79#post4240679
I run VB10 on VS10. All I want to do is gather information from a cmd window without using a text file. But when I try the above approaches, it seems like the readtoend fails or something... My variable remains blank.
Does anyone have any idea?
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Hello, I got your PM. I hope this code helps.
Private Sub CMDAutomate()
Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
' Dim mycmd As String = "net use \\172.x.x.x /user:mynetwork\"
Dim mycmd As String = "net use \\172.x.x.x\IPC$ /user:mynetwork\"
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False 'used for redirect
StartInfo.CreateNoWindow = True 'hides cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
' SW.WriteLine(txtInputBox.Text + " " + txtPWbox.Text) 'the command you wish to run.....
SW.WriteLine(mycmd + txtInputBox.Text + " " + txtPWbox.Text)
SW.WriteLine("Hello - Your drive is now mapped")
SW.WriteLine(txtPWbox)
Results = SR.ReadToEnd
SW.Close()
SR.Close()
Invoke(Finished)
End Sub
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
I was using net use to map a drive. I had the user enter data in textbox1 and textbox2 and using the redirects finished the command line portion of it.
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Hey,
I still have the same issues with your code. =(
Basically, the variable is blank no matter what. What does Invoke(Finished) do? That's the only part of your code I haven't used. I just let the sub end at SR.Close()... I wonder if that's the issue... If not, I have no idea what I am doing wrong.
It appears that my variable, after going through any of the above code will be set to blank. I get the feeling that my CMD window is just blank at the start and blank during and after I give it commands. Anyone know why?
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Quote:
Originally Posted by
RichardG
Hey,
I still have the same issues with your code. =(
Basically, the variable is blank no matter what. What does Invoke(Finished) do? That's the only part of your code I haven't used. I just let the sub end at SR.Close()... I wonder if that's the issue... If not, I have no idea what I am doing wrong.
It appears that my variable, after going through any of the above code will be set to blank. I get the feeling that my CMD window is just blank at the start and blank during and after I give it commands. Anyone know why?
It's been a few months now and I have fixed my issue. Basically, I was programming in a weird enterprise environment and I didn't have enough permissions to do what I wanted to. So here's my fix: to get the code to work correctly, first, I build the project. Then, I go to my project's bin/debug folder and copy the .exe of my project. I paste that .exe into my C:\temp folder. THEN from my desktop I right click run as admin a .bat file that I created. This .bat file contains nothing but "start C:\temp\filename.exe"
When I call the program like that, I am able to circumvent the administrative difficulties that I was experiencing. I hope writing this saves some other newbie days of fiddling.
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
since this is gr8 code and i am using same thread to ask my Question.
how it can work with pstools commands ?
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
could i hide the command line that i insert.. because there is a password in the command line and it should hide.. thanks.!!!
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Hi thanks, for this code, is exactly what I was looking for! I know it's been a while since last message, but I need some help here:
my intent is to execute an external script that runs some commands, this code is working fine when it as to invoke the external script and the script do its stuff just fine, only problem is I don't receive the updated output from the cmd. I'd like that the textbox shows each step in wich external script is.
Right now what the external script is doing is showed only at the end of its run, but I need realtime updates. Is this doable?
Thanks a lot, I hope it's clear enough
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
This was great, thank you
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
Oh man, this is a nice piece of code here. Kudos!
-
Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]
I did this twenty years ago... y'all need some help?