Differences
This shows you the differences between the selected revisions of the page.
2014-01-14 | 2014-01-15 | ||
draft (martin) | completed (martin) | ||
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 file transfer script using 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 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 [[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 [[scriptcommand_synchronize|synchronization]]). |
- | ===== Converting Command-Line ===== | + | ===== [[commandline]] Converting Command-Line ===== |
In your command that runs ''ftp.exe'' substitute ''ftp.exe'' with ''[[executables|winscp.com]]''. | In your command that runs ''ftp.exe'' substitute ''ftp.exe'' with ''[[executables|winscp.com]]''. | ||
Line 12: | Line 12: | ||
| ''-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 [[guide_ftp_script_to_sftp#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=logpath'' parameter. | | + | | ''-d'' | Enable [[logging|session logging]] using ''/log=<logfile>'' parameter. | |
- | | ''-A'' | SFTP servers typically do not allow anonymous login. | | + | | ''-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, see below. | | + | | ''-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. | | | ''host'' | While WinSCP also allows specifying hostname on command-line, recommended is to use ''open host'' command in the WinSCP script. | | ||
Line 40: | Line 40: | ||
Read more details about [[scripting#using_scripting|using scripting]]. | Read more details about [[scripting#using_scripting|using scripting]]. | ||
+ | |||
+ | ===== [[ini]] Isolating Script from Graphical Mode ===== | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <code> | ||
+ | winscp.com /script=script.txt /ini=nul | ||
+ | </code> | ||
===== [[credentials]] Converting Credentials ===== | ===== [[credentials]] 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 ''[[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 not recommended to specify hostname on command-line and username and password come 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 86: | Line 96: | ||
==== bye ==== | ==== bye ==== | ||
Use ''[[scriptcommand_exit|exit]]''. | Use ''[[scriptcommand_exit|exit]]''. | ||
+ | |||
+ | Note that WinSCP also supports ''bye'' alias. | ||
==== binary ==== | ==== binary ==== | ||
Line 106: | Line 118: | ||
==== cd ==== | ==== cd ==== | ||
- | Use same syntax in WinSCP script. | + | Use ''[[scriptcommand_cd|cd]]'' command. |
==== close ==== | ==== close ==== | ||
- | Use same syntax in WinSCP script. | + | Use ''[[scriptcommand_close|close]]'' command. |
==== delete ==== | ==== delete ==== | ||
- | Use ''[[scriptcommand_rm|rm]]'' command (note that ''delete'' alias is also supported). | + | Use ''[[scriptcommand_rm|rm]]'' command. |
+ | |||
+ | Note that WinSCP also supports ''delete'' alias. | ||
==== dir ==== | ==== dir ==== | ||
Line 118: | Line 132: | ||
WinSCP does not support storing listing to a local file (second parameter of ''dir'' command). | WinSCP does not support storing listing to a local file (second parameter of ''dir'' command). | ||
+ | |||
+ | Note that WinSCP also supports ''dir'' alias. | ||
==== disconnect ==== | ==== disconnect ==== | ||
- | Use ''[[scriptcommand_close|close]]'' command. | + | Alias to ''[[scriptcommand_close|close]]'' command. |
==== get ==== | ==== get ==== | ||
- | Use same syntax in WinSCP script. | + | 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. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code> | ||
+ | ascii | ||
+ | get index.html | ||
+ | </code> | ||
+ | |||
+ | converts to: | ||
+ | |||
+ | <code winscp> | ||
+ | get -transfer=ascii index.html | ||
+ | </code> | ||
==== glob ==== | ==== glob ==== | ||
- | In WinSCP, to escape special characters in file mask, use [[file_mask#basic_syntax|set pattern]]. | + | In WinSCP, to escape special characters in filename, use [[file_mask#basic_syntax|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 ==== | ||
Line 134: | Line 165: | ||
==== lcd ==== | ==== lcd ==== | ||
- | Use same syntax in WinSCP script. | + | Use ''[[scriptcommand_lcd|lcd]]'' command. |
==== literal ==== | ==== literal ==== | ||
- | Where are no literal 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. | + | Where 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. | ||
==== ls ==== | ==== ls ==== | ||
Line 157: | Line 190: | ||
<code> | <code> | ||
- | binary | + | ascii |
mget index.html about.html | mget index.html about.html | ||
</code> | </code> | ||
Line 164: | Line 197: | ||
<code winscp> | <code winscp> | ||
- | get -transfer=binary index.html about.html .\ | + | get -transfer=ascii index.html about.html .\ |
</code> | </code> | ||
+ | |||
+ | Note that WinSCP also supports ''mget'' as an alias to ''get'' command. | ||
==== mkdir ==== | ==== mkdir ==== | ||
- | Use same syntax in WinSCP script. | + | Use ''[[scriptcommand_mkdir|mkdir]]'' command. |
==== mput ==== | ==== mput ==== | ||
Line 175: | Line 210: | ||
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 ''[[guide_ftp_script_to_sftp#binary|binary]]'' or ''[[guide_ftp_script_to_sftp#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 download 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''). |
- | Note that ''get'' command never prompts before individual file transfer. | + | Note that ''put'' command never prompts before individual file transfer. |
Example: | Example: | ||
<code> | <code> | ||
- | binary | + | ascii |
mput index.html about.html | mput index.html about.html | ||
</code> | </code> | ||
Line 189: | Line 224: | ||
<code winscp> | <code winscp> | ||
- | put -transfer=binary index.html about.html ./ | + | put -transfer=ascii index.html about.html ./ |
</code> | </code> | ||
+ | Note that WinSCP also supports ''mput'' as an alias to ''put'' command. | ||
+ | ==== open ==== | ||
+ | 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. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code> | ||
+ | open ftp.example.com | ||
+ | username | ||
+ | password | ||
+ | </code> | ||
+ | |||
+ | or equivalent (when ''ftp.exe -n'' command-line parameter is used): | ||
+ | |||
+ | <code> | ||
+ | open ftp.example.com | ||
+ | user username | ||
+ | password | ||
+ | </code> | ||
+ | |||
+ | convert to: | ||
+ | |||
+ | <code winscp> | ||
+ | open sftp://username:password@sftp.example.com | ||
+ | </code> | ||
+ | |||
+ | ==== prompt ==== | ||
+ | |||
+ | WinSCP does not prompt for individual transfers during multi-file transfers, so there's no conversion needed. | ||
+ | |||
+ | ==== put ==== | ||
+ | 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. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code> | ||
+ | ascii | ||
+ | put index.html | ||
+ | </code> | ||
+ | |||
+ | converts to: | ||
+ | |||
+ | <code winscp> | ||
+ | put -transfer=ascii index.html | ||
+ | </code> | ||
+ | |||
+ | ==== pwd ==== | ||
+ | Use ''[[scriptcommand_pwd|pwd]]'' command. | ||
+ | |||
+ | ==== quit ==== | ||
+ | Alias to ''[[guide_ftp_script_to_sftp#bye|bye]]'' command. | ||
+ | |||
+ | ==== quote ==== | ||
+ | Alias to ''[[guide_ftp_script_to_sftp#literal|literal]]'' command. | ||
+ | |||
+ | ==== recv ==== | ||
+ | Alias to ''[[guide_ftp_script_to_sftp#get|get]]'' command. | ||
+ | |||
+ | Note that WinSCP also supports ''recv'' alias. | ||
+ | |||
+ | ==== rename ==== | ||
+ | Use ''[[scriptcommand_mv|mv]]'' command. | ||
+ | |||
+ | Note that WinSCP also supports ''rename'' alias. | ||
+ | |||
+ | ==== rmdir ==== | ||
+ | Use ''[[scriptcommand_rmdir|rmdir]]'' command. | ||
+ | |||
+ | ==== send ==== | ||
+ | Alias to ''[[guide_ftp_script_to_sftp#put|put]]'' command. | ||
+ | |||
+ | Note that WinSCP also supports ''send'' alias. | ||
+ | |||
+ | ==== type ==== | ||
+ | Command ''type ascii'' is an alias to ''[[guide_ftp_script_to_sftp#ascii|ascii]]'' command. | ||
+ | |||
+ | Command ''type binary'' is an alias to ''[[guide_ftp_script_to_sftp#binary|binary]]'' command. | ||
+ | |||
+ | ==== user ==== | ||
+ | In WinSCP, username is specified as part of [[session_url|session URL]] parameter of WinSCP ''open'' command. | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code> | ||
+ | open ftp.example.com | ||
+ | user username | ||
+ | password | ||
+ | </code> | ||
+ | |||
+ | converts to: | ||
+ | |||
+ | <code winscp> | ||
+ | open sftp://username:password@sftp.example.com | ||
+ | </code> | ||
+ | |||
+ | ==== verbose ==== | ||
+ | There is no direct equivalent. | ||
+ | |||
+ | You can instead enable [[logging|session logging]] using ''/log=<logfile>'' [[commandline|command-line]] parameter: | ||
+ | |||
+ | <code> | ||
+ | winscp.com /script=script.txt /ini=nul /log=session.log | ||
+ | </code> | ||
==== ! (exit to local shell) ==== | ==== ! (exit to local shell) ==== | ||
Line 198: | Line 340: | ||
For more advanced tasks, consider using [[library_powershell|WinSCP .NET assembly from PowerShell script]]. | For more advanced tasks, consider using [[library_powershell|WinSCP .NET assembly from PowerShell script]]. | ||
+ | |||
+ | ===== Example ===== | ||
+ | Command ''ftp.exe -s:script.txt'' with ''script.txt'': | ||
+ | <code> | ||
+ | prompt | ||
+ | open ftp.example.com | ||
+ | username | ||
+ | password | ||
+ | cd /home/user | ||
+ | binary | ||
+ | mget *.zip | ||
+ | close | ||
+ | quit | ||
+ | </code> | ||
+ | |||
+ | converts to command ''winscp.com /script=script.txt /ini=nul'' with ''script.txt'': | ||
+ | |||
+ | <code winscp> | ||
+ | option batch abort | ||
+ | option confirm off | ||
+ | open sftp://username:password@sftp.example.com | ||
+ | cd /home/user | ||
+ | get -transfer=binary *.zip | ||
+ | close | ||
+ | exit | ||
+ | </code> | ||