This is an old revision of the document!
Converting Windows FTP script to WinSCP SFTP script
This guide explains how to convert existing FTP 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 script using ftp.exe to WinSCP, in case you want to use some advanced feature that WinSCP offers, such as FTP over TLS/SSL, synchronization, passive mode1, parametrized script, timestamped file names, custom directory listing format, UTF-8 support, preserving file timestamp, and others.
Advertisement
- Converting Command-Line
- Isolating Script from Graphical Mode
- Converting Credentials
- Converting Commands
- Example
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=<logfile> parameter. |
-A |
SFTP servers typically do not allow anonymous login. With FTP protocol, use explicit anonymous username and some password if needed (typically in form of email address) with open command. |
-s:filename |
Use /script=filename parameter. Convert the script as documented 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
Isolating Script from Graphical Mode
WinSCP in scripting/console mode shares configuration with graphical mode by default. Particularly when implementing script from scratch, it is recommended to isolate the script from graphical mode configuration to avoid script breaking, when the configuration changes.
For that, add /ini=nul command-line parameter:
winscp.com /script=script.txt /ini=nul
Converting Credentials
The ftp.exe client, when hostname (host parameter) is specified on command line and unless -n parameter is used, needs username and password specified as the first lines of the script. With WinSCP it is recommended to specify hostname together with username and password as arguments to open command.
Advertisement
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:
open sftp://username:password@sftp.example.com/
Converting Commands
append
Use put -append <localfile> [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
Note that WinSCP also supports ascii command, but its use is deprecated.
bye
Use exit.
Note that WinSCP also supports bye alias.
binary
Use -transfer=binary switch for all following file transfer commands (put or get).
Advertisement
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 cd command.
close
Use close command.
delete
Use rm command.
Note that WinSCP also supports delete alias.
dir
Use ls command.
WinSCP does not support storing listing to a local file (second parameter of dir command).
Note that WinSCP also supports dir alias.
disconnect
Alias to close command.
get
Use get command.
Advertisement
You need to map previous binary or ascii commands to -transfer switch.
Example:
ascii get index.html
converts to:
get -transfer=ascii index.html
glob
In WinSCP, to escape special characters in filename, 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 lcd command.
literal
Where are no textual 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.
Advertisement
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.
Example:
ascii mget *.html *.txt
converts to:
get -transfer=ascii *.html *.txt .\
Note that WinSCP also supports mget as an alias to get command.
mkdir
Use mkdir command.
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 upload to current remote working directory (equivalent to mput).
Note that put command never prompts before individual file transfer.
Example:
ascii mput *.html *.txt
converts to:
put -transfer=ascii *.html *.txt ./
Advertisement
Note that WinSCP also supports mput as an alias to put command.
open
Use open command.
Credentials specified on separate lines of ftp.exe script (following open command) need to convert to part of session URL parameter of WinSCP open command. See converting credentials above. The same applies to username specified using user command.
Example:
open ftp.example.com username password
or equivalent (when ftp.exe -n command-line parameter is used):
open ftp.example.com user username password
convert to:
open sftp://username:password@sftp.example.com/
prompt
WinSCP does not prompt for individual transfers during multi-file transfers, so there’s no conversion needed.
put
Use put command.
You need to map previous binary or ascii commands to -transfer switch.
Example:
ascii put index.html
Advertisement
converts to:
put -transfer=ascii index.html
pwd
Use pwd command.
quit
Alias to bye command.
quote
Alias to literal command.
recv
Alias to get command.
Note that WinSCP also supports recv alias.
rename
Use mv command.
Note that WinSCP also supports rename alias.
rmdir
Use rmdir command.
send
Alias to put command.
Note that WinSCP also supports send alias.
type
Command type ascii is an alias to ascii command.
Advertisement
Command type binary is an alias to binary command.
user
In WinSCP, username is specified as part of session URL parameter of WinSCP open command.
Example:
open ftp.example.com user username password
converts to:
open sftp://username:password@sftp.example.com/
verbose
There is no direct equivalent.
You can instead enable session logging using /log=<logfile> command-line parameter:
winscp.com /script=script.txt /ini=nul /log=session.log
! (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.
Example
Command ftp.exe -s:script.txt with script.txt:
prompt open ftp.example.com username password cd /home/user binary mget *.zip close quit
Advertisement
converts to command winscp.com /script=script.txt /ini=nul with script.txt:
open sftp://username:password@sftp.example.com/ cd /home/user get -transfer=binary *.zip close exit
- WinSCP defaults to passive mode.Back