Archived Forum Post

Index of archived forum posts

Question:

SFTP Append to File on Server

Apr 21 '16 at 10:23

We are using the Sftp functions for now and that is working fine for now. But what I also need to do is append some text or a text file to a file already on our website. In looking at the VB6 examples, it looks like the write text file does that? I want to make sure it does not overwrite the text file already on the server, but appends the new text to it.


Answer

The SFTP protocol specification is modeled somewhat like the typical local file IO functions a program might use. You open a file, write to it or read to it, and then close it. At the time of opening, you specify the behavior - which might be to open for append, where the file pointer is automatically at the end of the file and any data written is appended, or open for truncate, where the file, if it already exists, is opened and the file pointer is positioned at 0.

The SFTP protocol is the same, except that you're dealing with remote files over an SSH connection. The SFtp.OpenFile method has the createDisposition argument that controls how the file is opened (i.e. truncate, append, etc.) Of course, you'll want to test it first -- don't just trust that it'll behave as you expect. I'm confident Chilkat will send the desired create disposition options according to the SFTP protocol w/ the open request, but any given SFTP server implementation may not implement the full range of options available in the protocol. (I would hope the basic "append" disposition is supported by all servers..)