Link to home
Start Free TrialLog in
Avatar of girlswants_me
girlswants_me

asked on

Retrieve information from any text file using javascript

Hi everyone!

 anybody can help me on how to retrieve a certain information from a txt file. Like for example (Please see below txt file).

Products.txt
==============
apple          100.00            100pcs.
banana         50.00             30pcs.
orange          20.00             2pcs.

My problem now is how to retrieve the information for "banana" from the Products.txt.
Using a JavaScript, i would like to have the information of "banana" which is "50.00" and "30pcs." and displaying them.

Anybody can help me?

Thank you in advance.
Avatar of girlswants_me
girlswants_me

ASKER

anybody?
You can use

var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile("product.txt", 1);
var content = f.ReadAll();

This will popup a security warning because of the ActiveXObject usage.

This will return you the entire content of the text file. If you have got the contents in some kind of delimited pattern, you can then parse the contents to get any particular line.

For instance, if the columns in the file are separated by a space:

var column1 = new Array();
var column2 = new Array();
var column3 = new Array();
var lines = content.split("\n");
for(var i=0; i<lines.length; i++){
  column1[i] = lines[i].split(" ")[0];
  column2[i] = lines[i].split(" ")[1];
  column3[i] = lines[i].split(" ")[2];
}

You can then loop through the columns arrays to get whatever value you want or use some other method like custom objects to store the data.
Sorry, your code does'nt work. I received a RUNTIME ERROR

Error:Automation server can't create object

my code is:

<HTML>
 
 <SCRIPT Language='JavaScript'>


  function readme(){
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var f = fso.OpenTextFile("product.txt", 1);
  var content = f.ReadAll();

  var column1 = new Array();
  var column2 = new Array();
  var column3 = new Array();
  var lines = content.split("\n");
  for(var i=0; i<lines.length; i++){
   column1[i] = lines[i].split(" ")[0];
   column2[i] = lines[i].split(" ")[1];
   column3[i] = lines[i].split(" ")[2];
   }
  }

 </SCRIPT>

 <BODY>
 <A href="#" onclick='readme()'>click here</A>
 </BODY>
</HTML>
Is it a file not found error. Try giving the full path and use either '/' or '\\' as separator.
ASKER CERTIFIED SOLUTION
Avatar of ThaSmartUno
ThaSmartUno

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<html>
<body>
<script type="text/javascript">
var contents;

function showContent(){
    var content = self.frames['textFile'].document.body.innerText;
        contents = content;
}


function displaycontent(){
   alert(contents.substring(contents.indexOf("Watercress"),contents.indexOf("Watercress")+80));

}

</script>
<iframe name="textFile" src="PLufile.txt" style="display:none" onload="showContent()"></iframe>
<a href='#' onclick='displaycontent()'>click here</a>
</body></html>
TheSmartUno,

 Thanks for the link. It works using the iframe as the object holds the text.

best regards,
So why did I get a C?  (did it not work for you?)
Sorry, i thought that "A" should be given if the answer is directly given. I give "C" because the answer was not coming from you directly.