Differences
This shows you the differences between the selected revisions of the page.
| 2014-11-06 | 2014-11-06 | ||
| we can now simply test file existence in scripting using stat command (martin) | Test remote files existence using wildcard (martin) | ||
| Line 404: | Line 404: | ||
| ~~AD~~ | ~~AD~~ | ||
| + | |||
| + | === [[mask]] Test remote files existence using wildcard === | ||
| + | |||
| + | If you are not looking for a specific file, but for an existence of any file matching a mask (e.g. ''*.txt''), you need to use ''[[library_session_listdirectory|Session.ListDirectory]]'' and query returned list of files. | ||
| + | |||
| + | <code powershell> | ||
| + | $remoteDir = "/home/user" | ||
| + | $wildcard = "*.txt" | ||
| + | |||
| + | $directoryInfo = $session.ListDirectory($remoteDir); | ||
| + | |||
| + | $matchingFiles = | ||
| + | $directoryInfo.Files | | ||
| + | Where-Object { $_.Name -Like $wildcard } | ||
| + | |||
| + | if ($matchingFiles) | ||
| + | { | ||
| + | Write-Host "Found $matchingFiles" | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Write-Host "No matching files" | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | The ''-Like'' operator supports ''*'', ''?'' and ''[]'' patterns in the [[http://technet.microsoft.com/en-us/library/hh847812.aspx|wildcard]]. You can also use ''-Match'' operator with a regular expression. Learn about [[http://technet.microsoft.com/en-us/library/hh847759.aspx|PowerShell comparison operators]] | ||
| === [[scripting]] Using WinSCP Scripting === | === [[scripting]] Using WinSCP Scripting === | ||