1

I have a piece of code that checks for the presence of .json. I need the code to look for .json only in a specific folder. How can I do it?

if (extName === '.json') { 
    const fullFileNamed = '../../theme-default/resource/i18n'
    // console.log (fullFileNamed, "fullFileNamed")

    if(fullFileNamed) {
      console.log ( fullFileNamed, "fullFileNamed")
      const fileContent = fs.readFileSync(fullFileName, 'utf8')
      console.log (fileContent, "fileContent")
    } 
}   

this if is not correct. how to do it right?

Maximilian Fixl
  • 670
  • 6
  • 23
Anya Moroz
  • 81
  • 1
  • 10
  • I think this will help you: https://stackoverflow.com/questions/2727167/how-do-you-get-a-list-of-the-names-of-all-files-present-in-a-directory-in-node-j – Benjamin Dec 18 '19 at 10:25
  • you need to correct the variable name `fullFileNamed` in the line `const fileContent = fs.readFileSync(fullFileName, 'utf8')` – Sumit Surana Dec 18 '19 at 10:41
  • do you want to check for the json file and print content of that file – Abhishek-Saini Dec 18 '19 at 10:43

2 Answers2

0

The if condition is fine, the variable name is misspelled in const fileContent = fs.readFileSync(fullFileName, 'utf8') and it should be corrected to const fileContent = fs.readFileSync(fullFileNamed, 'utf8')

if (extName === '.json') { 
    const fullFileNamed = '../../theme-default/resource/i18n'
    // console.log (fullFileNamed, "fullFileNamed")

    if(fullFileNamed) {
      console.log ( fullFileNamed, "fullFileNamed")
      const fileContent = fs.readFileSync(fullFileNamed, 'utf8')
      console.log (fileContent, "fileContent")
    } 
}  
Sumit Surana
  • 1,554
  • 2
  • 14
  • 28
0

You can use fs.access(path[, mode], callback), the optional mode argument can be used to check the accessibility of the file, for example fs.constants.F_OK check if the file exists and fs.constants.R_OK indicates that the file is readable. (The full list of File Access Constants)

if (extName === '.json') { 
        const fullFileNamed = '../../theme-default/resource/i18n';
        // console.log (fullFileNamed, "fullFileNamed")
        // Check if the file exists in the current directory, and if it is readable.
        fs.access(fullFileNamed, fs.constants.F_OK | fs.constants.R_OK, (err) => {
          if (err) {
            console.error(`${fullFileNamed} ${err.code === 'ENOENT' ? 'does not exist' : 'is not readable'}`);
          } else {
            console.log(`${fullFileNamed} exists, and it is readable`);
            const fileContent = fs.readFileSync(fullFileNamed, 'utf8');
            console.log ('fileContent', fileContent);
          }
        });
}   

For synchronous tests use fs.accessSync(path[, mode]):

try {
  fs.accessSync(fullFileNamed, fs.constants.F_OK | fs.constants.R_OK);
  console.log(`${fullFileNamed} exists, and it is readable`);
  const fileContent = fs.readFileSync(fullFileNamed, 'utf8');
  console.log ('fileContent', fileContent);
} catch (err) {
  console.error(`${fullFileNamed} ${err.code === 'ENOENT' ? 'does not exist' : 'is not readable'}`);
}
Fraction
  • 11,668
  • 5
  • 28
  • 48