Differences

This shows you the differences between the selected revisions of the page.

guide_ftp_script_to_sftp 2014-01-15 guide_ftp_script_to_sftp 2022-10-21 (current)
Line 1: Line 1:
====== Converting Windows FTP script to WinSCP SFTP script ====== ====== Converting Windows FTP script to WinSCP SFTP script ======
-This guide explains how to convert existing FTP file transfer script using [[http://technet.microsoft.com/en-us/library/bb490910.aspx|Windows built-in command-line FTP client]] (''ftp.exe'') to SFTP script using WinSCP.+This guide explains how to convert existing FTP file transfer script using [[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/ftp|Windows built-in command-line FTP client]] (''ftp.exe'') to SFTP [[scripting|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 [[scriptcommand_synchronize|synchronization]]).+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 [[ftps|FTP over TLS/SSL]], [[scriptcommand_synchronize|synchronization]], passive mode,((WinSCP defaults to passive mode.)) [[script_upload_multiple_servers|parametrized script]], [[scripting#timestamp|timestamped file names]], [[script_custom_listing_format_csv|custom directory listing format]], UTF-8 support, preserving file timestamp, recursive downloads and uploads, and others.
===== [[commandline]] Converting Command-Line ===== ===== [[commandline]] Converting Command-Line =====
Line 10: Line 10:
^ Parameter ^ Conversion ^ ^ Parameter ^ Conversion ^
-| ''-n'' | There is no equivalent. In WinSCP all of hostname, username and password are specified using single command ''[[scriptcommand_open|open]]''. See also [[guide_ftp_script_to_sftp#credentials|converting credentials]] below. |+| ''-n'' | There is no equivalent. In WinSCP all of hostname, username and password are specified using single command ''[[scriptcommand_open|open]]''. See also [[#credentials|converting credentials]] below. |
| ''-i'' | WinSCP does not prompt for individual transfers during multi-file transfers, so there's no conversion needed. | | ''-i'' | WinSCP does not prompt for individual transfers during multi-file transfers, so there's no conversion needed. |
-| ''-d'' | Enable [[logging|session logging]] using ''/log=<logfile>'' parameter. |+| ''-d'' | Enable [[logging|session logging]] using ''[[commandline#logging|/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 ''[[scriptcommand_open|open]]'' command. | | ''-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 ''[[scriptcommand_open|open]]'' command. |
-| ''-s:filename'' | Use ''/script=filename'' parameter. Convert the script as documented below. |+| ''-s:filename'' | Use ''[[commandline#scripting|/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. | | ''host'' | While WinSCP also allows specifying hostname on command-line, recommended is to use ''open host'' command in the WinSCP script. |
Line 27: Line 27:
converts to: converts to:
-<code>+<code batch>
winscp.com /script=script.txt winscp.com /script=script.txt
</code> </code>
- 
-===== [[batch]] 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: 
- 
-<code> 
-option batch abort 
-option confirm off 
-</code> 
- 
-Read more details about [[scripting#using_scripting|using scripting]]. 
===== [[ini]] Isolating Script from Graphical Mode ===== ===== [[ini]] Isolating Script from Graphical Mode =====
Line 45: Line 35:
WinSCP in scripting/console mode shares [[config|configuration]] with [[interfaces|graphical mode]] by default. Particularly when implementing script from scratch, it is recommended to [[scripting#configuration|isolate the script from graphical mode configuration]] to avoid script breaking, when the configuration changes. WinSCP in scripting/console mode shares [[config|configuration]] with [[interfaces|graphical mode]] by default. Particularly when implementing script from scratch, it is recommended to [[scripting#configuration|isolate the script from graphical mode configuration]] to avoid script breaking, when the configuration changes.
-For that, add ''/ini=nul'' command-line parameter:+For that, add ''[[commandline#configuration|/ini=nul]]'' command-line parameter:
-<code>+<code batch>
winscp.com /script=script.txt /ini=nul winscp.com /script=script.txt /ini=nul
</code> </code>
===== [[credentials]] Converting Credentials ===== ===== [[credentials]] 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 not recommended to specify hostname on command-line and username and password come as arguments to ''[[scriptcommand_open|open]]'' command.+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 ''[[scriptcommand_open|open]]'' command.
For example, command ''ftp.exe %%ftp.example.com%% -s:script.txt'', with ''script.txt'' starting: For example, command ''ftp.exe %%ftp.example.com%% -s:script.txt'', with ''script.txt'' starting:
Line 62: Line 52:
</code> </code>
-converts to command ''winscp.com /script=script.txt'', with script.txt starting:+converts to command ''winscp.com /script=script.txt'', with ''script.txt'' starting:
<code winscp> <code winscp>
-option batch abort +open sftp://username:password@sftp.example.com/
-option confirm off +
-open sftp://username:password@sftp.example.com+
</code> </code>
-For ''option'' commands, see [[guide_ftp_script_to_sftp#batch|above]].+Replace the ''%%sftp://%%'' with the ''%%ftp://%%'', if you keep using the FTP protocol. 
 + 
 +===== [[errors]] Error Handling ===== 
 +WinSCP by default aborts the script when some of the commands fail. On the contrary ''ftp.exe'' continues processing a script after an error. If your original ''ftp.exe'' script relies on this behavior, you can emulate it in WinSCP by switching the [[scriptcommand_option#batch|batch mode]] to ''on'' at the beginning of your script: 
 +<code winscp> 
 +option batch on 
 +</code>
===== Converting Commands ===== ===== Converting Commands =====
-==== append ==== 
-Use ''[[scriptcommand_put|put]] -append <localhost> [remotefile]''. 
-==== ascii ====+==== [[append]] append ==== 
 +Use ''[[scriptcommand_put|put]] -append <localfile> [remotefile]''. 
 + 
 +The ''-append'' switch is supported with the SFTP protocol only. 
 + 
 +==== [[ascii]] ascii ====
Use ''-transfer=ascii'' switch for all following file transfer commands (''[[scriptcommand_put|put]]'' or ''[[scriptcommand_get|get]]''). Use ''-transfer=ascii'' switch for all following file transfer commands (''[[scriptcommand_put|put]]'' or ''[[scriptcommand_get|get]]'').
Line 94: Line 91:
Note that WinSCP also supports ''ascii'' command, but its use is deprecated. Note that WinSCP also supports ''ascii'' command, but its use is deprecated.
-==== bye ====+==== [[bye]] bye ====
Use ''[[scriptcommand_exit|exit]]''. Use ''[[scriptcommand_exit|exit]]''.
Note that WinSCP also supports ''bye'' alias. Note that WinSCP also supports ''bye'' alias.
-==== binary ====+==== [[binary]] binary ====
Use ''-transfer=binary'' switch for all following file transfer commands (''[[scriptcommand_put|put]]'' or ''[[scriptcommand_get|get]]''). Use ''-transfer=binary'' switch for all following file transfer commands (''[[scriptcommand_put|put]]'' or ''[[scriptcommand_get|get]]'').
Line 117: Line 114:
Note that WinSCP also supports ''binary'' command, but its use is deprecated. Note that WinSCP also supports ''binary'' command, but its use is deprecated.
-==== cd ====+==== [[cd]] cd ====
Use ''[[scriptcommand_cd|cd]]'' command. Use ''[[scriptcommand_cd|cd]]'' command.
-==== close ====+==== [[close]] close ====
Use ''[[scriptcommand_close|close]]'' command. Use ''[[scriptcommand_close|close]]'' command.
-==== delete ====+==== [[delete]] delete ====
Use ''[[scriptcommand_rm|rm]]'' command. Use ''[[scriptcommand_rm|rm]]'' command.
Note that WinSCP also supports ''delete'' alias. Note that WinSCP also supports ''delete'' alias.
-==== dir ====+==== [[dir]] dir ====
Use ''[[scriptcommand_ls|ls]]'' command. Use ''[[scriptcommand_ls|ls]]'' command.
Line 135: Line 132:
Note that WinSCP also supports ''dir'' alias. Note that WinSCP also supports ''dir'' alias.
-==== disconnect ====+==== [[disconnect]] disconnect ====
Alias to ''[[scriptcommand_close|close]]'' command. Alias to ''[[scriptcommand_close|close]]'' command.
-==== get ====+==== [[get]] get ====
Use ''[[scriptcommand_get|get]]'' command. Use ''[[scriptcommand_get|get]]'' command.
-You need to map previous ''[[guide_ftp_script_to_sftp#binary|binary]]'' or ''[[guide_ftp_script_to_sftp#ascii|ascii]]'' commands to ''-transfer'' switch.+You need to map previous ''[[#binary|binary]]'' or ''[[#ascii|ascii]]'' commands to ''-transfer'' switch.
Example: Example:
Line 156: Line 153:
</code> </code>
-==== glob ==== +==== [[glob]] glob ==== 
-In WinSCP, to escape special characters in filename, use [[file_mask#basic_syntax|set pattern]].+In WinSCP, to escape special characters in filename, use [[file_mask#basic|set pattern]].
For example to download file with literal ''*'' character, use ''get filewithstar[*]''. For example to download file with literal ''*'' character, use ''get filewithstar[*]''.
-==== hash ====+==== [[hash]] hash ====
WinSCP always prints percentual progress of file transfer when run in console. It does not output progress when redirected to a file. WinSCP always prints percentual progress of file transfer when run in console. It does not output progress when redirected to a file.
-==== lcd ====+==== [[lcd]] lcd ====
Use ''[[scriptcommand_lcd|lcd]]'' command. Use ''[[scriptcommand_lcd|lcd]]'' command.
-==== literal ==== +==== [[literal]] literal ==== 
-Where are no textual commands in SFTP protocol. You can execute [[remote_command|remote shell commands]] using ''[[scriptcommand_call|call]]'' command.+There are no textual commands in SFTP protocol. You can execute [[remote_command|remote shell commands]] using ''[[scriptcommand_call|call]]'' command.
With FTP protocol, you can use ''call'' command to execute FTP protocol commands. With FTP protocol, you can use ''call'' command to execute FTP protocol commands.
-==== ls ====+==== [[ls]] ls ====
You can use ''[[scriptcommand_ls|ls]]'' command to list directory contents with full details. You can use ''[[scriptcommand_ls|ls]]'' command to list directory contents with full details.
-==== mdelete ====+==== [[mdelete]] mdelete ====
Use ''[[scriptcommand_rm|rm]]'' command. Use ''[[scriptcommand_rm|rm]]'' command.
-==== mget ====+==== [[mget]] mget ====
Use ''[[scriptcommand_get|get]]'' command. Use ''[[scriptcommand_get|get]]'' command.
-You need to map previous ''[[guide_ftp_script_to_sftp#binary|binary]]'' or ''[[guide_ftp_script_to_sftp#ascii|ascii]]'' commands to ''-transfer'' switch.+You need to map previous ''[[#binary|binary]]'' or ''[[#ascii|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''). 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'').
Line 191: Line 188:
<code> <code>
ascii ascii
-mget index.html about.html+mget *.html *.txt
</code> </code>
Line 197: Line 194:
<code winscp> <code winscp>
-get -transfer=ascii index.html about.html .\+get -transfer=ascii *.html *.txt .\
</code> </code>
Note that WinSCP also supports ''mget'' as an alias to ''get'' command. Note that WinSCP also supports ''mget'' as an alias to ''get'' command.
-==== mkdir ====+==== [[mkdir]] mkdir ====
Use ''[[scriptcommand_mkdir|mkdir]]'' command. Use ''[[scriptcommand_mkdir|mkdir]]'' command.
-==== mput ==== +==== [[mput]] mput ==== 
-Use ''[[scriptcommand_get|put]]'' command.+Use ''[[scriptcommand_put|put]]'' command.
-You need to map previous ''[[guide_ftp_script_to_sftp#binary|binary]]'' or ''[[guide_ftp_script_to_sftp#ascii|ascii]]'' commands to ''-transfer'' switch.+You need to map previous ''[[#binary|binary]]'' or ''[[#ascii|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''). 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'').
Line 218: Line 215:
<code> <code>
ascii ascii
-mput index.html about.html+mput *.html *.txt
</code> </code>
Line 224: Line 221:
<code winscp> <code winscp>
-put -transfer=ascii index.html about.html ./+put -transfer=ascii *.html *.txt ./
</code> </code>
Note that WinSCP also supports ''mput'' as an alias to ''put'' command. Note that WinSCP also supports ''mput'' as an alias to ''put'' command.
-==== open ====+==== [[open]] open ====
Use ''[[scriptcommand_open|open]]'' command. Use ''[[scriptcommand_open|open]]'' command.
-Credentials specified on separate lines of ''ftp.exe'' script (following ''open'' command) need to convert to part of [[session_url|session URL]] parameter of WinSCP ''open'' command. See [[guide_ftp_script_to_sftp#credentials|converting credentials]] above. The same applies to username specified using ''[[guide_ftp_script_to_sftp#user|user]]'' command.+Credentials specified on separate lines of ''ftp.exe'' script (following ''open'' command) need to convert to part of [[session_url|session URL]] parameter of WinSCP ''open'' command. See [[#credentials|converting credentials]] above. The same applies to username specified using ''[[#user|user]]'' command.
Example: Example:
Line 250: Line 247:
</code> </code>
-convert to:+converts to:
<code winscp> <code winscp>
-open sftp://username:password@sftp.example.com+open sftp://username:password@sftp.example.com/
</code> </code>
-==== prompt ====+Replace the ''%%sftp://%%'' with the ''%%ftp://%%'', if you keep using the FTP protocol. 
 + 
 +==== [[prompt]] prompt ====
WinSCP does not prompt for individual transfers during multi-file transfers, so there's no conversion needed. WinSCP does not prompt for individual transfers during multi-file transfers, so there's no conversion needed.
-==== put ====+==== [[put]] put ====
Use ''[[scriptcommand_put|put]]'' command. Use ''[[scriptcommand_put|put]]'' command.
-You need to map previous ''[[guide_ftp_script_to_sftp#binary|binary]]'' or ''[[guide_ftp_script_to_sftp#ascii|ascii]]'' commands to ''-transfer'' switch.+You need to map previous ''[[#binary|binary]]'' or ''[[#ascii|ascii]]'' commands to ''-transfer'' switch.
Example: Example:
Line 278: Line 277:
</code> </code>
-==== pwd ====+==== [[pwd]] pwd ====
Use ''[[scriptcommand_pwd|pwd]]'' command. Use ''[[scriptcommand_pwd|pwd]]'' command.
-==== quit ==== +==== [[quit]] quit ==== 
-Alias to ''[[guide_ftp_script_to_sftp#bye|bye]]'' command.+Alias to ''[[#bye|bye]]'' command.
-==== quote ==== +==== [[quote]] quote ==== 
-Alias to ''[[guide_ftp_script_to_sftp#literal|literal]]'' command.+Alias to ''[[#literal|literal]]'' command.
-==== recv ==== +==== [[recv]] recv ==== 
-Alias to ''[[guide_ftp_script_to_sftp#get|get]]'' command.+Alias to ''[[#get|get]]'' command.
Note that WinSCP also supports ''recv'' alias. Note that WinSCP also supports ''recv'' alias.
-==== rename ====+==== [[rename]] rename ====
Use ''[[scriptcommand_mv|mv]]'' command. Use ''[[scriptcommand_mv|mv]]'' command.
Note that WinSCP also supports ''rename'' alias. Note that WinSCP also supports ''rename'' alias.
-==== rmdir ====+==== [[rmdir]] rmdir ====
Use ''[[scriptcommand_rmdir|rmdir]]'' command. Use ''[[scriptcommand_rmdir|rmdir]]'' command.
-==== send ==== +==== [[send]] send ==== 
-Alias to ''[[guide_ftp_script_to_sftp#put|put]]'' command.+Alias to ''[[#put|put]]'' command.
Note that WinSCP also supports ''send'' alias. Note that WinSCP also supports ''send'' alias.
-==== type ==== +==== [[type]] type ==== 
-Command ''type ascii'' is an alias to ''[[guide_ftp_script_to_sftp#ascii|ascii]]'' command.+Command ''type ascii'' is an alias to ''[[#ascii|ascii]]'' command.
-Command ''type binary'' is an alias to ''[[guide_ftp_script_to_sftp#binary|binary]]'' command.+Command ''type binary'' is an alias to ''[[#binary|binary]]'' command.
-==== user ====+==== [[user]] user ====
In WinSCP, username is specified as part of [[session_url|session URL]] parameter of WinSCP ''open'' command. In WinSCP, username is specified as part of [[session_url|session URL]] parameter of WinSCP ''open'' command.
Line 324: Line 323:
<code winscp> <code winscp>
-open sftp://username:password@sftp.example.com+open sftp://username:password@sftp.example.com/
</code> </code>
-==== verbose ====+Replace the ''%%sftp://%%'' with the ''%%ftp://%%'', if you keep using the FTP protocol. 
 + 
 +==== [[verbose]] verbose ====
There is no direct equivalent. There is no direct equivalent.
-You can instead enable [[logging|session logging]] using ''/log=<logfile>'' [[commandline|command-line]] parameter:+You can instead enable [[logging|session logging]] using ''[[commandline#logging|/log=<logfile>]]'' command-line parameter:
-<code>+<code batch>
winscp.com /script=script.txt /ini=nul /log=session.log winscp.com /script=script.txt /ini=nul /log=session.log
</code> </code>
Line 358: Line 359:
<code winscp> <code winscp>
-option batch abort +open sftp://username:password@sftp.example.com/
-option confirm off +
-open sftp://username:password@sftp.example.com+
cd /home/user cd /home/user
get -transfer=binary *.zip get -transfer=binary *.zip
Line 366: Line 365:
exit exit
</code> </code>
 +
 +Replace the ''%%sftp://%%'' with the ''%%ftp://%%'', if you keep using the FTP protocol.

Last modified: by martin