forked from Iggy-Codes/node-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise4.js
More file actions
31 lines (30 loc) · 904 Bytes
/
exercise4.js
File metadata and controls
31 lines (30 loc) · 904 Bytes
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
// Asynchronous
// -----------
var fs = require('fs')
// __dirname : is the directory where the file is
// process.cwd(): is the directory where node is executing
// in this case are the same, but NOT ALWAYS
fs.readdir(__dirname, function (error, data) {
if (error) throw error
data.forEach(function (item) {
fs.lstat(item, function (error, stat) {
if (error) throw error
var prefix
if (stat.isFile()) prefix = 'FILE'
else if (stat.isDirectory()) prefix = 'DIR '
else prefix = '??? '
console.log(prefix + '=> ' + item)
})
})
})
// Synchronous
// -----------
console.log('\n\n Synchronous')
fs.readdirSync(__dirname).forEach(function (item) {
var stat = fs.lstatSync(item)
var prefix
if (stat.isFile()) prefix = 'FILE'
else if (stat.isDirectory()) prefix = 'DIR '
else prefix = '??? '
console.log(prefix + '=> ' + item + ' Synchronous')
})