Topic "Appending to Existing File using WinSCP Command Line"

Author Message
James Wendt

Guest


I have found several references to appending data to existing files with WinSCP but I have been unable to get it to work. I'm technical enough to be dangerous but I can follow directions so I hope somebody can give me the steps I need to make so that I can follow them.

My goal is as follows:

    Write log data to a file called log.txt on a Windows server
    Automatically upload log.txt to a remote SFTP server using WinSCP triggered by a timed event
    Once the file is uploaded, a process on the remote server will automatically grab and delete the remote log.txt file
    After upload, the local script will delete the log.txt file on the Windows machine

The process I listed above can be handled easily but, if the remote process has NOT grabbed and deleted the log.txt file before the local process is automatically triggered again, I will overwrite the remote file, causing data loss.

If the remote log.txt file already exists on the SFTP server, I need to be able to append whatever new data is contained in the log.txt file on the local Windows machine to the existing log.txt file on the remote server.

Example:

log.txt file on remote server contains:

1111
2222
3333
4444

log.txt on the local server contains:

5555
6666
7777

I want to append the data from the local file I am sending to the remote file so the end result is a log.txt file on the remote server that contains:

1111
2222
3333
4444
5555
6666
7777

I hope I explained this clearly.

I have been digging around in the forums for an answer and I see instructions that get me close but I am hoping somebody can clearly document the steps I need to take to make this happen.

Thank you for your time and I look forward to your answer.

J
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
I suppose you need -append switch of put command:
https://winscp.net/eng/docs/scriptcommand_put
_________________
Martin Prikryl
James Wendt

Guest


I have this working but now have basic question number two to ask...

Once I have confirmation that the file has sent, I'd like to move the local file to an archive directory.

How do I continue the script or what is a batch file command that would enable to me say, "If file XYZ was successfully transmitted from the local machine to the remote SFTP server, move file XYZ to archive folder"?

My current script looks like this:

option batch abort
option confirm off
open sftp://username:password@server.somewhere.com
put c:\folder1\folder1\test.txt
exit


Thank you.

Jim

prikryl wrote:
I suppose you need -append switch of put command:
https://winscp.net/eng/docs/scriptcommand_put
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
See https://winscp.net/eng/docs/script_local_move_after_successful_upload#using_winscp_scripting
_________________
Martin Prikryl
chal4oye
[View user's profile]

Joined: 2014-01-16
Posts: 1
Location: Pakistan
Write log data to a file called log.txt on a Windows server
Automatically upload log.txt to a remote SFTP server using WinSCP triggered by a timed event
Once the file is uploaded, a process on the remote server will automatically grab and delete the remote log.txt file
After upload, the local script will delete the log.txt file on the Windows machine

The process I listed above can be handled easily but, if the remote process has NOT grabbed and deleted the log.txt file before the local process is automatically triggered again, I will overwrite the remote file, causing data loss.

If the remote log.txt file already exists on the SFTP server, I need to be able to append whatever new data is contained in the log.txt file???
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
chal4oye wrote:
If the remote log.txt file already exists on the SFTP server, I need to be able to append whatever new data is contained in the log.txt file???

Use -append switch of put command:
https://winscp.net/eng/docs/scriptcommand_put
_________________
Martin Prikryl
Guest




Is it possible to append files using the .net classes?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Anonymous wrote:
Is it possible to append files using the .net classes?

It's not supported yet.

All you can do is to download the remote file, append the contents locally and upload a whole file back.

Or just modify the source code of .NET assembly to add the -append switch.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License