Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: can you write to a specific text file line using FileSystemObject

1. Well, it's pretty inefficient to have to keep invoking document.getElementById("xxx").value all the time.

Better would be to do
Code:
    var what = document.getElementById("xxx").value;
at the top of the function so you only do that code once.

But realistically, the I/O to the file is going to be eating up so much time that a minor optimization like that likely won't matter.

By the by: If you'd like to ignore the case (upper/lower) when you do the indexOf( ) then a better way would be to construct a regular expression.

Something iike:
Code:
    var what = new RegExp( document.getElementById("xxx").value, "gi" );
and then replace the indexOf with
Code:
    if ( what.test( lines[n] ) ...

2. Originally Posted by Old Pedant
By the by: If you'd like to ignore the case (upper/lower) when you do the indexOf( ) then a better way would be to construct a regular expression.

Something iike:
Code:
    var what = new RegExp( document.getElementById("xxx").value, "gi" );
and then replace the indexOf with
Code:
    if ( what.test( lines[n] ) ...
but what if they type 9-11-2001, $19.95, or even 1.5 into #xxx ? 3. Originally Posted by rnd me but what if they type 9-11-2001,$19.95, or even 1.5 into #xxx ?
Good point. Okay, so just use good old .toLowerCase() on both "what" and the lines[n].

(But why wouldn't 9-11-2001 still work? Not that it matters. Clearly the period in the others is a killer, not to mention many other possible characters.)

4. Originally Posted by Old Pedant
Good point. Okay, so just use good old .toLowerCase() on both "what" and the lines[n].

(But why wouldn't 9-11-2001 still work? Not that it matters. Clearly the period in the others is a killer, not to mention many other possible characters.)
i guess it would, outside of brackets, good eye.

you can do:
Code:
  var what = new RegExp( document.getElementById("xxx").value.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1'), "gi" );
with any input, afaik...

5. DOH. Yeah, I should have thought of that.

But what the heck, you could just as easily do:
Code:
  var what = new RegExp( document.getElementById("xxx").value.replace(/(\W)/g, '\\$1'), "gi" ); No? 6. Hi again, I came across a slight problem. The code works fine at deleting the line a word is in but it is not specific to the word entered such as "number" because another line in the example set from the original post has the word "numbered" and that gets deleted because it also contains the shorter "number" word. Any thoughts on a way to make the one word "number" a string or with a limit boundary at start and end to only remove the line with the exact match to actual word used. I tried this..... ("/\\b +what+ \\b/i") as seen in code below but know I have it wrong in some way. I know you are a lot smarter than I but I have to go with what works for me even if it is not a perfect code in your view. I stuck with the indexOf and used the var 'what' for the get element by id part as overall the code done that way works and I can follow it in my limited way. The added boundary to variable 'what' of course fails to work the way I have it and I need some help setting that up correctly. Code: <script type="text/javascript"> function removeLineContaining() { var what = document.getElementById("xxx").value.toLowerCase(); var fso = new ActiveXObject("Scripting.FileSystemObject"); var infile = fso.OpenTextFile("c:\\path\\to\\file.txt"); var lines = infile.ReadAll().split("\n"); var removed = [ ]; infile.Close( ); for ( var n = lines.length-1; n >= 0; --n ) { if ( lines[n].toLowerCase().indexOf("/\\b "+what+" \\b/i") >= 0 ) { removed.push( lines[n].toLowerCase() ); lines.splice( n, 1 ); } } var outfile = fso.CreateTextFile("c:\\path\\to\\file.txt", true); // true==overwrite outfile.write( lines.join("\n") ); outfile.Close(); return removed; } </script> Martin. 7. indexOf() can't use a regular expression. You would need to adapt the regexp code we showed you: Code:  // first "escape" all except \w characters... // that is, convert "ab-12" to "ab\-12" var what = document.getElementById("xxx").value.replace(/(\W)/g, "\\$1");
// then use that to create a regexp looking only for *WORDS*:
what = new Regexp("\\b" + what + "\\b", "gi" );
...
for ( var n = lines.length-1; n >= 0; --n )
{
if ( what.test( lines[n] ) )
{
removed.push( lines[n].toLowerCase() );
lines.splice( n, 1 );
}
}

If you have used the various suggestions and failed to get some working, take a look at the spelling of RegExp as I have on a few occasions kept putting Regexp without an upper case E and things just fail.

Martin.

Page 2 of 2 First 12

#### Posting Permissions

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