I wrote a code by using var fs= require("fs").promises;
var fs= require("fs").promises;
async function checkFileLoc(folderPath, depth) {
depth -= 1;
let files = await fs.readdir(folderPath);
files = await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const stats = await fs.stat(filePath);
if (stats.isDirectory() && depth > 0) {
return checkFileLoc(filePath, depth);
} else if (stats.isFile()) return filePath;
else return null;
})
);
return files
.reduce((all, folderContents) => all.concat(folderContents), [])
.filter((e) => e != null);
}
Then I install fs-extra
package in my project and replace var fs=require("fs");
to var fse=require("fs-extra");
and made changes in my code like this
var fse=require("fs-extra");
async function checkFileLoc(folderPath, depth) {
depth -= 1;
let files = await fse.readdir(folderPath);
files = await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const stats = await fse.stat(filePath);
if (stats.isDirectory() && depth > 0) {
return checkFileLoc(filePath, depth);
} else if (stats.isFile()) return filePath;
else return null;
})
);
return files
.reduce((all, folderContents) => all.concat(folderContents), [])
.filter((e) => e != null);
}
when I was using fs
i was getting the desire output and someone told me fs-extra
is advance then fs
and all you have to do is replace fs
with fse
my code not working properly any solution where i have made mistake ?
var fs= require("fs").promise;
is has i made to read all the files present inside directoriesFilePath=
[/home/work/test/abc.html
/home/work/test/index.js
/home/work/test/product.js
]
var fse=require("fs-extra")
emptyfilePath=[]
My output don't come while using fs-extra
I tried to figure it out what changes i have to made if I am using var fse= require("fs-extra");
so my code will look like this
var fse=require("fs-extra");
async function checkFileLoc(folderPath, depth) {
depth -= 1;
let files = fse.readdirSync(folderPath);
files = await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const stats = fse.statSync(filePath);
if (stats.isDirectory() && depth > 0) {
return checkFileLoc(filePath, depth);
} else if (stats.isFile()) return filePath;
else return null;
})
);
return files
.reduce((all, folderContents) => all.concat(folderContents), [])
.filter((e) => e != null);
}
I replaced my
var fs=require("fs");
to var fse=require("fs-extra");
let files = await fs.readdir(folderPath);
with let files = fs.readdirSync(folderPath);
const stats = await fs.stat(filePath);
with const stats = fs.statSync(filePath);
now i am getting my desire output