0

I have some file names in "LATIN" but the fs.existsSync function is not able to recognise it

1689017335025-Naan En Nesarudaiyavan â mix.json

I tired with buffer too but still not able to get the proper response.

I am providing the proper path of the file too but still its not detecting that file.

I am using the fs module in electron js.

Other files other than Latin are been properly detected by fs.existsSync

const localDownloadPath = path.join(app.getPath('userData'), ${endpath}/${detail.name});
if(fs.existsSync(localDownloadPath)){ 
   return true; 
}else {return false;}

1 Answers1

0

As said in Node.js docs,

Using fs.exists() to check for the existence of a file before calling fs.open(), fs.readFile(), or fs.writeFile() is not recommended. Doing so introduces a rare condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file does not exist.

So if you want to read/open a file, rather call the function and catch errors.

Example of fs.readFile():

const fs = require('node:fs');

let fd;

fs.readFile('myFile.txt','r',(err, data) => {
  if(err) {
    if(err.code == 'ENOENT') {
      console.error('myFile.txt does not exist');
      return;
    }
    throw err;
  }
  fd = data;
});

PS: I tried what you described in question as not working for you and it works well for me. Check if you wrote that filename correctly. Screenshot

MfyDev
  • 439
  • 1
  • 12
  • Tried but still not working – Henil Mehta Jul 13 '23 at 11:52
  • @HenilMehta try to replace `${endpath}/${detail.name}` in **your** code with `endpath,"/",detail.name` – MfyDev Jul 13 '23 at 12:51
  • @HenilMehta Are you sure `app.getPath('userData')` function in **your** code returns a valid path? Because `fs.existSync` never throws error - you can even pass to function random symbols and it just returns `false`. – MfyDev Jul 13 '23 at 12:56