DEV Community

CaptainCook
CaptainCook

Posted on

Reading and writing multiple text files from a directory ENOENT Error

I have folder named "Main" on the desktop that has two folders named "Input" and "Output".These folders are created manually (Right Click-New Folder). The script named Verification.js is outside of the folders named "Input" and "Output" folder and inside the "Main" folder. The script reads multiple text files in "Input" folder, runs the logic for all the text files, writes the updated text files in "Output" folder.

My updated program so far in Verification.js script is as follows:

var fs = require('fs')

fs.readdir('Input', (err, files) => {
if (err) {
//debugger;
console.log(err);
return;
}
files.forEach(file => {
fs.readFile(file, 'utf8', function (err,result) {
if (err) {
return console.log(err);
}
result = //logic;

  fs.writeFile('Output',result,'utf8', function (err) {
    if (err) {
      return console.log(err);
    }
  });

});
});

});

I get the following error: It does not recognize the text files.

C:\Users>cd C:\Users\Desktop\Main

C:\Users\Desktop\Main>node Verification.js

{ [Error: ENOENT: no such file or directory, open 'C:\Users\Desktop\Main\TestFile1.txt']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path:
'C:\Users\\Desktop\Main\TestFile1.txt' }
{ [Error: ENOENT: no such file or directory, open 'C:\Users\Desktop\Main\TestFile2.txt']
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path:
'C:\Users\\Desktop\Main\TestFile2.txt' }

How do I rectify it? It doesnt recognize the input folder in the 'path' attribute as shown in the error and also the text files.

Top comments (0)