Skip to content

Commit c177a68

Browse files
fev4targos
authored andcommitted
doc: provide an example to fs.stat()
Provided a small example along its output using fs.stat() to check the stats on two different paths, one a directory and the other a txt file. PR-URL: #28381 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 49c5339 commit c177a68

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

doc/api/fs.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,76 @@ error raised if the file is not available.
30203020
To check if a file exists without manipulating it afterwards, [`fs.access()`]
30213021
is recommended.
30223022

3023+
For example, given the following folder structure:
3024+
3025+
```fundamental
3026+
- txtDir
3027+
-- file.txt
3028+
- app.js
3029+
```
3030+
3031+
The next program will check for the stats of the given paths:
3032+
3033+
```js
3034+
const fs = require('fs');
3035+
3036+
const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
3037+
3038+
for (let i = 0; i < pathsToCheck.length; i++) {
3039+
fs.stat(pathsToCheck[i], function(err, stats) {
3040+
console.log(stats.isDirectory());
3041+
console.log(stats);
3042+
});
3043+
}
3044+
```
3045+
3046+
The resulting output will resemble:
3047+
3048+
```console
3049+
true
3050+
Stats {
3051+
dev: 16777220,
3052+
mode: 16877,
3053+
nlink: 3,
3054+
uid: 501,
3055+
gid: 20,
3056+
rdev: 0,
3057+
blksize: 4096,
3058+
ino: 14214262,
3059+
size: 96,
3060+
blocks: 0,
3061+
atimeMs: 1561174653071.963,
3062+
mtimeMs: 1561174614583.3518,
3063+
ctimeMs: 1561174626623.5366,
3064+
birthtimeMs: 1561174126937.2893,
3065+
atime: 2019-06-22T03:37:33.072Z,
3066+
mtime: 2019-06-22T03:36:54.583Z,
3067+
ctime: 2019-06-22T03:37:06.624Z,
3068+
birthtime: 2019-06-22T03:28:46.937Z
3069+
}
3070+
false
3071+
Stats {
3072+
dev: 16777220,
3073+
mode: 33188,
3074+
nlink: 1,
3075+
uid: 501,
3076+
gid: 20,
3077+
rdev: 0,
3078+
blksize: 4096,
3079+
ino: 14214074,
3080+
size: 8,
3081+
blocks: 8,
3082+
atimeMs: 1561174616618.8555,
3083+
mtimeMs: 1561174614584,
3084+
ctimeMs: 1561174614583.8145,
3085+
birthtimeMs: 1561174007710.7478,
3086+
atime: 2019-06-22T03:36:56.619Z,
3087+
mtime: 2019-06-22T03:36:54.584Z,
3088+
ctime: 2019-06-22T03:36:54.584Z,
3089+
birthtime: 2019-06-22T03:26:47.711Z
3090+
}
3091+
```
3092+
30233093
## fs.statSync(path[, options])
30243094
<!-- YAML
30253095
added: v0.1.21

0 commit comments

Comments
 (0)