DataFlex
DataFlex
Check if a Remote File or Directory Exists
See more SFTP Examples
Demonstrates the Chilkat SFtp.FileExists method, which checks a remote path and returns an integer describing what is there. The first argument is the remote path and the second (followLinks) selects whether a symbolic link is followed to its target. The return value is -1 on error, 0 if nothing exists, 1 for a file, 2 for a directory, 3 for a symbolic link, and higher values for special entry types.
Background: This returns more than a yes/no because "does it exist" usually comes with "and is it the kind of thing I expect" — a job that means to write a file should notice if a directory already occupies the path. The
followLinks flag decides whether a symbolic link is reported as a link (3, only when not following) or transparently resolved to its target's type. Always handle the -1 case: it means the check itself failed, which is different from the path not existing.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
Boolean iFollowLinks
Integer iResult
String sTemp1
Move False To iSuccess
// Demonstrates the SFtp.FileExists method, which checks a remote path and returns an integer
// describing what is there. The 1st argument is the remote path and the 2nd (followLinks)
// selects whether a symbolic link is followed to its target.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Connect, authenticate, and initialize the SFTP subsystem.
Move 22 To iPort
Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// When followLinks is True, a symbolic link is followed and the target's type is returned.
Move True To iFollowLinks
Get ComFileExists Of hoSftp "subdir/report.pdf" iFollowLinks To iResult
// Interpret the return value.
If (iResult = -1) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
If (iResult = 0) Begin
Showln "The path does not exist."
End
If (iResult = 1) Begin
Showln "A regular file exists."
End
If (iResult = 2) Begin
Showln "A directory exists."
End
// Other possible values: 3 = symbolic link (only when followLinks is False), 4 = special
// entry, 5 = unknown, 6 = socket, 7 = character device, 8 = block device, 9 = FIFO.
Send ComDisconnect To hoSftp
End_Procedure