Solved

VB.NET execute multiple command prompt commands

Posted on 2013-06-06
6
2,253 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

738 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