Solved

VB.NET execute multiple command prompt commands

Posted on 2013-06-06
6
2,176 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

830 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