35

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.

Belisarius
  • 21
  • 1
  • 1
  • 7
Mdlc
  • 7,128
  • 12
  • 55
  • 98

5 Answers5

49

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)
  }
});
raam86
  • 6,785
  • 2
  • 31
  • 46
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.

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
  • As i can t edit my comment, the link to the doc is https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options –  May 14 '15 at 16:26
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");
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    You will either need to put the zip creation in the callback of `readFile`, or use `readFileSync` instead – Bergi Jul 03 '13 at 13:51
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'))
Community
  • 1
  • 1
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
-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")

});
Francois
  • 3,050
  • 13
  • 21