Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.readdir failing on and don't know why

Running a node app to read a directory

fs.readdirSync('‎/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward');

I consistently get

 Stack:
    error properties: Object({ errno: -2, syscall: 'scandir', code: 'ENOENT', path: '‎/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward' })
    Error: ENOENT: no such file or directory, scandir '‎/Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward'

But I know the directory is there

ls -al /Users/edwardthompson/Dropbox/Apps/EvidentiaSoftware/records/thompson-hayward
    total 0
    drwxr-xr-x@ 9 edwardthompson  staff  288 Jan 18  2019 .
    drwxr-xr-x@ 6 edwardthompson  staff  192 Aug 29 12:30 ..
    drwxr-xr-x@ 9 edwardthompson  staff  288 Jan 18  2019 Births
    drwxr-xr-x@ 4 edwardthompson  staff  128 Jan 18  2019 Census
    drwxr-xr-x@ 3 edwardthompson  staff   96 Jan 18  2019 Deaths
    drwxr-xr-x@ 7 edwardthompson  staff  224 Jan 18  2019 Marriages
    drwxr-xr-x@ 4 edwardthompson  staff  128 Jan 18  2019 Military
    drwxr-xr-x@ 3 edwardthompson  staff   96 Jan 18  2019 Other
    drwxr-xr-x@ 8 edwardthompson  staff  256 Jan 18  2019 books

Not sure how to proceed

fs.lstat gives me the same ENOENT results. The Dropbox directory has extended attributes (can't find getfattr)

I tried /user/... and it did not make a difference

like image 564
ed4becky Avatar asked Aug 31 '19 01:08

ed4becky


People also ask

What is the difference between Readdir and readdirSync?

readdir(). It is the same as fs. readdirSync(), but has the callback function as the second parameter.

What is FS Readdir?

The fs. readdir() method is used to asynchronously read the contents of a given directory. The callback of this method returns an array of all the file names in the directory. The options argument can be used to change the format in which the files are returned from the method.

What is FS Dirent?

isDirectory() method is an inbuilt application programming interface of class fs. Dirent within File System module which is used to check if the particular dirent describes a Directory or not. Syntax: const dirent.isDirectory()


2 Answers

const path = require('path');

const dirName = path.join(__dirname, '‎/ (../)x? /records/thompson-hayward');

(../)x? replace this with ../ how many times you need

fs.readdirSync(dirName).forEach(folder => {
// code here
});
like image 161
Fiodorov Andrei Avatar answered Sep 22 '22 20:09

Fiodorov Andrei


You could try using path.resolve():

fs.readdirSync(path.resolve(‘Users’, ‘edwardthompson’, ‘Dropbox’, ‘Apps’, ‘EvidentiaSoftware’, ‘records’, ‘thompson-hayward’));

like image 29
b3nThomas Avatar answered Sep 20 '22 20:09

b3nThomas