title: How to run batch commands with dynamic statements?
body: <p>I am writing code in VB.NET 2.0 and want to run batch commands for FTP using FTP -s:filename command.</p>
  
  <p>I have a Batch file FTP.TXT for FTP Upload. It has the following statements:</p>
  
  <pre><code>OPEN &lt;FPT SERVER IP&gt;
  USERNAME
  PASSWORD
  ASC
  CD FOLDERNAME
  PUT D:\DRFT000009.TXT FTPDRFTIN.DRFT000009
  BYE
  </code></pre>
  
  <p>I have to dynamically change the filename in the Batch File. Now either I can create a Batch file at runtime and then read it or I got a code to set the input stream of the Process object. But its not working as desired.</p>
  
  <p>This code runs fine but here I read a static batch file FTP.TXT from the computer:</p>
  
  <pre><code>Public Sub FTP4()
      Dim psi As ProcessStartInfo
      Dim totalerror As String = ""
          psi = New ProcessStartInfo()
          psi.FileName = "FTP.EXE"
          psi.Arguments = " -s:D:\FTP.TXT"
          psi.RedirectStandardError = True
          psi.RedirectStandardOutput = True
          psi.CreateNoWindow = True
          psi.WindowStyle = ProcessWindowStyle.Hidden
          psi.UseShellExecute = False
  
          Dim process As Process = process.Start(psi)
          Dim error2 As String = process.StandardError.ReadToEnd()
          totalerror = totalerror &amp; error2
          process.WaitForExit()
  
  
          Response.Write(totalerror)
  
  End Sub
  </code></pre>
  
  <p>But I want somehow to get the FTP done with custom file name for each request. This is what I tried which is not working:</p>
  
  <pre><code>Public Sub FTP5()
      Dim totalerror As String = ""
      Dim BatchScriptLines(6) As String
  
      Dim process As New Process
      process.StartInfo.FileName = "FTP.EXE"
      process.StartInfo.UseShellExecute = False
      process.StartInfo.CreateNoWindow = True
      process.StartInfo.RedirectStandardInput = True
      process.StartInfo.RedirectStandardOutput = True
      process.StartInfo.RedirectStandardError = True
      process.Start()
      process.BeginOutputReadLine()
      Using InputStream As System.IO.StreamWriter = process.StandardInput
          InputStream.AutoFlush = True
          BatchScriptLines(0) = "OPEN &lt;FPT IP ADDRESS&gt;"
          BatchScriptLines(1) = "USERNAME"
          BatchScriptLines(2) = "PASSWORD"
          BatchScriptLines(3) = "ASC"
          BatchScriptLines(4) = "CD SFCD40DAT"
          BatchScriptLines(5) = "PUT D:\DRFT000006.TXT FTPDRFTIN.DRFT000006"
          BatchScriptLines(6) = "BYE"
          For Each ScriptLine As String In BatchScriptLines
              InputStream.Write(ScriptLine &amp; vbCrLf)
          Next
      End Using
  
      Dim error2 As String = process.StandardError.ReadToEnd()
      totalerror = totalerror &amp; error2
      process.WaitForExit()
  
  
      Response.Write(totalerror)
  
  End Sub
  </code></pre>
  
  <p>Please advise how I can get the "FTP -s:filename" command executed in this case. Basically I want to do something similar to single line batch file execution which I not able to do.</p>