Differences
This shows you the differences between the selected revisions of the page.
guide_automation 2009-03-30 | guide_automation 2023-10-27 (current) | ||
Line 1: | Line 1: | ||
- | ====== Automate file transfers (or synchronization) to FTP/SFTP server ====== | + | ====== Automate file transfers (or synchronization) to FTP server or SFTP server ====== |
- | //This guide contains simplified description of automating operations on FTP/SFTP server with WinSCP.// | + | |
+ | ~~AD~~ | ||
+ | |||
+ | //This guide contains a simplified description of automating operations on FTP/SFTP server with WinSCP. You may want to see [[scripting|detailed documentation]] of the scripting functionality instead//. | ||
WinSCP offers [[scripting]] interface that you can use to automate many operations that it supports, including file transfers, synchronization and other operations. | WinSCP offers [[scripting]] interface that you can use to automate many operations that it supports, including file transfers, synchronization and other operations. | ||
+ | |||
+ | //There is also [[library|WinSCP .NET assembly]] built on top of the scripting interface. If you plan to call WinSCP from your .NET code or [[library_powershell|PowerShell]], or if your task requires conditional processing, loops or other control structures, you should better use the .NET assembly. This guide focuses on simple automation tasks using scripting interface only.// | ||
+ | |||
+ | ===== Before Starting ===== | ||
Before starting you should: | Before starting you should: | ||
Line 8: | Line 15: | ||
* [[guide_connect|Know how to connect to your FTP/SFTP account]]. | * [[guide_connect|Know how to connect to your FTP/SFTP account]]. | ||
- | ===== Commands ===== | + | ===== [[commands]] Commands ===== |
- | To automate operation, you need to find out [[script_commands|commands]] necessary to implement it. For simple operations you need at least to: | + | To automate operation, you need to find out [[scripting#commands|commands]] necessary to implement it. For simple operations you need at least to: |
- | * Set script to be performed non-interactively. For most automation scripts, you should use commands ''[[script_commands#option|option batch on]]'' and ''[[script_commands#option|option confirm off]]''. | + | * Open session using ''[[scriptcommand_open|open]]'' command. |
- | * Open session. You can use ''[[script_commands#open|open]]'' command (alternatively specify session on [[commandline|command line]]). | + | * Perform operation. For [[task_upload|uploads]] use ''[[scriptcommand_put|put]]'' command. For [[task_download|downloads]] use ''[[scriptcommand_get|get]]'' command. For [[task_synchronize_full|synchronization]] use ''[[scriptcommand_synchronize|synchronize]]'' command. For other operations, see [[scripting#commands|supported commands]]. |
- | * Perform operation. For [[task_upload|uploads]] use ''[[script_commands#put|put]]'' command. For [[task_download|downloads]] use ''[[script_commands#get|get]]'' command. For [[task_synchronize_full|synchronization]] use ''[[script_commands#synchronize|synchronize]]'' command. For other operations, see [[script_commands|supported commands]]. | + | * Exit scripting using ''[[scriptcommand_exit|exit]]'' command. |
- | * Exit scripting using ''[[script_commands#exit|exit]]'' command. | + | |
- | ===== Script file ===== | + | For example a typical script to upload a file is: |
- | Assemble the commands into script file. You can name the script file as you like. See [[scripting#example|simple example]] and some [[scripts|useful scripts]]. | + | |
- | Use ''/script'' [[commandline|command line]] option to pass the script to WinSCP. You can embed whole command line into Windows batch file (''.bat''), such as: | + | <code winscp> |
- | <code> | + | # Connect to SFTP server using a password |
+ | open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx..." | ||
+ | # Upload file | ||
+ | put d:\examplefile.txt /home/user/ | ||
+ | # Exit WinSCP | ||
+ | exit | ||
+ | </code> | ||
+ | |||
+ | ===== [[script_file]] Script file ===== | ||
+ | Assemble the commands into a script file. You can name the script file as you like. See [[scripting#example|simple example]] and some [[scripts|useful scripts]]. | ||
+ | |||
+ | Use the ''/script'' [[commandline#scripting|command line]] option to pass the script to the WinSCP [[executables|executable]]. Generally, you should also use [[commandline#configuration|''/ini=nul'' switch]] to [[scripting#configuration|isolate the script execution from GUI configuration]] and [[commandline#logging|''/log='' switch]] to enable [[logging|session logging]]. You can embed the complete command line into a Windows batch file (''.bat''), like as follows: | ||
+ | <code batch> | ||
@echo off | @echo off | ||
- | winscp /console /script=myscript.txt | + | winscp.com /ini=nul /log=myscript.log /script=myscript.txt |
</code> | </code> | ||
- | ===== Using script ===== | + | ~~AD~~ |
+ | |||
+ | ===== [[generating]] Generating script ===== | ||
+ | |||
+ | You can have WinSCP [[ui_generateurl|generate a script template for you]] or even a complete batch file. | ||
+ | |||
+ | To generate a script for a file transfer: | ||
+ | |||
+ | * [[guide_connect|Connect in the GUI]]. | ||
+ | * [[ui_file_panel#selecting_files|Select the files]] you want to transfer. | ||
+ | * Use one of the file transfer commands: //Upload//, //Download//, //Upload and Delete//, //Download and Delete//. | ||
+ | * On the [[ui_copy|transfer confirmation dialog]], setup transfer options (if you need any non-default settings). | ||
+ | * Use the //[[ui_copy#generating_code|Transfer Settings > Generate Code]]// command. | ||
+ | * The [[ui_generateurl#script|Generate transfer code]] dialog will appear with the generated script or code template. | ||
+ | |||
+ | ===== [[using]] Using script ===== | ||
Now to make using script easier/automatic you can: | Now to make using script easier/automatic you can: | ||
- | * Make shortcut to it on desktop. Either make shortcut to batch file (''.bat'') or enter full command line to shortcut itself. | + | * Make shortcut to it on desktop to ease execution. Either make shortcut to batch file (''.bat'') or enter full command line to shortcut itself.((Note that it is not possible to use ''winscp.com'' (''.com'' files in general) directly from a shortcut. Call ''winscp.com'' from a batch file or use ''winscp.exe'' with ''[[commandline#scripting|/console]]'' command-line parameter.)) |
- | * [[faq_schedule|Schedule automatic execution]]. | + | * If the wrapping batch file takes filename as command line parameter (see [[#parametrized|below]]) you can: |
+ | * Make shortcut to it on desktop and use it by dropping files on the icon. Windows automatically run the batch file and passes path to dropped file as command-line parameter. | ||
+ | * In a similar way you can put the shortcut to the batch file into Explorer’s ‘Send To’ context menu (''C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo'' in Windows Vista and newer). &winpath &winvista | ||
+ | * [[guide_schedule|Schedule automatic execution]]. | ||
===== Notes ===== | ===== Notes ===== | ||
- | When connecting to SSH host, you will need to [[scripting#hostkey|accept its host key]]. | + | When connecting to the SSH host, you will need to [[scripting#hostkey|accept its host key]]. |
- | When connecting to FTPS host with [[ftps#certificate|certificate]] signed by untrusted authority you will need to verify the certificate. | + | When connecting to FTPS or WebDAVS host with [[tls#certificate|certificate]] signed by an untrusted authority you will need to verify the certificate. |
- | ===== Modifying the script automatically ===== | + | ===== [[parametrized]] Modifying the script automatically ===== |
You may want to modify the script automatically. For example you may want to operate it with different file each time. | You may want to modify the script automatically. For example you may want to operate it with different file each time. | ||
- | For simple modifications, you can use environment variables from the script: | + | //For tasks involving more complex modifications, conditional processing, loops or other control structures, you should better use the [[library|WinSCP .NET assembly]].// |
- | <code> | + | For simple modifications, you can pass the variable parts of the script from command line: |
- | option batch on | + | |
- | option confirm off | + | <code winscp> |
- | open session | + | open mysession |
- | put %FILE_TO_UPLOAD% | + | put %1% |
exit | exit | ||
</code> | </code> | ||
- | Alternatively, you can generate new script file each time. To automate that, make a wrapper script file. For simple tasks you can use built-in Windows scripting functionality from batch file (''.bat''). For complex tasks, you will need to use some scripting language, such as PHP or Perl. | + | Execute the above script using syntax: |
+ | |||
+ | <code batch> | ||
+ | winscp.com /ini=nul /log=script.log /script=script.tmp /parameter // c:\myfile.txt | ||
+ | </code> | ||
+ | |||
+ | You can also use environment variables in the script. | ||
+ | |||
+ | Alternatively, you can generate new script file each time. To automate that, make a wrapper script file. For simple tasks you can use built-in Windows scripting functionality from batch file (''.bat''). For complex tasks, you will need to use some scripting language, such JScript or VBScript from Windows script host or PHP or Perl. | ||
Following example shows batch file that takes filename on command line and generates WinSCP script file to upload that file to remote server: | Following example shows batch file that takes filename on command line and generates WinSCP script file to upload that file to remote server: | ||
- | <code> | + | <code batch> |
- | rem Generate temporary script to upload %1 | + | rem Generate a temporary script to upload %1 |
- | echo option batch on > script.tmp | + | ( |
- | echo option confirm off >> script.tmp | + | echo open mysession |
- | echo open session >> script.tmp | + | · echo put %1 |
- | echo put %1 >> script.tmp | + | · echo <nohilite>exit</nohilite> |
- | echo exit >> script.tmp | + | ) > script.tmp |
- | rem Execute script | + | rem Execute the script |
- | winscp /script=script.tmp | + | winscp.com /ini=nul /log=script.log /script=script.tmp |
- | rem Delete temporary script | + | rem Delete the temporary script |
del script.tmp | del script.tmp | ||
</code> | </code> | ||
Line 68: | Line 111: | ||
Now you can run the batch file like (supposing you have saved it to file ''upload.bat''): | Now you can run the batch file like (supposing you have saved it to file ''upload.bat''): | ||
- | <code> | + | <code batch> |
upload.bat c:\myfile.txt | upload.bat c:\myfile.txt | ||
</code> | </code> | ||
- | You can also put the script on your desktop and than easily use it by dropping files on its icon. Windows automatically run the script and passes path to dropped file as command-line parameter. | + | See more hints on [[#using|using parametrized batch file]]. |
+ | |||
+ | //See [[guide_automation_conditional#scripting|*]] for a more complex example; and [[guide_automation_advanced|*]] for examples of script generation using more powerful languages.// | ||
+ | |||
+ | ~~AD~~ | ||
===== [[results]] Checking script results ===== | ===== [[results]] Checking script results ===== | ||
To check results of the script you can: | To check results of the script you can: | ||
* Check exit code of WinSCP (exit code is the only relevant and reliable way to check if script completed successfully). See example below and [[faq_script_result|FAQ]]. | * Check exit code of WinSCP (exit code is the only relevant and reliable way to check if script completed successfully). See example below and [[faq_script_result|FAQ]]. | ||
- | * Save and inspect log file. [[logging_xml|XML log format]] is recommended. Use [[commandline|command-line]] parameter ''/log''. | + | * Save and inspect log file. [[logging_xml|XML log format]] is recommended. Use command-line parameter ''[[commandline#logging|/xmllog]]''. |
- | * Save and inspect output of the script. Use output redirection (''> filename'' at the end of commandline executing WinSCP). Note that you can redirect output of ''WinSCP.com'' only (see [[scripting#console|documentation]]). | + | * Save and inspect output of the script. Use [[executables|output redirection]]. |
- | Once you find out what was the result of the script, you can perform any action you like. E.g. after evaluating exit code of WinSCP, you can send a "success" or "error" email. For that use any command-line email client you like, e.g. [[http://glob.com.au/sendmail/|sendmail]]: | + | Once you find out what was the result of the script, you can perform any action you like: print a message, [[script_email|send an email]], etc. |
- | <code> | + | You should also make the batch file indicate a result in its exit code, particularly if it is called from some parent system (for example [[guide_ssis|SSIS]]). |
- | winscp.com /script=example.txt | + | |
- | if errorlevel 1 goto error | + | |
- | echo Success | + | See an example batch file: |
- | sendmail.exe -t < success_mail.txt | + | |
- | goto end | + | |
- | :error | + | <code batch> |
- | echo Error! | + | winscp.com /ini=nul /log=example.log /script=example.txt |
- | sendmail.exe -t < error_mail.txt | + | if %ERRORLEVEL% equ 0 ( |
- | + | ··echo Success | |
- | :end | + | exit /b 0 |
+ | ) else ( | ||
+ | echo Error! | ||
+ | exit /b 1 | ||
+ | ) | ||
</code> | </code> | ||
- | Where for example content of ''success_mail.txt'' may be: | + | A similar error handling is used in [[ui_generateurl|the batch file template]] that WinSCP can generate for you. |
- | <code> | + | |
- | From: script@example.com | + | |
- | To: me@example.com | + | |
- | Subject: Success | + | |
- | The files were uploaded successfully. | + | //If you require checking results of each command individually, you should better use the [[library|WinSCP .NET assembly]]. Alternatively, see the guide [[guide_automation_advanced|*]] for examples of checking script results (including XML log parsing) using more powerful languages and the guide to [[guide_interpreting_xml_log|*]] using C# language.// |
- | </code> | + | |
+ | ===== Example ===== | ||
+ | See [[scripting#example|example]] in scripting documentation. | ||
- | ===== Further Reading ===== | + | ===== Further reading ===== |
- | * [[reporting|Troubleshooting]]; | + | * [[troubleshooting|*]]; |
* [[scripting|Scripting]] documentation; | * [[scripting|Scripting]] documentation; | ||
- | * [[faq#scripting_automation|FAQ about scripting]]. | + | * Guide to [[guide_automation_advanced|*]]; |
+ | * [[library|*]]; | ||
+ | * [[commandline|Command-line]] parameters; | ||
+ | * WinSCP [[executables|executables]]; | ||
+ | ··* [[faq#scripting|FAQ about scripting]]; | ||
+ | * Example [[scripts|scripts]]; | ||
+ | * [[guide_schedule|*]]. |