none
How to execute a batch file with parameters in visual basic

    Question

  • I'm trying to run a batch file with parameters using vb code, on buttonclick.

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            If Process.Start("C:\Users\****\IE10_Blocker.cmd", "IE10_Blocker . /B") Then
                MsgBox("Internet Explorer 10 Updates Successfully Blocked!")
            Else
                MsgBox("Failed to Block Internet Explorer 10 Updates!")
            End If
        End Sub

    Basically, I'm incorporating the IE10_Blocker toolkit, which comes as a batch file .cmd. with the following parameters IE10_Blocker . /B where IE10_Blocker is the name of the batch file, the . represents computerName and /B represents block, into an application I'm trying to build.

    I want the user to be able to click on a button, which in turn will execute the IE10_Blocker.cmd with the above parameters.

    Upon successfull/failed execution of this script, I would like to receive a MessageBox popup either confirming successfull run or failure.

    I know I'm missing something.  

    Tuesday, July 02, 2013 10:05 PM

Answers

  • Hi,

     You will need to set the process up to redirect the standard output. Then you can read the text that the program shows in the cmd window that tells you if it was successful or not. I read threw the Microsoft page for the toolkit and it said to run the program by typing “ie10_blocker.cmd /B” in the cmd window so (ie10_blocker.cmd) will be the program name to run and (/B) will be the argument that it uses. It also said that after it runs, the program will show text in the cmd window that will let you know if it is successful so you will need to know what the text is that it returns to show success so you can replace the ("Success Text") that is in the code i posted below. Then if the text that is returned contains the text that shows it was successful it shows the success messagebox.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim proc As New Process
            proc.StartInfo.FileName = "C:\Users\****\IE10_Blocker.cmd" 'Use the the full Pathname of the program
            proc.StartInfo.Arguments = "/B" 'This is the argument that is used
            proc.StartInfo.UseShellExecute = False
            proc.StartInfo.RedirectStandardOutput = True
            proc.StartInfo.CreateNoWindow = True 'Dont show the cmd window when the program is running
            proc.Start()
    
            'Read the text from the cmd window
            Dim output As String = proc.StandardOutput.ReadToEnd()
    
            'Check to see if the text contains whatever the program shows in the cmd window to show it was successful
            If output.Contains("Success Text") Then 'Replace (Success Text) with the text that shows it was successful
                MessageBox.Show("Success")
            Else
                MessageBox.Show("Failed")
            End If
            proc.Dispose()
        End Sub
    

    • Marked as answer by CodeNemesis Wednesday, July 03, 2013 3:34 AM
    Wednesday, July 03, 2013 1:10 AM

All replies

  • Hi,

     You will need to set the process up to redirect the standard output. Then you can read the text that the program shows in the cmd window that tells you if it was successful or not. I read threw the Microsoft page for the toolkit and it said to run the program by typing “ie10_blocker.cmd /B” in the cmd window so (ie10_blocker.cmd) will be the program name to run and (/B) will be the argument that it uses. It also said that after it runs, the program will show text in the cmd window that will let you know if it is successful so you will need to know what the text is that it returns to show success so you can replace the ("Success Text") that is in the code i posted below. Then if the text that is returned contains the text that shows it was successful it shows the success messagebox.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim proc As New Process
            proc.StartInfo.FileName = "C:\Users\****\IE10_Blocker.cmd" 'Use the the full Pathname of the program
            proc.StartInfo.Arguments = "/B" 'This is the argument that is used
            proc.StartInfo.UseShellExecute = False
            proc.StartInfo.RedirectStandardOutput = True
            proc.StartInfo.CreateNoWindow = True 'Dont show the cmd window when the program is running
            proc.Start()
    
            'Read the text from the cmd window
            Dim output As String = proc.StandardOutput.ReadToEnd()
    
            'Check to see if the text contains whatever the program shows in the cmd window to show it was successful
            If output.Contains("Success Text") Then 'Replace (Success Text) with the text that shows it was successful
                MessageBox.Show("Success")
            Else
                MessageBox.Show("Failed")
            End If
            proc.Dispose()
        End Sub
    

    • Marked as answer by CodeNemesis Wednesday, July 03, 2013 3:34 AM
    Wednesday, July 03, 2013 1:10 AM
  • Thanks for the suggestion. I've replaced the generic messagebox popups with my own custom message displays. They work, which is great. Never would have thought of actually checking for the return text as the conditional value for the popup message.
    Wednesday, July 03, 2013 3:34 AM
  • Hi,

     Well, it was ether do it that way or i read that it creates a registry key when it is run but, it didn`t say where the registry key is located so i figured it might be easier to just read the standard output. Anyways, your welcome.  :)

    Wednesday, July 03, 2013 10:14 AM
  • Hey, another quick question. Is there any way to code the location of IE10_Blocker.cmd in visual basic in a sort of generic way. Kind of like %localhost%/defaultfolder/IE10_Blocker.cmd? I am asking this because I have my visual basic express 2010 project files located on my skydrive.

    Additionally, I have the skydrive app for windows 7 installed on 2 separate machines that I use for development. So when entering paths, I always have to re-enter paths when accessing the app on separate machines through visual basic express.

    This is because when accessing from separate locations the folders are named differently for the users in skydrive app.

    Ex:

    pc1/username/appfolder

    pc2/username/appfolder

    Friday, July 05, 2013 2:17 AM
  • How can I run this as an administrator? I tried this already:

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim process As System.Diagnostics.Process = Nothing
            Dim processStartInfo As System.Diagnostics.ProcessStartInfo
            processStartInfo = New System.Diagnostics.ProcessStartInfo()
            processStartInfo.FileName = "C:\Users\***\IE_Blocker\IE10_Blocker.cmd" 'Use the the full Pathname of the program
            processStartInfo.Verb = "runas"
            processStartInfo.Arguments = "/B" 'This is the argument that is used
            processStartInfo.UseShellExecute = False
            processStartInfo.RedirectStandardOutput = True
            processStartInfo.CreateNoWindow = True 'Dont show the cmd window when the program is running
            processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
            process = System.Diagnostics.Process.Start(processStartInfo)
            process.Start()
    
            'Read the text from the cmd window
            Dim output As String = process.StandardOutput.ReadToEnd()
    
            'Check to see if the text contains whatever the program shows in the cmd window to show it was successful
            If output.Contains("The operation completed successfully.") Then 'Replace text with the text that shows it was successful in the command prompt
                blocker_applied.Show()
            Else
                blocker_failed.Show()
            End If
            process.Dispose()
        End Sub


    • Edited by CodeNemesis Friday, July 05, 2013 3:50 AM code correction
    Friday, July 05, 2013 3:24 AM