Getting file system stats with fs.stat

Getting the stats of a file is quick and easy in node.js with just the core file system modules fs.stat method. A stat object contains useful information about a file such as when it was last modified, and the data size of it. A stat object also contains methods that can be used to find if the current file is in fact a file, or a directory.

basic example of fs.stat

For a basic example of fs.stat I was able to get the stats of a readme file in a demo folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let fs = require('fs'),
path = require('path'),
cwd = process.cwd();
fs.stat(path.join(cwd, 'README.md'), function (e, stats) {
if (e) {
console.log(e);
}
console.log(stats);
});

Find out if an item in a list is a file

When getting a stat object with fs.stat, there are many useful stats on the file, but there are also useful methods like stat.isFile that can be used to find out of an item in a dir is a file, or a directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
let fs = require('fs'),
path = require('path'),
cwd = process.cwd(),
dir_used = process.argv[2] || cwd;
let getDirList = function (dir) {
dir = dir || cwd;
return new Promise(function (resolve, reject) {
// read the contents of the dir
fs.readdir(dir, function (e, list) {
if (e) {
reject(e.message);
}
resolve(list);
});
});
};
let listFiles = function (dir) {
let i = 0,
len = 0,
files = [];
dir = dir || dir_used;
return new Promise(function (resolve, reject) {
getDirList(dir).then(function (list) {
let i = 0,
len = list.length,
next = function () {
let file = list[i];
fs.stat(path.join(dir, file), function (e, stat) {
//console.log(file);
//console.log(stat.isFile());
if (e) {
reject(e.message);
}
if (stat.isFile()) {
files.push(file);
}
i += 1;
if (i >= len) {
resolve(files);
} else {
next();
}
});
};
next();
//});
});
});
};
listFiles(dir_used).then(function (list) {
console.log(list);
});