spiffytech
spiffytech

Reputation: 6592

How to determine if an SFTP file is a directory in Node.js?

The ssh2 library's SFTP readdir method gives me back all the files in the remote directory. How can I tell if any of of them are directories?

Here's some example output from the library:

{ filename:
     'myfile',
    longname:
     '-rwxr-x---    1 myuser mygroup 19036227 Nov 21 11:05 myfile',
    attrs:
     Stats {
       mode: 33256,
       permissions: 33256,
       uid: 603,
       gid: 1014,
       size: 19036227,
       atime: 1542859216,
       mtime: 1542816340 } }

Upvotes: 0

Views: 750

Answers (1)

spiffytech
spiffytech

Reputation: 6592

The file's mode contains bits indicating its type. You can check it like this:

const fs = require('fs');

function isDir(mode) {
  return (mode & fs.constants.S_IFMT) == fs.constants.S_IFDIR;
}

isDir(myfile.attrs.mode);

Upvotes: 3

Related Questions