4

How to identify whether the item of a directory is a file or a directory using Net::SFTP or ruby code?

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Aditya
  • 425
  • 3
  • 8
  • 22

3 Answers3

6

The do_stat method seems like it could get you that information. See also the documentation for Net::SFTP::Attributes and perldoc -f stat.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
innaM
  • 47,505
  • 4
  • 67
  • 87
5

To illustrate the use of Manni's recommendation:

use Fcntl(:mode);

my $permissions  = $sftp->do_stat($path)->perm();
my $is_directory = S_ISDIR($permissions);
Community
  • 1
  • 1
Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
1

At least two ways to do it 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
dgo.a
  • 2,634
  • 23
  • 35