How to determine if an item is a file or directory using Net :: SFTP?

How to determine if a directory item is a file or directory using Net::SFTPor ruby ​​code?

+3
source share
3 answers

The method do_statseems like it can get you this information. See also the documentation for Net::SFTP::Attributesand perldoc -f stat.

+6
source

To illustrate the use of the Manni recommendation :

use Fcntl(:mode);

my $permissions  = $sftp->do_stat($path)->perm();
my $is_directory = S_ISDIR($permissions);
+5
source

At least two ways to do this in SFTP and Ruby:

require 'net/sftp'
Net::SFTP.start('HOSTNAME', 'USER', :password => 'PASSWORD') do |sftp|

  file = File.expand_path(__FILE__)
  dir  = File.dirname(file)

  sftp.lstat!(file).directory?
  sftp.lstat!(dir).file?

  sftp.file.open(dir, "r") do |f|
    f.stat.file?
    f.stat.directory? # true
  end

end
0
source

Source: https://habr.com/ru/post/1724004/


All Articles