Can pscp transfer to a temporary file and rename once done?

1.2k views Asked by At

I have a very big file that has to be transferred to a remote server.

On that remote server there is a job activating each 5 min that, once sees a file name starting with the right prefix, processes it.

What happens if the job "wakes up" in the middle of transfer? In that case it would process a corrupted file.

Do pscp create a .temp file and renames it accordingly to account for that? Or do I have to handle this manually?

1

There are 1 answers

0
Martin Prikryl On BEST ANSWER

No pscp does not transfer the files via a temporary file.

You would have to use another SFTP client – If you use pscp as SFTP client. The pscp defaults to SFTP, but it falls back to SCP, if SFTP is not available. If you need to use SCP (what is rare), you cannot do this, as SCP protocol does not support file rename.


Either an SFTP client that at least supports file rename – Upload explicitly to a temporary file name and rename afterwards. For that you can use psftp from PuTTY package, with its put and mv commands:

open user@hostname
put C:\source\path\file.zip /destination/path/file.tmp
mv /destination/path/file.tmp /destination/path/file.zip
exit

Or use an SFTP client that can upload a files via a temporary file automatically. For example WinSCP can do that. By default it does for files over 100 KB only. If your files are smaller, you can configure it to do it for all files using the -resumesupport switch.

An example batch file that forces an upload of a file via a temporary file:

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /log="C:\writable\path\to\log\WinSCP.log" /ini=nul ^
  /command ^
    "open sftp://username:[email protected]/ -hostkey=""ssh-ed25519 255 ...=""" ^
    "put -resumesupport=on C:\source\path\file.zip /destination/path/" ^
    "exit"

The code was generated by WinSCP GUI with the "Transfer to temporary filename" options set to "All files".

See also WinSCP article Locking files while uploading / Upload to temporary file name.

(I'm the author of WinSCP)


Related question: SFTP file lock mechanism.