Differences

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

script_checking_file_existence 2016-07-29 script_checking_file_existence 2022-10-21 (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 can use the ''[[library_session_enumerateremotefiles|Session.EnumerateRemoteFiles]]''.+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#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#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 45: Line 45:
exit /b 1 exit /b 1
</code> </code>
 +
 +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: 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:
Line 59: Line 61:
===== [[local]] Local file existence ===== ===== [[local]] Local file existence =====
-  * In [[library_powershell|PowerShell]], use ''[[https://technet.microsoft.com/en-us/library/hh849776.aspx|Test-Path]]'' cmdlet. See an example in [[library_example_check_existence_timestamp#powershell|Checking file existence and timestamp]]. +  * In [[library_powershell|PowerShell]], use ''[[ps&gt;microsoft.powershell.management/test-path|Test-Path]]'' cmdlet. See an example in [[library_example_check_existence_timestamp#powershell|*]]. 
-  * In a batch file, use ''[[https://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 ''[[https://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx|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|Checking file existence and timestamp]]. +  * In .NET, use ''[[dotnet&gt;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 ''[[https://msdn.microsoft.com/en-us/library/x23stk5t.aspx|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|Checking file existence and timestamp]].+  * 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|*]].

Last modified: by martin