Results 1 to 3 of 3

Thread: [RESOLVED] reading the content of a text file...

  1. #1

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    Resolved [RESOLVED] reading the content of a text file...

    Hi,
    The following script reads only the first line of the text file..
    How can I code it so that it is able to read all of the lines in the file..
    Thanks.

    VB Code:
    1. <script>
    2. function ReadIt() {
    3.      var filename = 'c:/27temp.txt';
    4.      if (confirm('Do you want to see what we wrote in your file?')) {
    5.           var fso, a, ForReading;
    6.           ForReading = 1;
    7.           fso = new ActiveXObject('Scripting.FileSystemObject');
    8.           file = fso.OpenTextFile(filename, ForReading, false);
    9.           var name = file.readline();
    10.           var password = file.readline();
    11.           file.Close();
    12.           document.write(name + '<br>');
    13.  
    14.           document.write(password);
    15.       alert(name +"...." + password)
    16.      }
    17. }
    18. </SCRIPT>

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: reading the content of a text file...

    I don't know what is the format of your text file, and I don't know how you want the windows to alert.. Here is what I come up with .

    The text file format is like:

    Andy
    123
    Mary
    456


    Code:
    <script>
    function ReadIt() { 
         var filename = 'test.txt'; 
         if (confirm('Do you want to see what we wrote in your file?')) { 
              var fso, a, ForReading; 
              ForReading = 1; 
              fso = new ActiveXObject('Scripting.FileSystemObject'); 
              file = fso.OpenTextFile(filename, ForReading, false);
    		 
    		   while (!file.AtEndOfStream) {
    				 var name = file.readline(); 
    				 var password = file.readline(); 
    				 document.write(name + "<br>");
    				 document.write(password + "<br>");
    			}
    
    			alert(name +"...." + password);
    			
    		 file.Close();
    		  
         
         } 
    } 
    </SCRIPT> 
    
    <body onLoad=ReadIt()>
    I think one var for file.readline() is enough but again I dont' know what format of the text file is.

  3. #3

    Thread Starter
    Fanatic Member merhaba's Avatar
    Join Date
    Sep 2002
    Location
    Istanbul,Bartin-Gallipoli(Gelibolu-Canakkale)
    Posts
    601

    Re: reading the content of a text file...

    I think what I need is a "for loop" to read the content of the text file located in "C:\"
    Could you anyone please add a "for-next" loop so I can read display the output...?
    thanks


    VB Code:
    1. <html><head>
    2. <SCRIPT LANGUAGE='JavaScript'>
    3. var password='theirpassword  şifreeeeeee';
    4. var name='username......isim';
    5.  
    6. function WriteToFile() {
    7.      var filename = 'c:/27temp.txt';
    8.      var fso = new ActiveXObject('Scripting.FileSystemObject');
    9.      if (fso.FileExists(filename)) {
    10.           var a, ForAppending, file;
    11.           ForAppending = 8;
    12.           file = fso.OpenTextFile(filename, ForAppending, false);
    13.           file.WriteLine(name);
    14.           file.WriteLine(password);
    15.           }
    16.      else {
    17.           var file = fso.CreateTextFile(filename, true);
    18.           file.WriteLine(password);
    19.           file.WriteLine(name);
    20.           }
    21.      file.Close();
    22.      }
    23.  
    24. function ReadIt() {
    25.      var filename = 'c:/27temp.txt';
    26.      if (confirm('Do you want to see what we put on your computer?')) {
    27.           var fso, a, ForReading;
    28.           ForReading = 1;
    29.           fso = new ActiveXObject('Scripting.FileSystemObject');
    30.           file = fso.OpenTextFile(filename, ForReading, false);
    31.           var name = file.readline();
    32.           var password = file.readline();
    33.           file.Close();
    34.           document.write(name + '<br>');
    35.           document.write(password);
    36.      }
    37. }
    38. </SCRIPT>
    39. </head>
    40. <body onload='WriteToFile();ReadIt()'>
    41. </body>
    42. </html>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width