RobT RobT - 1 year ago 62 Question

VB FTP code works in Debug (F10) but not when run or F5

I need to FTP a local file to Mainframe and have written the below script to create a local Batch text file using streamwriter and then use this file with the ftp -s: command to run it.

Here is the code.

Shared Sub TestFTP()


Dim hostname As String
Dim username As String
Dim password As String
Dim mainfile As String
Dim localfile As String

Dim success As Boolean
Dim message As String

'Test DATA

hostname = "XXIBM2"
username = "USER1"
password = "XXX1234"
mainfile = "XXTSO.USER1.TEST2"
localfile = "D:\TestFTP.txt"

'=============================BP Code========================
Dim localPath As String = "C:\BPFTP"
Dim isExists As Boolean = System.IO.Directory.Exists(localPath)

If (isExists = False) Then
End If

' Open StreamWriter And create batch file

Using writer As StreamWriter = New StreamWriter(localPath + "\\FTP.txt")

writer.WriteLine("open " + hostname)
writer.WriteLine("put " + localfile + " '" + mainfile + "'")
End Using

' Perform FTP

Interaction.Shell("ftp -n -s:C:\BPFTP\FTP.txt")

' Delete batch file

success = True

Catch e As Exception

success = False
message = e.Message

End Try

End Sub

If I run the code using F5 the file does not appear on the mainframe.

If I set a breakpoing at the Shell command and run the code (f5) to here and then F5 to the end the file does not get FTPd to the mainframe.


If I run the code to the breakpoint and then simply 'Step Over' the Shell command line using F10 then the file successfully FTPs to the mainframe.

Answer Source

When you run in debug mode, you are forcing a synchronous operation, you need to tell shell and your ftp application to wait in order to completely send the file.

Interaction.Shell("ftp -n -s:C:\BPFTP\FTP.txt", AppWinStyle.MinimizedFocus, True, 30000)

See here

This will force it to wait 30 seconds before continuing, if you set it to -1 it waits forever which can cause undesirable behavior.