Sample code for 30+ languages & platforms
Xbase++

File Existence Check

See more FTP Examples

Testing to see if a file exists on the FTP server. The GetSizeByName method is a convenient way to check if a file exists. It will return -1 if the file does not exist, otherwise it returns the size of the file in bytes.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oFtp
LOCAL nFileSize

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

oFtp := CreateObject("Chilkat.Ftp2")

oFtp:Hostname := "ftp.example.com"
oFtp:Username := "username"
oFtp:Password := "password"

//  Connect and login to the FTP server.
nSuccess := oFtp:Connect()
IF (nSuccess != 1)
    ? oFtp:LastErrorText
    oFtp:destroy()
    RETURN
ENDIF

//  Set the current remote directory to where the file is located:
nSuccess := oFtp:ChangeRemoteDir("something")
IF (nSuccess != 1)
    ? oFtp:LastErrorText
    oFtp:destroy()
    RETURN
ENDIF

//  Test to see if the file exists by getting the file size by name. 
//  If a -1 is returned, the file does not exist.
nFileSize := oFtp:GetSizeByName("test123.txt")
IF (nFileSize < 0)
    ? "file does not exist"
ELSE
    ? "file exists and is " + Str(nFileSize) + " bytes in size"
ENDIF

nSuccess := oFtp:Disconnect()

oFtp:destroy()