0
votes

I am writing code in VB.NET 2.0 and want to run batch commands for FTP using FTP -s:filename command.

I have a Batch file FTP.TXT for FTP Upload. It has the following statements:

OPEN <FPT SERVER IP>
USERNAME
PASSWORD
ASC
CD FOLDERNAME
PUT D:\DRFT000009.TXT FTPDRFTIN.DRFT000009
BYE

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.

This code runs fine but here I read a static batch file FTP.TXT from the computer:

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 & error2
        process.WaitForExit()


        Response.Write(totalerror)

End Sub

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:

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 <FPT IP ADDRESS>"
        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 & vbCrLf)
        Next
    End Using

    Dim error2 As String = process.StandardError.ReadToEnd()
    totalerror = totalerror & error2
    process.WaitForExit()


    Response.Write(totalerror)

End Sub

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.

1
The file FTP.TXT is a simple text file, why don't you try to rewrite it as you like and then call the same code that works?Steve

1 Answers

1
votes

Being a simple text file with a clear format, you could rewrite the file passing the parameters that need to be dynamically changed

Public Sub FTP4()

    ' Of course I assume that, at this point your program knows the exact values '
    ' to pass at the procedure that rebuilds the file'
    PrepareFTPFile("D:\DRFT000006.TXT", "USERNAME", "PASSWORD")

    ' Now you call the original code.....
    Dim psi As ProcessStartInfo
    Dim totalerror As String = ""
    psi = New ProcessStartInfo()
    psi.FileName = "FTP.EXE"
    psi.Arguments = " -s:D:\FTP.TXT"
    ....
End Sub


Public Sub PrepareFTPFile(fileToUpload as String, username as string, userpass as string)
    Using sw = new StreamWriter("D:\FTP.TXT", False)
       sw.WriteLine("OPEN <FPT IP ADDRESS>")
       sw.WriteLine(username)
       sw.WriteLine(userpass)
       sw.WriteLine("ASC")
       sw.WriteLine("CD SFCD40DAT")
       sw.WriteLine("PUT " + fileToUpload + " FTPDRFTIN." + Path.GetFileNameWithoutExtension(fileToUpdload))
       sw.WriteLine("BYE")
    End Using
End Sub