Solved

VB.NET execute multiple command prompt commands

Posted on 2013-06-06
6
2,142 Views
Last Modified: 2013-06-10
I have code below working and get a popup
When I run this it opens in the correct directory
RunCommandCom("cd ", "C:\Program Files (x86)\Java\jre7\bin", True)

How can I run the following line while in THAT firectory and the command prompt open.
java -cp m5.jar org.m5.sdk.hostedconnect.v2.MonitorMultipleExample http://url.com 
Or just run that java from .Net



    Shared Sub RunCommandCom(command As String, arguments As String, permanent As Boolean)
        Dim p As Process = New Process()
        Dim pi As ProcessStartInfo = New ProcessStartInfo()
        pi.Arguments = " " + If(permanent = True, "/K", "/C") + " " + command + " " + arguments
        pi.FileName = "cmd.exe"
        p.StartInfo = pi
        p.Start()
    End Sub

Open in new window

0
Comment
Question by:lrbrister
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 350 total points
ID: 39228520
Hi,

You may try the following:

Have a string variable to hold the arguments.
Define a WorkingDirectory, which will point out where the executable resides.
Call the executable with ProcessStartInfo.

So, you could actually have a Sub:
Private Sub RunProcess(a_strProcessName As String, _
     a_strProcessPath As String, _
     a_strArguments As String)

     Dim pi As ProcessStartInfo = New ProcessStartInfo(a_strProcessName , strArguments )
     ProcInfo.WorkingDirectory = a_strProcessPath 
     ProcInfo.UseShellExecute = False
     ProcInfo.CreateNoWindow = True

     Dim Proc As New Process

     Proc.StartInfo = ProcInfo
     Proc.Start()
     Proc.WaitForExit()
End Sub

Open in new window


You call the above as:
RunProcess("java.exe", _
      "C:\Program Files (x86)\Java\jre7\bin", _
      "-cp m5.jar org.m5.sdk.hostedconnect.v2.MonitorMultipleExample http://url.com")

Open in new window


Giannis
0
 

Author Comment

by:lrbrister
ID: 39229003
Hey guys,
 I'm on the road right now.
Will test your solution later today
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39229270
As indicated above, the Process is a good way to handle a window if you want to interact with it.  However, the simpler way is to simply shell out to the prompt to run a command, as long as you don't need to talk to the window once you send the command:

Shell("cmd /c dir")

Open in new window


This will open a command prompt and run the command (in this case "dir") then "c"lose the window.

If you want to see what happened in the window, replace the /c with /k and the window will be "k"ept open.  You may also want the window to display with "NormalFocus" so it doesn't stay minimized.

Shell("cmd /k dir", AppWinStyle.NormalFocus)

Open in new window


One thing to note is that the command can be anything you can put into a single DOS command line; for more than one line, you'd need to create a batch file (presumably through code unless it's the same each time), save it, then run the batch file.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:lrbrister
ID: 39230425
Hey guys,
  That particular command I'm running, If I run it in a actual command prompt, it sends the command to the vendor, then "stays open" indefinately.  There is a never ending stream of data goming in.

If the window "Opens" and "Closes right away",
Does the command keep running?
0
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 150 total points
ID: 39230847
with the open-close, it depends on the command...if the command runs a separate thread, it will keep running.  for example, you could use DOS to launch Notepad, the DOS window will typically not close until that notepad session closes.  others may simply launch the app then close the window and leave the app open.

if the window will stay open, you probably want to go with the "process" option mentioned first, especially if results are put into the DOS window.
0
 

Author Closing Comment

by:lrbrister
ID: 39234707
Thanks guys
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 20
VB.NET 1 22
Write to a printer using vb.net 9 28
VB.net capture  result of a SQL query in variables 3 11
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question