I am trying to read a JSON File which is in folder within a folder and in another folder and this file is created on my local machine while performing some task but the problem is every time folder name get change as folder name is generated by Math.random() method but with prefix of _createdFolder_ so its look like this _createdFolder_012 in place of 012 it can be 454 or 782 or any number as it is generate by Math.random() method if the folder name is fixed I can easily read the file but file name get changed and its been hard to read
and I want to read a json file which is path look like this
folderPath
_createdFolder_563/data/systemCode/access.json
as I want to read and fetch json file which is created on my local machine while performing some task but if the folder name is fixed I can easily read it but due to Math.random() its hard to read a file
fs.readFile("_createdFolder_501/data/systemCode/access.json", function(err, data) {
if (err) throw err;
const JsonData = JSON.parse(data);
console.log(JsonData);
});
is there any possible way I can read the folder which is created by Math.random()
Yes.
Meet FS.readdir(path). It lists the contents of a directory.
You will need to invoke that, giving it the path to the folder that contains the randomly-named folder. That will give you a list of all the files and folders.
Then you need to search that list for an item whose name matches that pattern. Then you have the name you need, and you can do the rest of your logic.