Documentation » Using WinSCP » Guides » Scripting/Automation »

Converting PuTTY PSFTP or OpenSSH SFTP script to WinSCP script

This guide explains primarily how to convert existing SFTP file transfer script using PuTTY SFTP client (psftp.exe) to script using WinSCP, in case you want to use some advanced feature that WinSCP offers, such as synchronization, parametrized script, timestamped file names, custom directory listing format and others.

As the PSFTP syntax is based on OpenSSH sftp client,1 you can also use the guide to convert your existing Linux SFTP script to a Windows SFTP script.

Advertisement

Advertisement

Converting Command-Line

In your command that runs psftp.exe, substitute the psftp.exe with winscp.com.

The following table shows how to convert some psftp.exe parameters:

Parameter Conversion
[user@]host Specify the username and the hostname in a session URL in an open command in the script.
Example: open sftp://user@host/
-b file Replace with /script=file.
-bc Use an option echo on command at the beginning of the script.
-be Use an option batch on command at the beginning of the script (to counter the default option batch abort).
-load sessname Import your PuTTY stored session to WinSCP and use an open sessname command in the script. Though this makes the script dependent on GUI configuration (see below). You should better use a Generate Session URL/Code function and use the generated open command in the script.
-l user Specify the username in a session URL in an open command in the script.
Example: open sftp://user@example.com/
-P port Specify the port number in a session URL in an open command in the script.
Example: open sftp://example.com:port/
-pw passw Specify the password using -password switch in an open command in the script.
Example: open sftp://user@example.com/ -password=passw
-pwfile path Specify the password file path using -password switch in an open command in the script and enable reading the password from the file using -passwordsfromfiles switch.
Example: open sftp://user@example.com/ -password=path -passwordsfromfiles
-i key Specify the private key using -privatekey switch in an open command in the script.
Example: open sftp://user@example.com/ -privatekey=key
-hostkey aa:bb:cc:... Specify the expected hostkey using -hostkey switch in an open command in the script. With WinSCP, you should include key type and size in the signature. While they are not required, it is still recommended to add them as otherwise WinSCP may choose to use a different (better) host key algorithm and the checksum will not match (the same is actually true for PSFTP).
Example: open sftp://user@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc:..."
(assuming 2048-bit RSA host key)
See also Verifying the host key section below.

For example the following PSFTP command-line:

psftp.exe martin@example.com -pw password -hostkey aa:bb:cc:... -b script.txt 

converts to:

winscp.com /script=script.txt

with the script.txt containing open command at its beginning:

open sftp://martin@example.com/ -password=password -hostkey="ssh-rsa 2048 aa:bb:cc:..."

(assuming 2048-bit RSA host key)

Advertisement

Isolating Script from Graphical Mode

WinSCP in a scripting/console mode shares a configuration with a graphical mode by default. Particularly when implementing the script from scratch, it is recommended to isolate the script from the graphical mode configuration to avoid the script breaking, when the GUI configuration changes.

For that, add an /ini=nul command-line parameter:

winscp.com /script=script.txt /ini=nul

Verifying the Host Key

If your psftp.exe command-line does not use -hostkey switch, it means that you rely on PuTTY/PSFTP host key cache in Windows registry. That makes your script non-portable.

With WinSCP it’s recommended to always verify the host key explicitly using -hostkey switch of an open command.

Converting Commands

Many PSFTP commands work in WinSCP without any change.

The differences are described below.

WinSCP requires the script to end with the exit command for the process to close automatically.

cd

Use a cd command with the same argument.

chmod

Use a chmod command with the same arguments.

WinSCP supports octal permissions format only.

close

Use a close command.

del

Use a rm command with the same arguments.

Advertisement

dir

Use a ls command with the same argument.

Note that WinSCP also supports a dir alias.

get

Use a get command with the same arguments.

WinSCP downloads directories recursively by default. So remove a -r switch, if used.

When downloading file(s) to a specific directory, make sure you terminate a target path with a backslash:

get *.txt c:\path\

lcd

Use a lcd command with the same argument.

lpwd

Use a lpwd command.

mget

Use a get command.

The WinSCP command get can accept multiple files as its arguments, but the last argument needs to be a target path. Use .\ to download to the current local working directory (equivalent to the mget).

Example:

mget *.html *.txt

converts to:

get *.html *.txt .\

Note that WinSCP also supports a mget as an alias to the get command.

Advertisement

mkdir

Use a mkdir command with the same argument.

mput

Use a put command.

The WinSCP command put can accept multiple files as its arguments, but the last argument needs to be a target path. Use ./ to upload to the current remote working directory (equivalent to the mput).

Example:

mput *.html *.txt

converts to:

put *.html *.txt ./

Note that WinSCP also supports a mput as an alias to the put command.

mv

Use a mv command with the same arguments.

When moving file(s) to a different directory, make sure you terminate the target path with a slash:

mv *.txt /path/

PSFTP aliases to the mv command, a rename and a ren, convert the same way.

open

Use an open command.

For example the full PSFTP open command:

open user@host 22

Advertisement

converts to:

open sftp://user@host:22/

put

Use put command with the same arguments.

WinSCP uploads directories recursively by default. So remove a -r switch, if used.

When uploading file(s) to a specific directory, make sure you terminate the target path with a slash:

put *.txt /path/

pwd

Use a pwd command.

quit

Use an exit command.

Note that WinSCP also supports bye alias, just as PSFTP.

reget

WinSCP resumes SFTP transfers automatically. To explicitly resume transfer (for example with an FTP protocol), use get -resume.

reput

WinSCP resumes SFTP transfers automatically. To explicitly resume transfer (for example with an FTP protocol), use put -resume.

rmdir

Use a rmdir command with the same arguments.

! (exit to local shell)

WinSCP does not support exiting to a local shell. You need to exit WinSCP script; execute your local commands from a wrapping batch file; and start a new WinSCP script afterwards.

For more advanced tasks, consider using WinSCP .NET assembly from PowerShell script.

Advertisement

Example

A command psftp.exe -b script.txt with script.txt:

open user@example.com
cd /home/user
mget *.zip
close
quit

converts to a command winscp.com /script=script.txt /ini=nul with script.txt:

open sftp://user@example.com/
cd /home/user
get *.zip
close
exit
  1. Both the PSFTP command-line switches and the script commands are subset of OpenSSH sftp switches and commands.Back

Last modified: by martin