Archived Forum Post

Index of archived forum posts

Question:

SFTP Directory Listing Order

Feb 12 '16 at 10:00

Is there a way with the opendir/read listing to return the data from oldest to newest? I am using the perl chilkat library.


Answer

You should be able to send a raw command to do that. I think there is a SendCommand method and the Quote would also do that. ls

But it looks like the FTP protocol does not support options. SFTP docs allow options on the ls command.

http://www.computerhope.com/unix/sftp.htm


Answer

SFTP does not have quote or send command method.

This is how I do it. Get your list and then return a associative array which can be sorted. The problem is you have to have similar routines because servers on the other end do not behave the same way. Especially if you are working with a mainframe on the other end. Do not remember but that associate array is probably already in order.

ie $rlist{"filename.txt"} = "20160211020250"

   $dirListing = $ftp->ReadDir($handle);

$num = $dirListing->get_NumFilesAndDirs(); if ($num < 0) { $message="listDirectory:Error [$!]n" . $ftp->lastErrorText(); $cm->Comment($message,1); return ($message); } if ($num > 0) { for ($rc=0; $rc <= $num - 1; $rc++) { $fileObj = $dirListing->GetFileObject($rc); $file = $fileObj->filename();

       if (!$fileObj->get_IsDirectory())
        {
          $file =~ s!\/!!;
          $tm = $fileObj->get_LastModifiedTime($timed);
          $ftime = sprintf "%04d%02d%02d%02d%02d%02d",$timed->{wYear},$timed->{wMonth},$timed->{wDay},$timed->{wHour},$timed->{wMinute},$timed->{wSecond};
          $rlist{$file} = $ftime;
       }
   }

} $cm->Comment("readDirectory:OK [$dir]",1); return %rlist;