Skip to Content

WinSCP scripts and batch files

Anything you do in WinSCP by hand can run unattended from a script. The classic pair is a script file (WinSCP commands) and a batch file that runs it and checks the result.

Task Scheduler starts the batch file, the batch file runs winscp.com with the script, the log and exit code tell you whether it worked.
Task Scheduler starts the batch file, the batch file runs winscp.com with the script, the log and exit code tell you whether it worked.

Let WinSCP write the script for you

  1. In the Login dialog select your site → Manage → Generate Session URL/Code.
  2. Choose the Script tab, format Batch file. WinSCP writes the exact open line including the -hostkey fingerprint, so the script never stops to ask.
  3. Or in Commander: select files → Transfer Settings → Generate Code for a ready put/get.

backup.txt — the WinSCP script

open sftp://student@203.0.113.10/ -hostkey="ssh-ed25519 255 SHA256:LA+auhZNrl+orHphcUOZ/FeRoAg/7Au9Kh59b5OHm3w" -privatekey="C:\Users\you\.ssh\id_ed25519.ppk"
lcd C:\work\site
cd /home/student/site
synchronize remote -delete -criteria=time
get /var/log/app/*.log C:\work\logs\
exit

backup.bat — the runner

@echo off
set WINSCP="C:\Program Files (x86)\WinSCP\WinSCP.com"
%WINSCP% /ini=nul /log="C:\work\backup.log" /script="C:\work\backup.txt"
if %ERRORLEVEL% neq 0 (
  echo FAILED - see C:\work\backup.log
) else (
  echo OK
)
  • winscp.com (not .exe) is the console version for scripts.
  • Check the install path on your PC: where /r "C:\Program Files*" winscp.com.
  • /ini=nul ignores your GUI settings so the script behaves the same on every PC.
  • Exit code 0 = everything succeeded. Anything else = read the log.
  • A key with a passphrase cannot be typed by a scheduled job: run Pageant at logon, or use a separate key without a passphrase that is allowed only for this job (see next lesson).

Try it — first script

  1. Generate the open line for your lab site.
  2. Write a script that uploads hello.txt and downloads /etc/os-release.
  3. Run the batch file twice and read backup.log; then break the host name and confirm you get FAILED.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.