Differences
This shows you the differences between the selected revisions of the page.
script_checking_file_existence 2015-12-17 | script_checking_file_existence 2024-09-02 (current) | ||
Line 3: | Line 3: | ||
===== [[remote]] Remote file existence ===== | ===== [[remote]] Remote file existence ===== | ||
- | ==== Using WinSCP .NET Assembly ==== | + | ==== [[library]] Using WinSCP .NET Assembly ==== |
Use method ''[[library_session_fileexists|Session.FileExists]]'' from [[library|WinSCP .NET assembly]]. | Use method ''[[library_session_fileexists|Session.FileExists]]'' from [[library|WinSCP .NET assembly]]. | ||
Line 13: | Line 13: | ||
if ($session.FileExists($remotePath)) | if ($session.FileExists($remotePath)) | ||
{ | { | ||
- | Write-Host ("File {0} exists" -f $remotePath) | + | Write-Host "File $remotePath exists" |
} | } | ||
</code> | </code> | ||
Line 19: | Line 19: | ||
See complete [[library_session_fileexists#powershell|PowerShell example for Session.FileExists]]. | See complete [[library_session_fileexists#powershell|PowerShell example for Session.FileExists]]. | ||
- | If you are not looking for a specific file, but for any file matching a mask (e.g. ''*.txt''), you need to use ''Session.ListDirectory'' and [[library_example_listing_files_matching_wildcard|query returned list of files]]. | + | If you are not looking for a specific file, but for any file matching a mask (e.g. ''*.txt''), you can use the ''[[library_session_enumerateremotefiles|Session.EnumerateRemoteFiles]]''. For a complex example, that uses this technique, see [[script_downloading_when_done_file_exists#separate|*]]. |
~~AD~~ | ~~AD~~ | ||
==== [[scripting]] Using WinSCP Scripting ==== | ==== [[scripting]] Using WinSCP Scripting ==== | ||
- | You can use a ''[[scriptcommand_stat|stat]]'' command in the ([[scripting#using_scripting|default]]) ''[[scriptcommand_option|option batch abort]]'' mode to query file attributes. If the file does not exist, the ''stat'' command fails and so does the script. Then, [[scripting#checking_results|test WinSCP exit code]] to determine, if the file exists or not. | + | You can use a ''[[scriptcommand_stat|stat]]'' command in the ([[scripting#using_scripting|default]]) ''[[scriptcommand_option#batch|option batch abort]]'' mode to query file attributes. If the file does not exist, the ''stat'' command fails and so does the script. Then, [[scripting#result|test WinSCP exit code]] to determine, if the file exists or not. |
<code batch> | <code batch> | ||
Line 39: | Line 39: | ||
echo File %REMOTE_PATH% exists | echo File %REMOTE_PATH% exists | ||
rem Do something | rem Do something | ||
- | exit 0 | + | exit /b 0 |
:error | :error | ||
- | echo Error or file %REMOTE_PATH% not exists | + | echo Error or file %REMOTE_PATH% <nohilite>not</nohilite> exists |
- | exit 1 | + | exit /b 1 |
</code> | </code> | ||
- | To check for existence of any file matching a mask, instead of a specific file, enable ''[[scriptcommand_option|option failonnomatch on]]'' mode and use ''[[scriptcommand_ls|ls mask]]'' command, instead of ''stat name'' command: | + | If the further processing involves WinSCP commands, you can add the commands directly to the main WinSCP script, just after the ''stat'' command. For an example, see [[script_downloading_when_done_file_exists#fixed_scripting|*]]. |
+ | |||
+ | To check for existence of any file matching a mask, instead of a specific file, enable ''[[scriptcommand_option#failonnomatch|option failonnomatch on]]'' mode and use ''[[scriptcommand_ls|ls mask]]'' command, instead of ''stat name'' command: | ||
<code batch> | <code batch> | ||
Line 59: | Line 61: | ||
===== [[local]] Local file existence ===== | ===== [[local]] Local file existence ===== | ||
- | * In [[library_powershell|PowerShell]], use ''[[http://technet.microsoft.com/en-us/library/hh849776.aspx|Test-Path]]'' cmdlet. See [[library_session_getfiles#powershell|example]]. | + | * In [[library_powershell|PowerShell]], use ''[[ps>microsoft.powershell.management/test-path|Test-Path]]'' cmdlet. See an example in [[library_example_check_existence_timestamp#powershell|*]]. |
- | * In a batch file, use ''[[http://technet.microsoft.com/en-us/library/cc754335.aspx|if exist]]'' command. See [[guide_automation_conditional#scripting|example]]. | + | * In a batch file, use ''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/if|if exist]]'' command. See [[guide_automation_conditional#scripting|example]]. |
- | * In .NET, use ''[[http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx|File.Exists]]'' method. See [[library_session_getfiles#csharp|C#]] and [[library_session_getfiles#vbnet|VB.NET]] example. | + | * In .NET, use ''[[dotnet>system.io.file.exists|File.Exists]]'' method. See [[library_example_check_existence_timestamp#csharp|C#]] and [[library_example_check_existence_timestamp#vbnet|VB.NET]] example in [[library_example_check_existence_timestamp|*]]. |
- | * In WSH, use ''[[http://msdn.microsoft.com/en-us/library/x23stk5t.aspx|Scripting.FileSystemObject.FileExists]]'' method. See [[library_session_getfiles#jscript|JScript]] and [[library_session_getfiles#jscript|VBScript]] example. | + | * In WSH, use ''[[https://learn.microsoft.com/en-us/previous-versions/x23stk5t(v=vs.85)|Scripting.FileSystemObject.FileExists]]'' method. See [[library_example_check_existence_timestamp#jscript|JScript]] and [[library_example_check_existence_timestamp#vbscript|VBScript]] example in [[library_example_check_existence_timestamp|*]]. |
===== Further Reading ===== | ===== Further Reading ===== | ||
Line 68: | Line 70: | ||
* Guide to [[guide_automation|scripting/automation]]; | * Guide to [[guide_automation|scripting/automation]]; | ||
* [[library|WinSCP .NET assembly]]; | * [[library|WinSCP .NET assembly]]; | ||
- | * [[library_powershell|Using WinSCP .NET Assembly from PowerShell]]. | + | * [[library_powershell|*]]. |