Is there any way that i can get a folder stats with node js?.
I want a function in nodejs
that i can see the folder stats not a file state
i have tried this function
fs.stat(path,(error,state)=>{
console.log(state)
})
but it seems like it just works for files not folders im working on server-side with node.js my code in case you need it
let folders=fs.readdirSync(path.join(__dirname,'../movies/'));
folders.map((paths)=>{
fs.stat(paths,(f,state)=>{
console.log(f,state)
})
})
files in the above code have only folders state is undefined and thats what i get an error
{ Error: ENOENT: no such file or directory, stat 'C:\Users\DELL N5559\Desktop\stock\Folder'
errno: -4058,
code: 'ENOENT',
syscall: 'stat',
path: 'C:\\Users\\DELL N5559\\Desktop\\stock\\Folder' }
enter code here
enter code here
When used properly, fs.stat()
gives this information on a folder taken directly from a console.log(stats)
in this code (as run on Windows, but I believe it should work on other platforms as well):
const fs = require('fs');
fs.stat("html", function(err, stats) {
if (err) {
console.log(err);
} else {
console.log(stats);
}
});
Result in console:
Stats {
dev: 2525584580,
mode: 16822,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: undefined,
ino: 281474976939511,
size: 0,
blocks: undefined,
atimeMs: 1517560386009.7627,
mtimeMs: 1517560386009.7627,
ctimeMs: 1517560386009.7627,
birthtimeMs: 1517560385994.137,
atime: 2018-02-02T08:33:06.010Z,
mtime: 2018-02-02T08:33:06.010Z,
ctime: 2018-02-02T08:33:06.010Z,
birthtime: 2018-02-02T08:33:05.994Z
}
Per the doc, the birthtime
may or may not be the creation date (varies by OS). ctime
is not creation time (it's file node changed time, which is node parameters, not file content).
From the doc:
birthtime "Birth Time" - Time of file creation. Set once when the file is created. On filesystems where birthtime is not available, this field may instead hold either the ctime or 1970-01-01T00:00Z (ie, unix epoch timestamp 0). This value may be greater than atime or mtime in this case. On Darwin and other FreeBSD variants, also set if the atime is explicitly set to an earlier value than the current birthtime using the utimes(2) system call.
Which seems like a way of saying, it's just a big mess and varies by platform. So, if you wanted to use it for something useful, you'd probably have to test the heck out of your platform to see if it provided what you really need.
Related discussions:
Is there still no Linux kernel interface to get file creation date?
How to get file created date using fs module?
In addition, in your code, you are not passing enough of a path to you fs.stat()
function. fs.readdir()
returns just the file/dir name (no path attached) so when you then try to do:
folders.map((paths)=>{...}
The paths
variable is just a single filename that is not in the current directory. To make things work, you have to put the path back on it before calling fs.stat()
.
let root = path.join(__dirname,'../movies/');
let folders = fs.readdirSync(root);
folders.map(name => {
let fullname = path.join(root, name);
fs.stat(fullname,(err, state) => {
console.log(err, state)
});
});