Upload local file and append it to existing file
Posted: Thu Aug 03, 2006 1:22 pm
The following code uses the WinAPI to upload a local file to an FTP server. If the file doesn't exist on the server, it is created. But an already existing file doesn't get overwritten. It remains intact and the uploaded file is appended to the end of the server file. This is especially useful in a datacenter with a mainframe where new data are transferred to the server at the end of every work day and collected there until the end of the week when the final processing on the mainframe takes place.
The code uses the powerful and not very well known WinInet function FTPCommand() with which you can send FTP commands to the FTP server which are not implemented in WinInet. For a better understanding of the FTPCommand() function and its powerful possibilities take a look at the following background information for Visual BASIC applications:
http://www.vbip.com/wininet/wininet-ftp-command-01.asp
Code: Select all
; ----- Upload local file and append it to existing file on FTP-Server
; Please change to your UserID/Password etc.:
; -------------------------------------------------
#ClientFilename = "YourClientPathAndFilename.Txt"
#FTPServerIP = "YourServerNameOrIP"
#Password = "YourPassword"
#ServerFilename = "YourServerPathAndFilename.Txt"
#UserID = "YourUserID"
; -------------------------------------------------
#FileBufferSize = 1024
#FTP_TRANSFER_TYPE_ASCII = 1
#INTERNET_DEFAULT_FTP_PORT = 21
#INTERNET_SERVICE_FTP = 1
ErrorBuffer.S
ErrorBufferSize.L
ErrorCode.L
FTPConnectHandle.L
FTPFileHandle.L
FTPOpenHandle.L
NumBytesRead.L
NumBytesWritten.L
FTPOpenHandle = InternetOpen_("FTP", 1, "", "", 0)
If FTPOpenHandle = 0
MessageRequester("Error", "Initialisation of FTP connection failed!", #MB_ICONERROR)
End
EndIf
FTPConnectHandle = InternetConnect_(FTPOpenHandle, #FTPServerIP, #INTERNET_DEFAULT_FTP_PORT, #UserID, #Password, #INTERNET_SERVICE_FTP, 0, 0)
If FTPConnectHandle = 0
MessageRequester("Error", "Connection to FTP Server failed!", #MB_ICONERROR)
Else
If FtpCommand_(FTPConnectHandle, #True, #FTP_TRANSFER_TYPE_ASCII, "APPE " + #ServerFilename + #CRLF$, 0, @FTPFileHandle) = #False
ErrorCode = GetLastError_()
If ErrorCode = 0
MessageRequester("Error", "The Windows call GetLastError() didn't return an error code!", #MB_ICONERROR)
Else
InternetGetLastResponseInfo_(@ErrorCode, 0, @ErrorBufferSize)
If ErrorBufferSize > 0
ErrorBuffer = Space(ErrorBufferSize)
InternetGetLastResponseInfo_(@ErrorCode, @ErrorBuffer, @ErrorBufferSize)
MessageRequester("Error", RemoveString(ErrorBuffer, #CRLF$))
EndIf
EndIf
Else
If FTPFileHandle = 0
MessageRequester("Error", "The FTP Server didn't provide a data connection port!", #MB_ICONERROR)
Else
If ReadFile(1, #ClientFilename) = #False
MessageRequester("Error", "Opening of local file '" + #ClientFilename + "' failed!", #MB_ICONERROR)
Else
*FileBuffer = AllocateMemory(#FileBufferSize)
While Eof(1) = 0
NumBytesRead = ReadData(1, *FileBuffer, #FileBufferSize)
If InternetWriteFile_(FTPFileHandle, *FileBuffer, NumBytesRead, @NumBytesWritten) = #False
MessageRequester("Error", "Upload of local file to FTP Server failed!", #MB_ICONERROR)
AppendFailed = #True
Break
EndIf
Wend
If AppendFailed <> #True
MessageRequester("Info", "The local file '" + #ClientFilename + "' was successfully appended" + #CR$ + "to the server file " + #ServerFilename, #MB_ICONINFORMATION)
EndIf
EndIf
EndIf
EndIf
EndIf
If InternetCloseHandle_(FTPOpenHandle) = #False
MessageRequester("Error", "Closing of FTP connection failed!", #MB_ICONERROR)
EndIf
http://www.vbip.com/wininet/wininet-ftp-command-01.asp