-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.js
More file actions
79 lines (66 loc) · 2.02 KB
/
list.js
File metadata and controls
79 lines (66 loc) · 2.02 KB
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
var path = require("path");
var fs = require("fs");
//joining path of directory
const _path = ClearParam(process.argv[2]);
console.log(_path)
let _isList = (() => {
if((process.argv[3] || "") === "-l")
return true;
if ((process.argv[2] || "").includes("-l")) {
return true;
}
return false;
})();
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) {
try {
const { time, size } = createdDate(file);
fileList += `${time} , ${size} b ${file.name} \n`;
} catch (error) {
fileList += file.name + " ";
}
} else {
fileList += file.name + " ";
}
}
});
console.log("");
console.log(fileList);
});
function ClearParam(Param = '')
{
if (Param.includes("\""))
return Param.replace("\"", "\\").split(' ')[0];
else
return Param.trim();
}