2

How do we get the file full path in response when we use method sftp.dir.entries?

I want the file content of a directory(recursive). I have used sftp.dir.entries method to list the files present. How should I get the contents?

user3636388
  • 187
  • 2
  • 24
  • With FTP you should recursively issue the change directory command to navigate into directories and list their content. It can’t be done in one shot. – Aleksei Matiushkin Jun 06 '17 at 12:09

1 Answers1

2

Perhaps you are looking for the glob method:

Works as ::Dir.glob, matching (possibly recursively) all directory entries under path against pattern. If a block is given, matches will be yielded to the block as they are found; otherwise, they will be returned in an array when the method finishes.

Because working over an SFTP connection is always going to be slower than working purely locally, don't expect this method to perform with the same level of alacrity that ::Dir.glob does; it will work best for shallow directory hierarchies with relatively few directories, though it should be able to handle modest numbers of files in each directory.

So, for instance (also taken from the documentation):

sftp.dir.glob("/remote/path", "**/*.rb")

should return all the *.rb files in any directory level on the remote machine. To get all files, use "**/*" instead. See also the Dir class' glob method.

Kathryn
  • 1,557
  • 8
  • 12