Differences
This shows you the differences between the selected revisions of the page.
guide_automation_conditional 2014-11-25 | guide_automation_conditional 2022-10-21 (current) | ||
Line 2: | Line 2: | ||
When [[guide_automation|automating an operation]] with WinSCP, you may want to perform it only when a certain condition is met. Typically you may want to test if a particular file exists before attempting to transfer it. | When [[guide_automation|automating an operation]] with WinSCP, you may want to perform it only when a certain condition is met. Typically you may want to test if a particular file exists before attempting to transfer it. | ||
- | ===== Using WinSCP .NET Assembly ===== | + | ===== [[library]] Using WinSCP .NET Assembly ===== |
Particularly for complex conditions or conditions involving remote files, using [[library|WinSCP .NET assembly]] is the easiest and the recommended approach. | Particularly for complex conditions or conditions involving remote files, using [[library|WinSCP .NET assembly]] is the easiest and the recommended approach. | ||
Line 14: | Line 14: | ||
if ($session.FileExists($remotePath)) | if ($session.FileExists($remotePath)) | ||
{ | { | ||
- | Write-Host ("File {0} exists, downloading" -f $remotePath) | + | Write-Host "File $remotePath exists, downloading" |
$session.GetFiles($remotePath, $localPath).Check() | $session.GetFiles($remotePath, $localPath).Check() | ||
} | } | ||
else | else | ||
{ | { | ||
- | Write-Host ("File {0} does not exist" -f $remotePath) | + | Write-Host "File $remotePath does not exist" |
} | } | ||
</code> | </code> | ||
Line 25: | Line 25: | ||
See also: | See also: | ||
- | * [[script_checking_file_existence|Checking file existence]]; | + | * [[script_checking_file_existence|*]]; |
- | * [[library_powershell|Using WinSCP .NET assembly from PowerShell]]; | + | * [[script_downloading_when_done_file_exists|*]]; |
+ | * [[library_powershell|*]]; | ||
* Example for ''[[library_session_fileexists#powershell|Session.FileExists]]'' (complete code for above example); | * Example for ''[[library_session_fileexists#powershell|Session.FileExists]]'' (complete code for above example); | ||
- | * Example for ''[[library_session_getfiles#powershell|Session.GetFiles]]'' (showing how to test a local file existence and how to compare local and remote file timetamps). | + | * Example [[library_example_check_existence_timestamp|*]] (showing how to test a local and a remote file existence and how to compare local and remote file timetamps). |
===== [[scripting]] Conditions in a Batch File ===== | ===== [[scripting]] Conditions in a Batch File ===== | ||
Line 34: | Line 35: | ||
If you do not want to use .NET assembly, for simple conditions or conditions involving local files, you may be able to move the condition outside of WinSCP [[scripting|script]] into a [[guide_automation_advanced#batch_file|wrapper batch file]]. | If you do not want to use .NET assembly, for simple conditions or conditions involving local files, you may be able to move the condition outside of WinSCP [[scripting|script]] into a [[guide_automation_advanced#batch_file|wrapper batch file]]. | ||
- | For example you can use an ''[[http://technet.microsoft.com/en-us/library/cc754335.aspx|if]]'' command to test a local file existence before uploading it: | + | For example you can use an [[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/if|''if'' command]] to test a local file existence before uploading it: |
<code batch> | <code batch> | ||
- | if exist c:\upload\test.txt winscp.com /script=upload.txt | + | if exist c:\upload\test.txt winscp.com /ini=nul /log=upload.log /script=upload.txt |
+ | </code> | ||
+ | |||
+ | If you need different conditions for individual script commands, you can generate the WinSCP script file on the fly step by step: | ||
+ | |||
+ | <code batch> | ||
+ | @echo off | ||
+ | set FILE1=c:\upload\test.txt | ||
+ | set FILE2=c:\other\file.txt | ||
+ | |||
+ | ( | ||
+ | echo open sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xxxxxxxxxxx..." | ||
+ | if exist %FILE1% echo put %FILE1% | ||
+ | if exist %FILE2% echo put %FILE2% | ||
+ | echo <nohilite>exit</nohilite> | ||
+ | ) > upload.txt | ||
+ | |||
+ | winscp.com /ini=nul /log=upload.log /script=upload.txt | ||
</code> | </code> | ||
See also: | See also: | ||
- | * [[script_checking_file_existence|Checking file existence]]; | + | * [[script_checking_file_existence|*]]; |
+ | * [[script_downloading_when_done_file_exists|*]]; | ||
* [[guide_automation#results|Checking script results]]; | * [[guide_automation#results|Checking script results]]; | ||
- | * [[guide_automation_advanced|Advanced FTP/SFTP scripting]]; | + | * [[guide_automation_advanced|*]]; |
- | * [[guide_interpreting_xml_log|Interpreting XML log for advanced scripting]]. | + | * [[guide_interpreting_xml_log|*]]. |