Results 1 to 16 of 16

Thread: [RESOLVED] Execute CMD commands in VB with parameters

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Resolved [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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Execute CMD commands in VB with parameters

    We'll need to see your code...

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    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.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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()
    Last edited by TysonLPrice; Jun 3rd, 2019 at 01:22 PM.
    Please remember next time...elections matter!

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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?

  10. #10
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Execute CMD commands in VB with parameters

    Quote Originally Posted by .paul. View Post
    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

    ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
    Process.Start(info);
    Please remember next time...elections matter!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Execute CMD commands in VB with parameters

    Quote Originally Posted by VB.NET Developer View Post
    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)

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Execute CMD commands in VB with parameters

    Quote Originally Posted by VB.NET Developer View Post
    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.

    Here's another attempt (without highlighting this time):
    Code:
            Shell("cmd.exe /c " + TextBox1.Text, AppWinStyle.Hide)

    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)

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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!

  16. #16
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    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""")
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width