• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 959
  • Last Modified:

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.
0
girlswants_me
Asked:
girlswants_me
  • 5
  • 2
  • 2
1 Solution
 
girlswants_meAuthor Commented:
anybody?
0
 
sathishvCommented:
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.
0
 
girlswants_meAuthor Commented:
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>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sathishvCommented:
Is it a file not found error. Try giving the full path and use either '/' or '\\' as separator.
0
 
ThaSmartUnoCommented:
if you are trying to run this on a web site and not on your own computer ActiveXObject will not work
for a web site look at this open Q
http:/Q_21381676.html
0
 
girlswants_meAuthor Commented:
<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>
0
 
girlswants_meAuthor Commented:
TheSmartUno,

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

best regards,
0
 
ThaSmartUnoCommented:
So why did I get a C?  (did it not work for you?)
0
 
girlswants_meAuthor Commented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now