Tcl
Tcl
SFTP Get File Date/Times in Different Formats
See more SFTP Examples
Demonstrates how to get remote file date/times in different formats.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
set sftp [new_CkSFtp]
# Connect to the SSH server.
set hostname "my-sftp-server.com"
set port 22
set success [CkSFtp_Connect $sftp $hostname $port]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
# Authenticate with the SSH server.
set success [CkSFtp_AuthenticatePw $sftp "myLogin" "myPassword"]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
# After authenticating, the SFTP subsystem must be initialized:
set success [CkSFtp_InitializeSftp $sftp]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
# Open a directory on the server...
# This example opens the "junk" directory located under the HOME directory of the SSH user account.
set handle [CkSFtp_openDir $sftp "junk"]
if {[CkSFtp_get_LastMethodSuccess $sftp] == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
exit
}
# Download the directory listing:
set dirListing [new_CkSFtpDir]
set success [CkSFtp_ReadDirListing $sftp $handle $dirListing]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
delete_CkSFtpDir $dirListing
exit
}
# Close the directory handle
set success [CkSFtp_CloseHandle $sftp $handle]
if {$success == 0} then {
puts [CkSFtp_lastErrorText $sftp]
delete_CkSFtp $sftp
delete_CkSFtpDir $dirListing
exit
}
# 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")
set fileObj [new_CkSFtpFile]
set sbFilename [new_CkStringBuilder]
set caseSensitive 0
set bLocalDateTime 0
set dt [new_CkDateTime]
set i 0
set n [CkSFtpDir_get_NumFilesAndDirs $dirListing]
while {$i < $n} {
set success [CkSFtpDir_FileAt $dirListing $i $fileObj]
if {$success == 0} then {
puts [CkSFtpDir_lastErrorText $dirListing]
delete_CkSFtp $sftp
delete_CkSFtpDir $dirListing
delete_CkSFtpFile $fileObj
delete_CkStringBuilder $sbFilename
delete_CkDateTime $dt
exit
}
puts [CkSFtpFile_filename $fileObj]
# Get the last-modified date/time
CkDateTime_SetFromRfc822 $dt [CkSFtpFile_lastModifiedTimeStr $fileObj]
# Get the date/time in other formats offered by the CkDateTime object.
# such as Wed, 18 Oct 2017 09:08:21 GMT
puts "RFC822 format: [CkDateTime_getAsRfc822 $dt $bLocalDateTime]"
# such as 1990-12-31T23:59:60Z
puts "Timestamp: [CkDateTime_getAsTimestamp $dt $bLocalDateTime]"
# Such as: "02/16/2008 12:15:12" where hour is 0 to 23.
puts "RFC822 format: [CkDateTime_getAsIso8601 $dt MM/DD/YYYY hh:mm:ss $bLocalDateTime]"
set i [expr $i + 1]
}
puts "Success."
delete_CkSFtp $sftp
delete_CkSFtpDir $dirListing
delete_CkSFtpFile $fileObj
delete_CkStringBuilder $sbFilename
delete_CkDateTime $dt