Show page source of guide_automation #70419

= FTP/SFTPサーバーへの自動ファイル転送

このページでは、WinSCPを使ってFTP/SFTPサーバーに自動的にファイルを転送したり、ファイル/フォルダの同期を行う方法について説明します。

WinSCPはスクリプト機能を備えており、これを利用することでファイル転送やフォルダの同期といったさまざまな処理を自動的に実行できます。

== コマンド
WinSCPのスクリプト機能では、実行したい処理を[script_commands コマンド]で記述することで自動的に実行することができます。基本的なスクリプトの流れは次のようになります。

 * [script_commands#option option batch on]コマンドと[script_commands#option option confirm off]コマンドを実行し、進捗表示および各種確認表示を抑制します
 * [script_commands#open open]コマンドを実行してホストに接続します
 * 目的の処理を実行します。ファイルをアップロードするには[script_commands#put put]コマンドを使用します。ダウンロードは[script_commands#get get]コマンドで行えます。また、[script_commands#synchronize synchronize]コマンドで指定したディレクトリを同期させることもできます。そのほかコマンドの詳細は[script_commands コマンド一覧]を参照してください
 * [script_commands#exit exit]コマンドを実行して処理を終了します

{{{ comment

== スクリプトファイル

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 [[executables|executable]]. You can embed whole command line into Windows batch file (''.bat''), such as:
<code>
@echo off
winscp.com /script=myscript.txt
</code>

===== [[using]] Using script =====
Now to make using script easier/automatic you can:
  * Make shortcut to it on desktop to ease execution. Either make shortcut to batch file (''.bat'') or enter full command line to shortcut itself.
  * If the wrapping batch file takes filename as command line parameter (see [[guide_automation#modifying_the_script_automatically|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:\documents and settings\username\sendto'').
  * [[guide_schedule|Schedule automatic execution]].

===== Notes =====
When connecting to 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.

===== 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.

For simple modifications, you can pass the variable parts of the script from command line:

<code winscp>
option batch on
option confirm off
open mysession
put %1%
exit
</code>

Execute the above script using syntax:

<code>
winscp.com /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 JavaScript or VBScript from Windows scripting 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:

<code>
rem Generate temporary script to upload %1
echo option batch on > script.tmp
echo option confirm off >> script.tmp
echo open mysession >> script.tmp
echo put %1 >> script.tmp
echo exit >> script.tmp

rem Execute script
winscp.com /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>

See more hints on [[guide_automation#using|using parameterised batch file]].

//See [[guide_automation_advanced|guide to advanced scripting]] for examples of script generation using more powerful languages.//

===== [[results]] Checking script results =====
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]].
  * Save and inspect log file. [[logging_xml|XML log format]] is recommended. Use [[commandline|command-line]] parameter ''/log''.
  * 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]]:

<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>

//See [[guide_automation_advanced|guide to advanced scripting]] for examples of checking script results (including XML log parsing) using more powerful languages.//


===== Further reading =====
  * [[troubleshooting|Troubleshooting]];
  * [[scripting|Scripting]] documentation;
  * Guide to [[guide_automation_advanced|advanced scripting]];
  * [[commandline|Command-line]] parameters;
  * WinSCP [[executables|executables]];
  * [[faq#scripting_automation|FAQ about scripting]];
  * Example [[scripts|scripts]].

}}}