Guide

Automating file transfers

A script repeats a session you have already proved by hand. It will not ask before replacing a file.

ExamplePlaceholders only. Do not put a real password in the file.
open sftp://[email protected]/ -hostkey="ssh-ed25519 255 SHA256:verified-fingerprint"
lcd C:\Projects\site
cd /var/www/html
put index.html
exit
Run it from winscp.com with /ini=nul and /log until the log shows this path. Batch mode will not ask before replacing a file.

The situation

The same upload or comparison happens often enough that clicking is the risky part. You want a command that stops when something is wrong.

Prerequisites

People already writing .NET code can use the documented assembly. Session.SynchronizeDirectories maps to synchronize. This page stays with the script file.

Write and test a script

  1. One file first open, lcd, cd, put or get, exit.
  2. Console winscp.com /ini=nul /script=transfer.txt /log=transfer.log from an existing window so errors stay on screen.
  3. Read the log Check the path and the size before you add more files.
  4. Preview a sync synchronize remote -preview does not transfer. Add -delete only to remove remote files that are absent locally. It is ignored for both.
  5. Schedule later The task’s account and starting directory are a common reason a desktop script fails at night.

/script and /command imply batch mode, so overwrite questions are off. A failed command stops the script.

Example script

The console above is the example. Replace the host, the user, the fingerprint, and the paths. The URL syntax has a place for a password. Leave it empty. A script that contains one will leak it to anyone who can read the file or the scheduled task.

Expected result

The console exits without an error. The log shows open, the directory change, and the transfer. index.html on the server matches the local size. A preview run changes nothing. Use winscp.com from Command Prompt or PowerShell. winscp.exe can open a console that closes before you read it.

Common mistakes

Security

The durable pattern is a private key plus -hostkey set to the fingerprint you verified. A key with no passphrase can be used by anyone who copies the file. The documentation allows that only as a special case. For FTPS, -certificate pins the certificate you expect. Do not drop to plain FTP to avoid that line.

Troubleshooting

If no log appears, the program failed before the session. Read the console. Relative paths in a scheduled task resolve against the task’s starting directory. /loglevel=* can record the password. Leave it off if the log might be shared. A normal /log is enough.

Winsaip is an independent educational resource and is not affiliated with or endorsed by the developers of WinSCP.