class Net::SFTP::Operations::Dir
end
puts entry.name
sftp.dir.glob(“/remote/path”, “*/.rb”) do |entry|
p sftp.dir.entries(“/remote/path”).map { |e| e.name }
end
puts entry.name
sftp.dir.foreach(“/remote/path”) do |entry|
::Dir class.
for searching and enumerating directory entries, similarly to the standard
A convenience class for working with remote directories. It provides methods
def [](path, pattern)
Identical to calling #glob with a +flags+ parameter of 0 and no block.
def [](path, pattern) glob(path, pattern, 0) end
def entries(path)
Returns an array of Name objects representing the items in the given
def entries(path) results = [] foreach(path) { |entry| results << entry } return results end
def foreach(path)
remote server. Yields a Name object to the block, rather than merely
Calls the block once for each entry in the named directory on the
def foreach(path) handle = sftp.opendir!(path) while entries = sftp.readdir!(handle) entries.each { |entry| yield entry } end return nil ensure sftp.close!(handle) if handle end
def glob(path, pattern, flags=0)
shallow directory hierarchies with relatively few directories, though
same level of alacrity that ::Dir.glob does; it will work best for
working purely locally, don't expect this method to perform with the
Because working over an SFTP connection is always going to be slower than
be returned in an array when the method finishes.
will be yielded to the block as they are found; otherwise, they will
entries under +path+ against +pattern+. If a block is given, matches
Works as ::Dir.glob, matching (possibly recursively) all directory
def glob(path, pattern, flags=0) flags |= ::File::FNM_PATHNAME path = path.chop if path.end_with?('/') && path != '/' results = [] unless block_given? queue = entries(path).reject { |e| %w(. ..).include?(e.name) } while queue.any? entry = queue.shift if entry.directory? && !%w(. ..).include?(::File.basename(entry.name)) queue += entries("#{path}/#{entry.name}").map do |e| e.name.replace("#{entry.name}/#{e.name}") e end end if ::File.fnmatch(pattern, entry.name, flags) if block_given? yield entry else results << entry end end end return results unless block_given? end
def initialize(sftp)
def initialize(sftp) @sftp = sftp end