Results 1 to 18 of 18

Thread: [2005] Using the system console to run a command

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    [2005] Using the system console to run a command

    Just a simple question:

    I'm wanting to run a traceroute in my program. I have methods in place to interpret the output, but I couldn't get to this bit before because I was in university accommodation which blocks traceroutes.

    Anyhow, how can I do one within my program? The plan is to do it without showing a command prompt, then pass the output to my parsing method. I also can't afford to pay for some third party implementation of it - someone suggested before that I can use the system console or words to a similar effect, I just don't know how.

    Thanks.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    Yes, you can use the cmd and pass the dos commands as arguments with an output to a textfile. Or even with a bit more work you can redirect the output to eliminate the extra file need. Gigemboy has a codebank thread on redirecting outputs.
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    Thanks for the response.

    What I was actually after was the way to actually do this within the code - what I have to type to, say, execute "tracert 140.191.222.5" for example. That's just the bit I'm stuck on, since I've never actually done it before.

    I don't think I need to redirect the output since I plan to be storing it in text files anyway.

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Using the system console to run a command

    It should look something like this
    Code:
    Dim proc As New Process()
            With proc.StartInfo
                .FileName = "tracert.exe"
                .Arguments = "140.191.222.5"
                .CreateNoWindow = True
                .UseShellExecute = False
                .RedirectStandardOutput = True
            End With
            proc.Start()
            proc.WaitForExit()
            Dim output As String = proc.StandardOutput.ReadToEnd()
            Dim writer As New IO.StreamWriter("your output file path here")
            writer.Write(output)
            writer.Close()
            proc.Close()

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    Thanks very much, I'll give that a go.

    I feel a bit bad asking such rudimentary questions, I have bought 2 books but I can't seem to find anything about that specific thing in either

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    Yes stanav but with one more argument for the output file as it makes it easier.

    .Arguments = "140.191.222.5 > c:\MyFile.txt"
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    Okay, this is what I have:

    Code:
        Private Function traceRoute(ByVal ip As String, ByVal outputFile As String)
            Dim proc As New Process()
            With proc.StartInfo
                .FileName = "tracert.exe"
                .Arguments = ip
                .CreateNoWindow = True
                .UseShellExecute = False
                .RedirectStandardOutput = True
            End With
            proc.Start()
            proc.WaitForExit()
            Dim output As String = proc.StandardOutput.ReadToEnd()
            Dim writer As New IO.StreamWriter(outputFile)
            writer.Write(output)
            writer.Close()
            proc.Close()
        End Function
    This is what I use in the button that calls it:

    Code:
    traceRoute("72.14.207.99", "C:\TESTTracert.txt")
    What happens is I see tracert pop up in process explorer, so it is indeed starting, but it seems like nothing happens after that - the program stops responding. Traceroute doesn't complete and the file I passed over is not created.

    Is this a problem with my using blackslash in the file path? I can't remember if there were issues in this language with doing that - I know there are in some others. I have tried using \\.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    You need to use the argument as I posted because the ">" character tells dos to output the results to the file specified.
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    Ahh of course - I was typing that in when I was doing them manually but I suffered a 'brain disengaged' error when trying to implement it.

    Right. This time around it starts the tracert process but doesn't create any file. It doesn't hang this time.

    This is my current function:

    Code:
        Private Function traceRoute(ByVal ip As String, ByVal outputFile As String)
            Dim proc As New Process()
            With proc.StartInfo
                .FileName = "tracert.exe"
                .Arguments = ip & " > " & outputFile
                .CreateNoWindow = True
                .UseShellExecute = False
                '.RedirectStandardOutput = True
            End With
            proc.Start()
            proc.WaitForExit()
            'Dim output As String = proc.StandardOutput.ReadToEnd()
            'Dim writer As New IO.StreamWriter(outputFile)
            'writer.Write(output)
            'writer.Close()
            proc.Close()
        End Function
    I didn't think I could use your exact argument text since I need to be able to specify the output file and IP in variables, but I assume this is meant to have the same effect?
    Last edited by Anjow; Mar 26th, 2007 at 05:11 PM.

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    Any thoughts?

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    What is the values of your argument if you output them to the console window?
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    Sorry about the delay, the topic was temporarily buried so I thought it had died.

    I have tried sending the argument as .Arguments = "72.14.207.99 > c:\test.txt" and it doesn't work. However, when I enter that as the argument manually in a command prompt outside of this program it works fine. Sending just the IP and no filepath works fine within the program too.

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    I figured out the argument issue in this thread:
    http://vbforums.com/showthread.php?p...38#post2828238

    Setting the working directory is the key so you dont need to pass the entire path of the output file.
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim oPSI As New System.Diagnostics.ProcessStartInfo
    3.     Dim oProcess As System.Diagnostics.Process
    4.     With oPSI
    5.         .FileName = "C:\Windows\System32\cmd.exe"
    6.         .CreateNoWindow = True
    7.         .WindowStyle = ProcessWindowStyle.Hidden
    8.         .WorkingDirectory = "C:\Users\VB-Guru\"
    9.         .UseShellExecute = True
    10.         .Arguments = "/C Ping google.com > PingResults.txt"
    11.     End With
    12.     oProcess = System.Diagnostics.Process.Start(oPSI)
    13.     oProcess.WaitForExit()
    14.     System.Diagnostics.Process.Start(oPSI.WorkingDirectory & "PingResults.txt")
    15. End Sub
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    I can't say I'm exactly sure what you mean, but I had a look at the linked thread and now the inside of my 'with' bit looks like this:

    Code:
                .FileName = "tracert.exe"
                .WorkingDirectory = "C:\"
                .Arguments = "72.14.207.99 > test.txt"
                .CreateNoWindow = True
                .UseShellExecute = False
    Which seems to yield the same problems.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    It works just fine for me using your provided ip address, tracert.exe and the code I posted.

    Tracing route to eh-in-f99.google.com [72.14.207.99]

    over a maximum of 30 hops:



    1 1 ms <1 ms <1 ms 192.168.1.1

    2 25 ms 24 ms 24 ms netblock-66-159-208-1.dslextreme.com [66.159.208.1]

    3 25 ms 24 ms 25 ms LAX1.CR1.Gig9-0-3.dslextreme.com [66.51.203.33]

    4 24 ms 25 ms 24 ms ge-5-1-115.ipcolo1.LosAngeles1.Level3.net [63.209.70.133]

    5 35 ms 35 ms 35 ms ae-31-53.ebr1.LosAngeles1.Level3.net [4.68.102.94]

    6 38 ms 36 ms 35 ms ae-2.ebr1.SanJose1.Level3.net [4.69.132.9]

    7 36 ms 35 ms 36 ms ae-1-100.ebr2.SanJose1.Level3.net [4.69.132.2]

    8 67 ms 73 ms 70 ms ae-3.ebr1.Denver1.Level3.net [4.69.132.58]

    9 70 ms 72 ms 71 ms ae-1-100.ebr2.Denver1.Level3.net [4.69.132.38]

    10 90 ms * * ae-3.ebr1.Chicago1.Level3.net [4.69.132.62]

    11 116 ms 110 ms 111 ms ae-2.ebr2.NewYork1.Level3.net [4.69.132.66]

    12 101 ms 101 ms 100 ms ae-21-54.car1.NewYork1.Level3.net [4.68.97.116]

    13 105 ms 107 ms 106 ms GOOGLE-INC.car1.NewYork1.Level3.net [4.71.172.86]

    14 106 ms 106 ms 106 ms 72.14.236.213

    15 116 ms 116 ms 118 ms 72.14.233.115

    16 120 ms 116 ms 118 ms 66.249.94.90

    17 119 ms 118 ms 120 ms 66.249.94.50

    18 117 ms 116 ms 119 ms eh-in-f99.google.com [72.14.207.99]



    Trace complete.
    Last edited by RobDog888; Mar 30th, 2007 at 12:27 PM.
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    Exactly as I tested it. Did you also add the cmd.exe as shown in my previous code post too?
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     Dim oPSI As New System.Diagnostics.ProcessStartInfo
    3.     Dim oProcess As System.Diagnostics.Process
    4.     With oPSI
    5.         .FileName = "C:\Windows\System32\cmd.exe"
    6.         .CreateNoWindow = True
    7.         .WindowStyle = ProcessWindowStyle.Hidden
    8.         .WorkingDirectory = "C:\Users\VB-Guru\"
    9.         .UseShellExecute = True
    10.         .Arguments = "/C tracert.exe 72.14.207.99 > PingResults.txt"
    11.     End With
    12.     oProcess = System.Diagnostics.Process.Start(oPSI)
    13.     oProcess.WaitForExit()
    14.     System.Diagnostics.Process.Start(oPSI.WorkingDirectory & "PingResults.txt")
    15. End Sub
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    63

    Re: [2005] Using the system console to run a command

    I hadn't added that bit. I have now, and it seems to be working perfectly.

    Thanks very much for your assistance

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] Using the system console to run a command

    No prob. Glad to help.

    Ps, dont forget to Resolve your thread so other members will know its solved.
    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!
    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 i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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