Skip to content

Commit c03df4c

Browse files
fev4addaleax
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 8df76e6 commit c03df4c

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
@@ -3082,6 +3082,76 @@ error raised if the file is not available.
30823082
To check if a file exists without manipulating it afterwards, [`fs.access()`]
30833083
is recommended.
30843084

3085+
For example, given the following folder structure:
3086+
3087+
```fundamental
3088+
- txtDir
3089+
-- file.txt
3090+
- app.js
3091+
```
3092+
3093+
The next program will check for the stats of the given paths:
3094+
3095+
```js
3096+
const fs = require('fs');
3097+
3098+
const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
3099+
3100+
for (let i = 0; i < pathsToCheck.length; i++) {
3101+
fs.stat(pathsToCheck[i], function(err, stats) {
3102+
console.log(stats.isDirectory());
3103+
console.log(stats);
3104+
});
3105+
}
3106+
```
3107+
3108+
The resulting output will resemble:
3109+
3110+
```console
3111+
true
3112+
Stats {
3113+
dev: 16777220,
3114+
mode: 16877,
3115+
nlink: 3,
3116+
uid: 501,
3117+
gid: 20,
3118+
rdev: 0,
3119+
blksize: 4096,
3120+
ino: 14214262,
3121+
size: 96,
3122+
blocks: 0,
3123+
atimeMs: 1561174653071.963,
3124+
mtimeMs: 1561174614583.3518,
3125+
ctimeMs: 1561174626623.5366,
3126+
birthtimeMs: 1561174126937.2893,
3127+
atime: 2019-06-22T03:37:33.072Z,
3128+
mtime: 2019-06-22T03:36:54.583Z,
3129+
ctime: 2019-06-22T03:37:06.624Z,
3130+
birthtime: 2019-06-22T03:28:46.937Z
3131+
}
3132+
false
3133+
Stats {
3134+
dev: 16777220,
3135+
mode: 33188,
3136+
nlink: 1,
3137+
uid: 501,
3138+
gid: 20,
3139+
rdev: 0,
3140+
blksize: 4096,
3141+
ino: 14214074,
3142+
size: 8,
3143+
blocks: 8,
3144+
atimeMs: 1561174616618.8555,
3145+
mtimeMs: 1561174614584,
3146+
ctimeMs: 1561174614583.8145,
3147+
birthtimeMs: 1561174007710.7478,
3148+
atime: 2019-06-22T03:36:56.619Z,
3149+
mtime: 2019-06-22T03:36:54.584Z,
3150+
ctime: 2019-06-22T03:36:54.584Z,
3151+
birthtime: 2019-06-22T03:26:47.711Z
3152+
}
3153+
```
3154+
30853155
## fs.statSync(path[, options])
30863156
<!-- YAML
30873157
added: v0.1.21

0 commit comments

Comments
 (0)