Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005] - Page 2-VBForums
Page 2 of 2 FirstFirst 12
Results 41 to 57 of 57

Thread: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

  1. #41
    New Member
    Join Date
    Apr 2012
    Posts
    4

    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!

  2. #42
    New Member
    Join Date
    May 2012
    Posts
    2

    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!

  3. #43
    New Member
    Join Date
    May 2012
    Posts
    2

    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:
    1. Private Sub CMDAutomate()
    2.         'Dim sipdomain As TextBox = txtCommand
    3.         Dim myprocess As New Process
    4.         Dim StartInfo As New System.Diagnostics.ProcessStartInfo
    5.         StartInfo.FileName = "cmd" 'starts cmd window
    6.         StartInfo.RedirectStandardInput = True
    7.         StartInfo.RedirectStandardOutput = True
    8.         StartInfo.UseShellExecute = False 'required to redirect
    9.         StartInfo.CreateNoWindow = True 'creates no cmd window
    10.         myprocess.StartInfo = StartInfo
    11.         myprocess.Start()
    12.         Dim SR As System.IO.StreamReader = myprocess.StandardOutput
    13.         Dim SW As System.IO.StreamWriter = myprocess.StandardInput
    14.         SW.WriteLine("nslookup -querytype=srv _h323ls._udp." & txtCommand.Text) 'Checks the H323ls SRV record
    15.         SW.WriteLine("nslookup -querytype=srv _h323cs._tcp." & txtCommand.Text) 'Checks the H323cs SRV record
    16.         SW.WriteLine("nslookup -querytype=srv _sip._tcp." & txtCommand.Text) 'Checks the sip.tcp SRV record
    17.         SW.WriteLine("nslookup -querytype=srv _sip._udp." & txtCommand.Text) 'Checks the sip.udp SRV record
    18.         SW.WriteLine("nslookup -querytype=srv _sips._tcp." & txtCommand.Text) 'Checks the sips.tcp SRV record
    19.         SW.WriteLine("exit") 'exits command prompt window
    20.         Results = SR.ReadToEnd 'returns results of the command window
    21.         SW.Close()
    22.         SR.Close()
    23.         'invokes Finished delegate, which updates textbox with the results text
    24.         Invoke(Finished)
    25.     End Sub

  4. #44
    New Member
    Join Date
    May 2012
    Posts
    7

    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 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.
    Attached Files Attached Files

  5. #45
    New Member
    Join Date
    Sep 2012
    Posts
    10

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application

    Quote Originally Posted by gigemboy View Post
    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:
    1. Dim myprocess As New Process
    2.         Dim StartInfo As New System.Diagnostics.ProcessStartInfo
    3.         StartInfo.FileName = "cmd" 'starts cmd window
    4.         StartInfo.RedirectStandardInput = True
    5.         StartInfo.RedirectStandardOutput = True
    6.         StartInfo.UseShellExecute = False 'required to redirect
    7.         StartInfo.CreateNoWindow = True '<---- creates no window, obviously
    8.         myprocess.StartInfo = StartInfo
    9.         myprocess.Start()
    10.         Dim SR As System.IO.StreamReader = myprocess.StandardOutput
    11.         Dim SW As System.IO.StreamWriter = myprocess.StandardInput
    12.         SW.WriteLine(txtCommand.Text) 'the command you wish to run.....
    13.         SW.WriteLine("exit") 'exits command prompt window
    14.         txtResults.Text = SR.ReadToEnd 'returns results of the command window
    15.         SW.Close()
    16.         SR.Close()
    i'm nt able to get the result of the command window on my form

  6. #46
    New Member
    Join Date
    Sep 2012
    Posts
    10

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application

    Quote Originally Posted by sanjeev467 View Post
    i'm nt able to get the result of the command window on my form
    Plz someone answer it

  7. #47
    New Member
    Join Date
    Sep 2012
    Posts
    10

    Question 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

  8. #48
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,116

    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"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel Core 2 Extreme Ed., 2 WD Raptor 10K RPM 300 GB HDs, 2 GBs DDR2 667 MHz RAM, 2 Viewsonic 24" LCDs, Windows 7 SP-1/Windows 8.1, Office 2010, VS 2013

  9. #49
    New Member
    Join Date
    Sep 2012
    Posts
    10

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    Quote Originally Posted by RobDog888 View Post
    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

  10. #50
    New Member
    Join Date
    Sep 2012
    Posts
    10

    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

  11. #51
    New Member
    Join Date
    Sep 2012
    Posts
    15

    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?

  12. #52
    New Member
    Join Date
    Apr 2012
    Posts
    13

    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
    Last edited by bvrider1; Sep 20th, 2012 at 08:39 PM.

  13. #53
    New Member
    Join Date
    Apr 2012
    Posts
    13

    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.

  14. #54
    New Member
    Join Date
    Sep 2012
    Posts
    15

    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?

  15. #55
    New Member
    Join Date
    Sep 2012
    Posts
    15

    Re: Automate Command Prompt Window (CMD), Redirect Output to Application [2003/2005]

    Quote Originally Posted by RichardG View Post
    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.

  16. #56
    New Member
    Join Date
    Dec 2012
    Posts
    7

    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 ?

  17. #57
    Registered User
    Join Date
    Aug 2014
    Posts
    1

    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.!!!

Page 2 of 2 FirstFirst 12

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.