locked
Control batch application from VB windows form RRS feed

  • Question

  • Hello.
    I have the following problem.
    I have a VB windows application and I need a way to control input output from this windows application to a batch file.
    The batch script is quite complex so converting to a VB console application is out of the question because it will too much time.
    An simplified example is this:
    I have this batch file test.bat(just for example)

     

    @echo off
    
    set /p choice=name : %choice%
    
    echo Hello %choice%!

     

    I repeat this is very simple but It will help me a lot in solving my problem...
    In this batch file the user input the name which is then stored in a variable and after the user press enter on the screen will appear Hello + user input.

    Now the problems:

    1. Is it possible to enter the name in a textbox from the windows form and by pushing a send button pass the name to the batch application and then the batch file to continue the operation of displaying the Hello message???
    2. After the batch file finishes the operations it will open another console windows (3) with status of the various operations that take place on the system(in my case connected users to a server)
    Is it possible to hide this child console windows that are generated after my main .bat application finished the operations of opening connections???
    Can this be done from the windows application or I need to modify the .bat script???
    3. Is there a way to collect the console windows output in a textbox from the windows application???
    One idea that came to me was to redirect the console output to a textfile and then read the textfile in the VB application but I want to know if there is another way.
    4. Is it possible to hide this batch file inside the windows application exe???

     

    Wednesday, August 4, 2010 10:25 AM

Answers

  • Have a look at the sample

    Get input from a page

    'Get the contents of a page
        Dim p As New Process
        Dim pi As New ProcessStartInfo
        pi.UseShellExecute = False
        pi.RedirectStandardOutput = True
        pi.Arguments = "www.google.com"
        pi.WorkingDirectory = "C:\windows\system32"
        'this for nt* computers 
        pi.FileName = "ping"
        p.StartInfo = pi
        p.StartInfo = pi
        p.Start()
        Dim sr As IO.StreamReader = p.StandardOutput
        Dim sb As New System.Text.StringBuilder("")
        Dim input As Integer = sr.Read
        Do Until input = -1
          sb.Append(ChrW(input))
          input = sr.Read
        Loop
        MessageBox.Show(sb.ToString)
    

    You have yourself to set the arguments of course for the badfile

    sb.ToString is the result


    Success
    Cor
    Wednesday, August 4, 2010 11:21 AM

All replies

  • Here some samples how to use process start

    http://www.vb-tips.com/StartProcess.aspx


    Success
    Cor
    Wednesday, August 4, 2010 10:33 AM
  • Thank you.

    But the most important problem is point 1.

    How can I make the .bat file to wait for my input from the windows form textbox and how to make the bat file to receive the input from my textbox and continue the execution with this input received???

    i know about SendKeys but this is NOT very reliable...

    • Proposed as answer by Cor Ligthert Wednesday, August 4, 2010 11:18 AM
    Wednesday, August 4, 2010 10:55 AM
  • Have a look at the sample

    Get input from a page

    'Get the contents of a page
        Dim p As New Process
        Dim pi As New ProcessStartInfo
        pi.UseShellExecute = False
        pi.RedirectStandardOutput = True
        pi.Arguments = "www.google.com"
        pi.WorkingDirectory = "C:\windows\system32"
        'this for nt* computers 
        pi.FileName = "ping"
        p.StartInfo = pi
        p.StartInfo = pi
        p.Start()
        Dim sr As IO.StreamReader = p.StandardOutput
        Dim sb As New System.Text.StringBuilder("")
        Dim input As Integer = sr.Read
        Do Until input = -1
          sb.Append(ChrW(input))
          input = sr.Read
        Loop
        MessageBox.Show(sb.ToString)
    

    You have yourself to set the arguments of course for the badfile

    sb.ToString is the result


    Success
    Cor
    Wednesday, August 4, 2010 11:21 AM