[RESOLVED] Execute CMD commands in VB with parameters
I want to quietly execute CMD commands within my GUI app to run CMD external app with specified parameters from textbox. I will have cmd.exe in my directory, so I suppose that I will be able to pass only arguments of CMD-only app instead of cd desktop cd folder/onlycmdapp.exe (that works perfectly with warning message)
Could you help me? I´ve tried many ways to do that, but none seems to work properly for me.
Thanks a lot.
VB.NET Developer
Re: Execute CMD commands in VB with parameters
cmd.exe exists in all versions of windows. Why not just use the regular cmd.exe in c:\windows\system32 ???
What warning message are you getting?
Re: Execute CMD commands in VB with parameters
Currently none. But when I have tried to close CMD via exit/close commads commmand, it reopens automatically.
Re: Execute CMD commands in VB with parameters
We'll need to see your code...
Re: Execute CMD commands in VB with parameters
Sure. Here is it:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Shell("cmd.exe /k" + TextBox1.Text)
Close()
End Sub
End Class
This closes main form and not CMD window. The same with Me.Close command.
Re: Execute CMD commands in VB with parameters
If you use the /c option rather than /k option, the window should close automatically when the command is done.
If you don't want to see the command window, either there is an option for that, or you may need to use a Process object where it has the option to not open the window. Unfortunately I'm heading out the door and don't have time to look for the option, but I'm sure there is a way to not show the command windows.
Re: Execute CMD commands in VB with parameters
Oh, I resolved it via GetProcessByName command. Sorry for stupid question, but I am beginner in VB and generally in programming.
Re: Execute CMD commands in VB with parameters
Since you are using .Net I'd suggest looking at using "Process" Shell is similar to Process.Start and you have more control:
https://www.dotnetperls.com/process-vbnet
CreateNoWindow: Specifies that you want to run a command line program silently without flashing a console window.
You can also trap warning and see if you want to act on them or ignore them.
Here is a sample I use just to give you the flavor:
Code:
Dim mProcess As New Process
mProcess.StartInfo.WorkingDirectory = "C:\_Team\_Deploy\SQL\LTR1TEAMSQLUA CMISQL\Rating"
mProcess.StartInfo.FileName = "SVN"
mProcess.StartInfo.Arguments = "update 2_ToDeploy"
mProcess.StartInfo.RedirectStandardOutput = True
mProcess.StartInfo.RedirectStandardError = True
mProcess.StartInfo.UseShellExecute = False
mProcess.StartInfo.CreateNoWindow = False
mProcess = Process.Start(mProcess.StartInfo)
pOutput = mProcess.StandardOutput.ReadToEnd
pError = mProcess.StandardError.ReadToEnd
mProcess.WaitForExit()
Re: Execute CMD commands in VB with parameters
cmd.exe exists in all versions of windows. Why not just use the regular cmd.exe in c:\windows\system32 ???
What warning message are you getting?
Re: Execute CMD commands in VB with parameters
Quote:
Originally Posted by
.paul.
cmd.exe exists in all versions of windows. Why not just use the regular cmd.exe in c:\windows\system32 ???
What warning message are you getting?
Let's split the difference...Use process.start to run cmd.exe :p
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
Process.Start(info);
Re: Execute CMD commands in VB with parameters
Currently, cmd window leaves running even the command is executed. I have the "/c" switch, but it does not terminate cmd after the command completes.
Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Shell("cmd.exe /c" + TextBox1.Text, AppWinStyle.Hide)
End Sub
Re: Execute CMD commands in VB with parameters
Quote:
Originally Posted by
VB.NET Developer
I have the "/c" switch
Actually you don't... what switch you have will vary based on what is in the textbox.
If the textbox contains the word Hello, then the switch will be /cHello
You should add a space after the letter c, ie:
Code:
Shell("cmd.exe /c" + TextBox1.Text, AppWinStyle.Hide)
Re: Execute CMD commands in VB with parameters
This does not work. Your code doesn´t contains space. I´ve tried that with space after the "c" switch, but it does not work either. And, I´ve noticed that in task manager there are many "cmd" and "conhost" processess.
I was thinking about one possible solution which could work, but problem is that some operations will be executed much faster than others i.e. it´s not possible to terminate CMD process after a fixed amount of time. One solution could be to detect file length and calculate optimal termination time based on, but this can vary from file to file - that´s not universal for two different files with same size. Another solution could be to detect if output is generated and if yes, terminate cmd.exe.
Is there an another way?
Thanks.
VB.NET Developer
Re: Execute CMD commands in VB with parameters
Quote:
Originally Posted by
VB.NET Developer
Your code doesn´t contains space.
It looks that way, because something odd has happened with the forum (and I haven't seen that happen before). It showed up properly when I posted it, and you can see it if you quote the post, but the space seems to have become hidden from display. :confused:
Here's another attempt (without highlighting this time):
Code:
Shell("cmd.exe /c " + TextBox1.Text, AppWinStyle.Hide)
Quote:
I was thinking about one possible solution ...
Those options are dubious and/or unreliable (especially if it will run on multiple computers).
Is it possible to run the target program without using cmd.exe at all?
eg:
Code:
Shell(TextBox1.Text, AppWinStyle.Hide)
Re: Execute CMD commands in VB with parameters
That works perfectly - CMD is hidden and it closes automatically when the command completes. That´s I am looking for. Thanks a lot!
Re: [RESOLVED] Execute CMD commands in VB with parameters
Hi
not sure what you want to execute.
I use this to delete Files from the Folder
Code:
Dim strcommand As String
strcommand = "del E:\MoveBackup /s /f /q "
'write to Textfile what was deleted
Shell("cmd.exe /c " & strcommand & "> ""E:\LogDeletedFiles.txt""")