Differences

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

faq_automation 2008-03-21 faq_automation (current)
Line 1: Line 1:
-====== How do I automate file transfers? ====== 
-WinSCP offers [[scripting]] interface that you can use to automate many operation that it supports, including file transfers. 
-===== Commands ===== 
-To automate operation, you need to find out [[script_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. You can use ''[[script_commands#open|open]]'' command (alternatively specify session on [[commandline|command line]]). 
-  * Perform operation. For [[task_upload|uploads]] use ''[[script_commands#put|put]]'' command. For [[task_download|downloads]] use ''[[script_commands#get|get]]'' command. For other operations, see [[script_commands|supported commands]]. 
-  * Exit scripting using ''[[script_commands#exit|exit]]'' command. 
- 
-===== Script file ===== 
-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> 
-@echo off 
-winscp /console /script=myscript.txt 
-</code> 
- 
-===== Using script ===== 
-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. 
-  * [[faq_schedule|Schedule automatic execution]]. 
- 
-===== Notes ===== 
-When connecting to particular host for the first time, you will be prompted to [[ssh#verifying_the_host_key|verify host key]]. 
- 
-===== 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. To do that you need to 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. 
- 
-Following example shows batch file that takes filename on command line and generates WinSCP script file to upload that file to remote server: 
- 
-<code> 
-rem Generate temporary script to upload %1 
-echo option batch on > script.tmp 
-echo option confirm off >> script.tmp 
-echo open session >> script.tmp 
-echo put %1 >> script.tmp 
-echo exit >> script.tmp 
- 
-rem Execute script 
-winscp /script=script.tmp 
- 
-rem Delete temporary script 
-del script.tmp 
-</code> 
- 
-Now you can run the batch file like (supposing you have saved it to file ''upload.bat''): 
- 
-<code> 
-upload.bat c:\myfile.txt 
-</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. 
- 
-===== [[results]] Checking script results ===== 
-To check results of the script your can: 
-  * Check exit code of WinSCP. See example below and [[faq_script_result|another FAQ]]. 
-  * Save and inspect log file. Use [[commandline|command-line]] parameter ''/log''. 
-  * 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]]). 
- 
-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]]: 
- 
-<code> 
-winscp.com /script=example.txt 
-if errorlevel 1 goto error 
- 
-echo Success 
-sendmail.exe -t < success_mail.txt 
-goto end 
- 
-:error 
-echo Error! 
-sendmail.exe -t < error_mail.txt 
- 
-:end 
-</code> 
- 
-Where for example content of ''success_mail.txt'' may be: 
-<code> 
-From: script@example.com 
-To: me@example.com 
-Subject: Success 
- 
-The files were uploaded successfully. 
-</code>