When I try to download file using FTP, sometimes it happened that I have got file cut somewhere in the middle. It happens because file upload hasn't been finished yet and I try to download the file. After file dowload we remove file on FTP.... and now You can say bye bye to the not-downloaded content.

While changing some code I found solution for this problem. I have tried to implement function to move file from FTP. Just connect, get file, remove it and close connection. Simple, isn't it? But suddenly I realised that until the file is not uploaded completely I cannot remove it from FTP! Wow. I've just change the procedure to make 10 retries with 10 seconds wait and now I can enjoy the whole file :-)

Solution presented bellow is made in LotusScript, but until it is based on Windows API FTP functions You can implement it in any environment based on Windows API. In this solution I am using NotesFTP class made by Paul D. Ray.

I am curious if it is general behaviour for FTP protocol or some special servers?

Here is the code:

Dim triesCnt As Long
Dim ftp As New NotesFTPSession
Call ftp.Connect (ftpServer, userName, password, INTERNET_FLAG_PASSIVE)

On Error Resume Next
If triesCnt > 0 Then
'Retry after 10sec
Print "MoveFileFTP, retry "+Cstr(triesCnt)
Sleep 10
End If

If triesCnt > 10 Then
'If there are more than 10 tries, remove locafile and quit
Print "MoveFileFTP, unsuccesfull"
Kill localFileName
Goto finish
End If

Call ftp.GetFile(remotFileName, localFileName, FTP_TRANSFER_TYPE_BINARY)

triesCnt = triesCnt + 1
Call ftp.DeleteFile(remotFileName)
Goto again
End If

Call ftp.Disconnect
Delete ftp


Newer Post Older Post Home