This is an old revision of the document!
Converting Windows FTP script to WinSCP SFTP script
This guide explains how to convert existing file transfer script using Windows built-in command line FTP client (ftp.exe
) to SFTP script using WinSCP.
You can also use it to convert ftp.exe
script to FTP script using WinSCP, in case you want to use some advanced feature that WinSCP offers (such as synchronization).
Advertisement
Advertisement
Converting Command-Line
In your command that runs ftp.exe
substitute ftp.exe
with winscp.com
.
Following table shows how to convert some ftp.exe
parameters:
Parameter | Conversion |
---|---|
-n |
There is no equivalent. In WinSCP all of hostname, username and password are specified using single command open . See also converting credentials below. |
-i |
WinSCP does not prompt for individual transfers during multi-file transfers, so there’s no conversion needed. |
-d |
Enable session logging using /log=logpath parameter. |
-A |
SFTP servers typically do not allow anonymous login. |
-s:filename |
Use /script=filename parameter, see below. |
host |
While WinSCP also allows specifying hostname on command-line, recommended is to use open host command in the WinSCP script. |
Other parameters can be typically ignored.
For example following command:
ftp.exe -s:script.txt
converts to:
winscp.com /script=script.txt
Setting up Batch Mode
As opposite to ftp.exe
, WinSCP by default uses interactive mode, even when script is used. So your WinSCP script should typically start with following commands to enable batch mode and to turn off overwrite confirmations:
option batch abort option confirm off
Advertisement
Read more details about using scripting.
Converting Credentials
The ftp.exe
client, when hostname is specified on command line and unless -n
parameter is used, needs username and password specified as first commands of the script. With WinSCP it is not recommended to specify hostname on command-line and username and password come as arguments to open
command.
For example, command ftp.exe ftp.example.com -s:script.txt
, with script.txt
starting:
username password ...
converts to command winscp.com /script=script.txt
, with script.txt starting:
option batch abort option confirm off open sftp://username:password@sftp.example.com
For option
commands, see above.
Converting Commands
append
Use put -append <localhost> [remotefile]
.
ascii
Use -transfer=ascii
switch for all following file transfer commands (put
or get
).
Example:
ascii put file.txt
converts to:
put -transfer=ascii file.txt
Advertisement
Note that WinSCP also supports ascii
command, but its use is deprecated.
bye
Use exit
.
binary
Use -transfer=binary
switch for all following file transfer commands (put
or get
).
Example:
binary put file.txt
converts to:
put -transfer=binary file.txt
Note that WinSCP also supports binary
command, but its use is deprecated.
cd
Use same syntax in WinSCP script.
close
Use same syntax in WinSCP script.
delete
Use rm
command (note that delete
alias is also supported).
dir
Use ls
command.
WinSCP does not support storing listing to a local file (second parameter of dir
command).
disconnect
Use close
command.
Advertisement
get
Use same syntax in WinSCP script.
glob
In WinSCP, to escape special characters in file mask, use set pattern.
For example to download file with literal *
character use get filewithstar[*]
.
hash
WinSCP always prints percentual progress of file transfer when run in console. It does not output progress when redirected to a file.
lcd
Use same syntax in WinSCP script.
literal
Where are no literal commands in SFTP protocol. You can execute remote shell commands using call
command. With FTP protocol, you can use call
command to execute FTP protocol commands.
ls
You can use ls
command to list directory contents with full details.
mdelete
Use rm
command.
mget
Use get
command.
You need to map previous binary
or ascii
commands to -transfer
switch.
WinSCP command get
can accept multiple files as its arguments, but the last argument needs to be target path. Use .\
to download to current local working directory (equivalent to mget
).
Note that get
command never prompts before individual file transfer.
Advertisement
Example:
binary mget index.html about.html
converts to:
get -transfer=binary index.html about.html .\
mkdir
Use same syntax in WinSCP script.
mput
Use put
command.
You need to map previous binary
or ascii
commands to -transfer
switch.
WinSCP command put
can accept multiple files as its arguments, but the last argument needs to be target path. Use ./
to download to current remote working directory (equivalent to mput
).
Note that get
command never prompts before individual file transfer.
Example:
binary mput index.html about.html
converts to:
put -transfer=binary index.html about.html ./
! (exit to local shell)
WinSCP does not support exiting to 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