0

Here I am putting the file names in to filename.txt, but I am not getting how to compare with existing file in the filename.txt

const yargs = require('yargs')
const fs = require('fs')
const command = process.argv[2]; // I am giving in terminal like nodejs app.js file1.txt//
var argv = fs.appendFile('filename.txt', command, (err) => {
    if (err) throw err;
    console.log('The files were updated!');
    console.log(argv)
});

The contents in text file will be filename ,my question is how to take and compare with new filename(whether they are matching or not )

Robert
  • 7,394
  • 40
  • 45
  • 64

1 Answers1

0

Use fs.readFile to read the text file and then compare the process.argv[2] text with each filename. You don't really need to use the yargs package for this.

const fs = require('fs')
const command = process.argv[2];

fs.readFile("./filename.txt", "utf8", function (error, dataStr) {
    if (error) {
        console.log(error);
    }

    data = dataStr.split("\n"); // presumes each 'filename' to check is on a new line

    if (data.indexOf(command) > -1) {
        console.log('It exists in the filename.txt file');
    } else {
        console.log('It doesnt exist in the filename.txt file');
    }

});

This presumes your 'filename.txt' file looks like this:

file1.txt
file2.txt
file3.txt
jpmc
  • 1,147
  • 7
  • 18