Differences

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

guide_automation 2020-11-24 guide_automation 2023-10-27 (current)
Line 25: Line 25:
<code winscp> <code winscp>
# Connect to SFTP server using a password # Connect to SFTP server using a password
-open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx...="+open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx..."
# Upload file # Upload file
put d:\examplefile.txt /home/user/ put d:\examplefile.txt /home/user/
Line 35: Line 35:
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]]. 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]]. You can embed the complete command line into a Windows batch file (''.bat''), like as follows:+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> <code batch>
@echo off @echo off
-winscp.com /ini=nul /script=myscript.txt+winscp.com /ini=nul /log=myscript.log /script=myscript.txt
</code> </code>
Line 52: Line 52:
  * [[ui_file_panel#selecting_files|Select the files]] you want to transfer.   * [[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//.   * 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).+  * 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.   * 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.   * The [[ui_generateurl#script|Generate transfer code]] dialog will appear with the generated script or code template.
Line 65: Line 65:
===== 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 or WebDAVS host with [[tls#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.
===== [[parametrized]] Modifying the script automatically ===== ===== [[parametrized]] Modifying the script automatically =====
Line 85: Line 85:
<code batch> <code batch>
-winscp.com /ini=nul /script=script.tmp /parameter // c:\myfile.txt+winscp.com /ini=nul /log=script.log /script=script.tmp /parameter // c:\myfile.txt
</code> </code>
Line 95: Line 95:
<code batch> <code batch>
-rem Generate temporary script to upload %1 +rem Generate a temporary script to upload %1 
-echo open mysession >&gt; script.tmp +
-echo put %1 &gt;> script.tmp +··echo open mysession 
-echo exit >> script.tmp+· echo put %1 
 +  echo &lt;nohilite>exit&lt;/nohilite> 
 +) > script.tmp
-rem Execute script +rem Execute the script 
-winscp.com /ini=nul /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 115: Line 117:
See more hints on [[#using|using parametrized batch file]]. See more hints on [[#using|using parametrized batch file]].
-//See [[guide_automation_advanced|guide to advanced scripting]] for examples of script generation using more powerful languages.//+//See [[guide_automation_conditional#scripting|*]] for a more complex example; and [[guide_automation_advanced|*]] for examples of script generation using more powerful languages.//
~~AD~~ ~~AD~~
-winscp.exe /console /command &quot;option batch continue&quot; &quot;option confirm off&quot; &quot;open sftp://apadmin:abc123@@172.22.141.242:22&quot; &quot;option transfer binary&quot; &quot;put pcmp.war /home/apadmin/&quot; +===== [[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 command-line parameter ''[[commandline#logging|/xmllog]]''. 
 +  * 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: print a message, [[script_email|send an email]], etc. 
 + 
 +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]]). 
 + 
 +See an example batch file: 
 + 
 +&lt;code batch&gt; 
 +winscp.com /ini=nul /log=example.log /script=example.txt 
 +if %ERRORLEVEL% equ 0 ( 
 +  echo Success 
 +  exit /b 0 
 +) else ( 
 +  echo Error! 
 +  exit /b 1 
 +
 +&lt;/code> 
 + 
 +A similar error handling is used in [[ui_generateurl|the batch file template]] that WinSCP can generate for you. 
 + 
 +//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.// 
===== Example ===== ===== Example =====
See [[scripting#example|example]] in scripting documentation. See [[scripting#example|example]] in scripting documentation.
===== Further reading ===== ===== Further reading =====
-  * [[troubleshooting|Troubleshooting]];+  * [[troubleshooting|*]];
  * [[scripting|Scripting]] documentation;   * [[scripting|Scripting]] documentation;
-  * Guide to [[guide_automation_advanced|advanced scripting]]; +  * Guide to [[guide_automation_advanced|*]]; 
-  * [[library|WinSCP .NET assembly]];+  * [[library|*]];
  * [[commandline|Command-line]] parameters;   * [[commandline|Command-line]] parameters;
  * WinSCP [[executables|executables]];   * WinSCP [[executables|executables]];
  * [[faq#scripting|FAQ about scripting]];   * [[faq#scripting|FAQ about scripting]];
  * Example [[scripts|scripts]];   * Example [[scripts|scripts]];
-  * [[guide_schedule|Schedule file transfers or synchronization]].+  * [[guide_schedule|*]].

Last modified: by 223.137.213.164