Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

SFTP Get File Date/Times in Different Formats

See more SFTP Examples

Demonstrates how to get remote file date/times in different formats.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSftp
LOCAL cHostname
LOCAL nPort
LOCAL cHandle
LOCAL oDirListing
LOCAL oFileObj
LOCAL oSbFilename
LOCAL nCaseSensitive
LOCAL nBLocalDateTime
LOCAL oDt
LOCAL i
LOCAL n

nSuccess := 0

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

oSftp := CreateObject("Chilkat.SFtp")

//  Connect to the SSH server.  
cHostname := "my-sftp-server.com"
nPort := 22
nSuccess := oSftp:Connect(cHostname, nPort)
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Authenticate with the SSH server. 
nSuccess := oSftp:AuthenticatePw("myLogin", "myPassword")
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  After authenticating, the SFTP subsystem must be initialized:
nSuccess := oSftp:InitializeSftp()
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Open a directory on the server...
//  This example opens the "junk" directory located under the HOME directory of the SSH user account.
cHandle := oSftp:OpenDir("junk")
IF (oSftp:LastMethodSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    RETURN
ENDIF

//  Download the directory listing:

oDirListing := CreateObject("Chilkat.SFtpDir")
nSuccess := oSftp:ReadDirListing(cHandle, oDirListing)
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    oDirListing:destroy()
    RETURN
ENDIF

//  Close the directory handle
nSuccess := oSftp:CloseHandle(cHandle)
IF (nSuccess == 0)
    ? oSftp:LastErrorText
    oSftp:destroy()
    oDirListing:destroy()
    RETURN
ENDIF

//  Iterate over the files.
//  Examine each filename and indicate those that match *FICHERO*.pdf
//  (i.e. the filename contains the word "FICHERO" and ends in ".pdf")
oFileObj := CreateObject("Chilkat.SFtpFile")
oSbFilename := CreateObject("Chilkat.StringBuilder")
nCaseSensitive := 0
nBLocalDateTime := 0

oDt := CreateObject("Chilkat.CkDateTime")
i := 0
n := oDirListing:NumFilesAndDirs
DO WHILE i < n

    nSuccess := oDirListing:FileAt(i, oFileObj)
    IF (nSuccess == 0)
        ? oDirListing:LastErrorText
        oSftp:destroy()
        oDirListing:destroy()
        oFileObj:destroy()
        oSbFilename:destroy()
        oDt:destroy()
        RETURN
    ENDIF

    ? oFileObj:Filename

    //  Get the last-modified date/time
    oDt:SetFromRfc822(oFileObj:LastModifiedTimeStr)

    //  Get the date/time in other formats offered by the CkDateTime object.

    //  such as Wed, 18 Oct 2017 09:08:21 GMT
    ? "RFC822 format: " + oDt:GetAsRfc822(nBLocalDateTime)

    //  such as 1990-12-31T23:59:60Z
    ? "Timestamp: " + oDt:GetAsTimestamp(nBLocalDateTime)

    //  Such as: "02/16/2008 12:15:12"  where hour is 0 to 23.
    ? "RFC822 format: " + oDt:GetAsIso8601("MM/DD/YYYY hh:mm:ss", nBLocalDateTime)

    i := i + 1
ENDDO

? "Success."

oSftp:destroy()
oDirListing:destroy()
oFileObj:destroy()
oSbFilename:destroy()
oDt:destroy()