This is an old revision of the document!

Documentation » Using WinSCP » Guides » Scripting/Automation »

Converting PuTTY PSFTP script to WinSCP script

This guide explains 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.

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.
-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 in a session URL in an open command in the script.
Example: open sftp://user:passw@example.com/
-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. WinSCP requires key type and size in the signature.
Example: open sftp://user@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc..."
(assuming 2048-bit RSA host key)

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:password@example.com/ -hostkey="ssh-rsa 2048 aa:bb:cc:..."

(assuming 2048-bit RSA host key)

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.

Advertisement

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

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

Converting Commands

Many PSFTP commands work in WinSCP without any change.

The differences are described below.

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.

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\

Advertisement

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.

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

Advertisement

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

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.

Advertisement

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.

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

Advertisement

Last modified: by martin