Solved

VB.NET execute multiple command prompt commands

Posted on 2013-06-06
6
2,019 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

12 Experts available now in Live!

Get 1:1 Help Now