Skip to content

Commit dfc6f87

Browse files
authored
Create directory-iterator-async.js
1 parent 9b21dd3 commit dfc6f87

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

directory-iterator-async.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const isFile = file => {
5+
const stats = fs.lstatSync(file);
6+
return stats.isFile();
7+
};
8+
9+
const getFileName = file => {
10+
const fileParts = file.split("/");
11+
return fileParts[fileParts.length - 1];
12+
};
13+
14+
const crawl = (file, indentation) => {
15+
console.log(indentation + getFileName(file));
16+
17+
if (isFile(file)) {
18+
// collect only files
19+
} else {
20+
const files = fs.readdirSync(file);
21+
files.forEach(subfile => {
22+
crawl(file + "/" + subfile, indentation + "\t");
23+
});
24+
}
25+
};
26+
27+
const startingDirectory = "PATH_TO_YOUR_DIRECTORY";
28+
crawl(startingDirectory, 0);

0 commit comments

Comments
 (0)