I am currently having trouble with what is most likely a very simple function for most.
I have what I call a Find & Read function.
The "find" part of the function utilises a child_process
with the find -name
command in order to locate a given file by name and extension,
the "Read" part of the function is where the problem seems to be.
The "Read" part of the function is supposed to use the results from the command executed in the child_process
, in a seperate variable, in order to start reading the file using fs.readFile
, however everything I have tried presents me with the following error in the console...
Locating File...
File Found: this/way/to/the/File.txt
Reading File...
Reading File Failed!
Error: ENOENT: no such file or directory, open 'this/way/to/the/File.txt'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at /home/user/test/index.js:23:8
at ChildProcess.exithandler (node:child_process:390:7)
at ChildProcess.emit (node:events:527:28)
at maybeClose (node:internal/child_process:1092:16)
at Socket.<anonymous> (node:internal/child_process:451:11)
at Socket.emit (node:events:527:28)
at Pipe.<anonymous> (node:net:710:12) {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: 'this/way/to/the/File.txt'
}
However the isn't correct, the file actually does exist inside the directory found by the command executed in the child_process
but it's still throwing me errors saying the file doesn't exist... if the file didn't exist, it wouldn't have located and printed its location in the console... so I'm not sure where the error is coming from here, hence why I've come StackOverflow for help.
I am using the JavaScript coding below to to try and achieve this...
var exec = require("child_process").exec, child;
var fs = require('fs');
var path = require('path');
// This seems to be working fine to locate the file
var folder = '/just/some/folder/location/';
var file = "File.txt";
console.log("Locating File...")
console.log();
exec('find -name ' + file, { cwd: folder }, (error, stdout, stderr) => {
var filePath = stdout.substring(stdout.indexOf("./") + 2).split("\n")[0];
if (error !== null) {
console.log("Locating Failed Failed!");
console.log(error);
console.log(stderr);
return;
}
// but it fails to read the file afterwards
console.log("File Found: " + filePath); // print filepath in the log to confirm the file was found
console.log();
console.log("Reading File...");
fs.readFile(filePath, 'utf8', (error, data) => {
if (error) {
console.log('Reading File Failed');
console.log(error);
return;
}
});
});
Problem solved.
The problem was located within the filePath
variable.
What was happening was, I didn't join both the folder
variable & the filePath
variable together with path.join()
once the file
was located!
For some reason as well, the instance of .trim("\n")[0])
at the end of the filePath
variable needed to be changed to an instance of .trim("\n"))
... Because for some reason the former coding was causing problems with the function as well.
The below instance of the filePath variable...
var filePath = stdout.substring(stdout.indexOf("./") + 2).split("\n")[0];
Was was changed to the following...
var filePath = path.join(apkFolder, stdout.substring(stdout.indexOf("./") + 2).split("\n"));
Which now runs beautifully, the file gets located and it gets read beautifully!
Here's the Full fixed code below...
var folder = '/just/some/folder/location/';
var file = "File.txt";
console.log("Locating File...")
console.log();
exec('find -name ' + file, { cwd: folder }, (error, stdout, stderr) => {
var filePath = path.join(folder, stdout.substring(stdout.indexOf("./") + 2).split("\n")); // Fix contained here
if (error !== null) {
console.log("Locating File Failed!");
console.log(error);
console.log(stderr);
return;
}
console.log("File Found: " + filePath);
console.log();
console.log("Reading File...");
fs.readFileSync(filePath, 'utf8', (error, data) => {
if (error) {
console.log('Reading File Failed');
console.log(error);
return;
}
});
});