Solved

VB.NET execute multiple command prompt commands

Posted on 2013-06-06
6
2,077 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now