Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Sftp
string ls_Hostname
long li_Port
string ls_Handle
oleobject loo_DirListing
oleobject loo_FileObj
oleobject loo_SbFilename
long li_CaseSensitive
long li_BLocalDateTime
oleobject loo_Dt
long i
long n

li_Success = 0

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

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Connect to the SSH server.  
ls_Hostname = "my-sftp-server.com"
li_Port = 22
li_Success = loo_Sftp.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Authenticate with the SSH server. 
li_Success = loo_Sftp.AuthenticatePw("myLogin","myPassword")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  After authenticating, the SFTP subsystem must be initialized:
li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Open a directory on the server...
//  This example opens the "junk" directory located under the HOME directory of the SSH user account.
ls_Handle = loo_Sftp.OpenDir("junk")
if loo_Sftp.LastMethodSuccess = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Download the directory listing:

loo_DirListing = create oleobject
li_rc = loo_DirListing.ConnectToNewObject("Chilkat.SFtpDir")

li_Success = loo_Sftp.ReadDirListing(ls_Handle,loo_DirListing)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_DirListing
    return
end if

//  Close the directory handle
li_Success = loo_Sftp.CloseHandle(ls_Handle)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_DirListing
    return
end if

//  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")
loo_FileObj = create oleobject
li_rc = loo_FileObj.ConnectToNewObject("Chilkat.SFtpFile")

loo_SbFilename = create oleobject
li_rc = loo_SbFilename.ConnectToNewObject("Chilkat.StringBuilder")

li_CaseSensitive = 0
li_BLocalDateTime = 0

loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime")

i = 0
n = loo_DirListing.NumFilesAndDirs
do while i < n

    li_Success = loo_DirListing.FileAt(i,loo_FileObj)
    if li_Success = 0 then
        Write-Debug loo_DirListing.LastErrorText
        destroy loo_Sftp
        destroy loo_DirListing
        destroy loo_FileObj
        destroy loo_SbFilename
        destroy loo_Dt
        return
    end if

    Write-Debug loo_FileObj.Filename

    //  Get the last-modified date/time
    loo_Dt.SetFromRfc822(loo_FileObj.LastModifiedTimeStr)

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

    //  such as Wed, 18 Oct 2017 09:08:21 GMT
    Write-Debug "RFC822 format: " + loo_Dt.GetAsRfc822(li_BLocalDateTime)

    //  such as 1990-12-31T23:59:60Z
    Write-Debug "Timestamp: " + loo_Dt.GetAsTimestamp(li_BLocalDateTime)

    //  Such as: "02/16/2008 12:15:12"  where hour is 0 to 23.
    Write-Debug "RFC822 format: " + loo_Dt.GetAsIso8601("MM/DD/YYYY hh:mm:ss",li_BLocalDateTime)

    i = i + 1
loop

Write-Debug "Success."


destroy loo_Sftp
destroy loo_DirListing
destroy loo_FileObj
destroy loo_SbFilename
destroy loo_Dt