-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.js
62 lines (48 loc) · 1.75 KB
/
list.js
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
var path = require('path');
var fs = require('fs');
//joining path of directory
const _path = process.argv[2];
const _isList = ((process.argv[3] || "") === "-l")
var directoryPath =_path;
console.log(directoryPath)
console.log("")
//passsing directoryPath and callback function
fs.readdir(directoryPath, { withFileTypes: true }, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
let fileList = "";
const dirCount = files.filter(x => x.isDirectory() === true).length
const fileCount = files.filter(x => x.isDirectory() === false).length
console.log(` ${dirCount} directories in ${directoryPath}`)
console.log(` ${fileCount} files in ${directoryPath}`)
console.log("")
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
if(file.isDirectory()){
console.log('\x1b[36m%s\x1b[0m', file.name);
}
});
files.forEach(function (file) {
// Do whatever you want to do with the file
function createdDate (file) {
let __filepath = directoryPath + '\\' + file.name;
const { birthtime ,size} = fs.statSync(__filepath)
//console.log(fs.statSync(__filepath))
return {time : birthtime.toDateString() , size};
}
if(!file.isDirectory())
{
if(_isList){
const {time , size} = createdDate(file);
fileList += `${time} , ${size} b ${file.name} \n`
} else{
fileList += file.name+ " "
}
}
});
console.log("");
console.log(fileList)
});