0

Intro: I'm trying to make a html application (.htm) to make some business calculations. The issue that comes is that I need to keep records of everything. First I found some visual basic scripts to read/write .mdb files, but that was too complicated for me since I have never worked with vbs. So, I decided to use javascript to read/write .csv file

This is the function I found for reading:

            function displayClassList() {
            var path="log.csv"
            var fso = new ActiveXObject('Scripting.FileSystemObject'),
            iStream=fso.OpenTextFile(path, 1, false);
            document.getElementById("searchResults").innerHTML="";
            while(!iStream.AtEndOfStream) {
                var line=iStream.ReadLine();
                document.getElementById("searchResults").innerHTML += line + "<br/>";
            }
            iStream.Close();
        }

It works good.

The problem I have is when it comes to writing. I can not append text to a new line in the document. This is the script I got:

var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var s = fso.CreateTextFile("./ClassList.csv", true); 
s.WriteLine("helloworld"); 
s.Close(); 
} 

The problem with this script is that it replaces all the existing text with "helloworld". What I want is to write "helloworld" in new line. Any solution for that?

Also, is there any way to edit a specific line, like replacing all text in line x?

Here are the scripts for download so that you can test them : http://ge.tt/7u5bDAV2/v/0

2 Answers 2

1

If you want to append to the file without overwriting the existing contents, you can use the OpenTextFile method - note that the CreateTextFile method you're using truncates the existing contents.

var fso = new ActiveXObject("Scripting.FileSystemObject"); 
var s = fso.OpenTextFile("./ClassList.csv", 8); 
s.WriteLine("helloworld"); 
s.Close(); 

There is no easy way of modifying one line of a text file, unless the modifications you're making leave the line the same length, since otherwise if your changes are shorter you will leave part of the old line unchanged, while if your changes are longer you would overwrite the next line.

More importantly, the FileSystemObject does not support seeking, which you would need in order to jump to a specific line.

If you want to modify one line of the file, your best bet is to:

  1. Open the existing file for reading, and also create a new file for writing
  2. Read the existing file line by line, writing the content you want to keep to the new file
  3. Write your modified line(s) to the new file where needed
  4. Close both files, and rename the new file to replace the old one

Having said that, maybe it would be easier for you if your data file was an HTML or XML document rather than a CSV, since you could then use DOM manipulation functions to read and write it.

Sign up to request clarification or add additional context in comments.

8 Comments

When replacing CreateTextFile with OpenTextFile Error: Invalid procedure call or argument
Line 6 in my document, which is line 3 in the javascript script
The third argument should be one of the Tristate constants shown in the documentation, not false.
Sorry, it is line 2 in the second script. When changing it to OpenText file I get that error, and when changing "true" to tristate constant it's the same thing
You can download th files here: ge.tt/7u5bDAV2/v/0 to test them yourself :) Thank you
|
0

Generally you use "\n" in your string to create new lines. It represents the newline character in a JS string.

Here's how "lines" work in text files. It's just a long sequence of characters, and one of the possible characters is the newline character. Whatever program renders the file when you view it just knows to show text after a newline character below any text that was before it. You could split the string you read by the newline character and get an Array representing each line and work with it that way. Then to write you'd join that Array by the newline character and write the resulting string.

Note that some programs require "\r\n" to represent a proper newline and won't render new lines for just a "\n"...so try "\r\n" as the newline if you're having trouble getting newlines to work for the program you use to view the text files.


EDIT: Since you don't seem to believe me I'll just prove it to you with code. I did it with an .hta file, but the concept is the same.

Made a text file "myText.txt" and an .hta file with the code in it. The text file held the contents:

This is line 1
Line 2
the third line
line 4 and stuff
fifth line

Then in my code I made these two functions for easily reading and writing:

function getFile(fname)
{
    var opener = new ActiveXObject("Scripting.FileSystemObject");
    var pointer = opener.OpenTextFile(fname, 1, true);
    var cont = pointer.ReadAll();
    pointer.Close();
    return cont;
}

function setFile(fname, content)
{
    var opener = new ActiveXObject("Scripting.FileSystemObject");
    var pointer = opener.OpenTextFile(fname, 2, true);
    pointer.WriteLine(content);
    pointer.Close();
}

For the programs I was using it uses "\r\n" for the newline. So that's what the example will use. I simply utilize splitting and joining on the string of content to edit whatever line of it I choose:

var content = getFile('myFile.txt'); // read it
var lineArr = content.split('\r\n'); // now we have an array of the file's lines
lineArr[2] = 'NEW LINE CONTENT!'; // editing third line (indexed from 0)
var newContent = lineArr.join('\r\n'); // make it text again with newlines
setFile("myFile.txt", newContent); // write it

Now the text file looks like this:

This is line 1
Line 2
NEW LINE CONTENT!
line 4 and stuff
fifth line

Bam. Editing individual lines in a text format file by understanding how newlines work in text.

10 Comments

If I use /n + "helloworld" It will replace all text with a new empty line and "jelloworld in the second line.
@DanielRika - It's \n and it's a string...but yeah, that's literally what I just explained. Setting the file to a newline and the text helloworld would go down a line and then helloworld. To edit only part of the text you'd read all the text in and then just edit the part you want in the string, then write it all out again with the new total value. Like I stated, the best way to do that by line would be to read it all in and then split the string by the newline character...then you have an array of each line to work with. Then join the array by the newline character once done and write again.
@DanielRika - It's basically the text file equivalent of <br>. That character represents doing down a line.
What I needed is the below code martin explained. Just a \n wouldn't work in the explained case because the "fso.CreateTextFile" command resets the text document to having no content.
@DanielRika - Which is why I explained that you'd read the content in first, and gave you an explanation of how newlines work in that content so that you could manipulate it.
|

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.