40

Is it possible to open a text file with JavaScript (location like http://example.com/directory/file.txt) and check if the file contains a given string/variable?

In PHP this can be accomplished easily with something like:

$file = file_get_contents("filename.ext");
if (!strpos($file, "search string")) {
    echo "String not found!";
} else {
    echo "String found!";
}

Is there a way to do this? I'm running the "function" in a .js file with Node.js, appfog.

0

5 Answers 5

53

You can not open files client side with javascript.

You can do it with node.js though on the server side.

fs.readFile(FILE_LOCATION, function (err, data) {
  if (err) throw err;
  if(data.indexOf('search string') >= 0){
   console.log(data) //Do Things
  }
});

Newer versions of node.js (>= 6.0.0) have the includes function, which searches for a match in a string.

fs.readFile(FILE_LOCATION, function (err, data) {
  if (err) throw err;
  if(data.includes('search string')){
   console.log(data)
  }
});
Sign up to request clarification or add additional context in comments.

Comments

15

You can also use a stream. They can handle larger files. For example:

var fs = require('fs');
var stream = fs.createReadStream(path);
var found = false;

stream.on('data',function(d){
  if(!found) found=!!(''+d).match(content)
});

stream.on('error',function(err){
    then(err, found);
});

stream.on('close',function(err){
    then(err, found);
});

Either an 'error' or 'close' will occur. Then, the stream will close since the default value of autoClose is true.

2 Comments

As i can t edit my comment, the link to the doc is nodejs.org/api/fs.html#fs_fs_createreadstream_path_options
It is buggy as it is because the stream may "cut" the content to find in two. There is some additional work to do here to make it work.
2

Is there a, preferably easy, way to do this?

Yes.

require("fs").readFile("filename.ext", function(err, cont) {
    if (err)
        throw err;
    console.log("String"+(cont.indexOf("search string")>-1 ? " " : " not ")+"found");
});

1 Comment

You will either need to put the zip creation in the callback of readFile, or use readFileSync instead
1

OOP way :

var JFile=require('jfile');
var txtFile=new JFile(PATH);
var result=txtFile.grep("word") ;
 //txtFile.grep("word",true) -> Add 2nd argument "true" to ge index of lines which contains "word"/

Requirement :

npm install jfile

Brief :

((JFile)=>{
      var result= new JFile(PATH).grep("word");
})(require('jfile'))

Comments

-1

From the client side you can definitely do this :

var xhttp = new XMLHttpRequest(), searchString = "foobar";

xhttp.onreadystatechange = function() {

  if (xhttp.readyState == 4 && xhttp.status == 200) {

      console.log(xhttp.responseText.indexOf(searchString) > -1 ? "has string" : "does not have string")

  }
};

xhttp.open("GET", "http://somedomain.io/test.txt", true);
xhttp.send();

If you want to do it on the server side with node.js use File System package this way :

var fs = require("fs"), searchString = "somestring";

fs.readFile("somefile.txt", function(err, content) {

    if (err) throw err;

     console.log(content.indexOf(searchString)>-1 ? "has string" : "does not have string")

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.