Hey, Scripting Guy! How Can I Check to See if Two Lines of Text are in a File and, If They Aren’t, Append Those Lines to the File?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Let me first say that your stuff is great. Really helpful scripts mixed with great humor; you guys are pissa’s (Australian slang for “funny”). I have a script that needs to open a file and check to see if two lines of text are in that file. If they are, then I leave the file alone; if they aren’t, I need to append the two lines to the end of the file. Unfortunately, though, I can’t get the script to work: it always appends the two lines, even if both those lines are already in the file. Can you help me?
— GT

Spacer Hey, Scripting Guy! Answer Script Center

Hey, GT, and, g’day; it’s always nice to hear from an Aussie, you know, a bloke from the Back of Bourke, way out there in Woop Woop, as we like to say. No doubt you’re a bit agro about this script that doesn’t work right, you’re probably as cross as a frog in a sock and you’re thinking “Have I gone troppo? Do I have kangaroos loose in the top paddock?” But no drama, mate. Let’s have a squizz here and give it a burl, eh? By the time we’re through, she’ll be apples, and you’ll be happy as Larry.

And best of all, this won’t cost you big bikkies; it’s on the house. We’ll give you the mate’s rate on this one.

If we understand you correctly, you need to modify the All.js file that’s used to configure the Firefox Web browser. That leads to an ovious question: are the Scripting Guys going to spit the dummy just because you’re using Firefox? Well, admittedly, we’re a bit gobsmacked that you aren’t using Internet Explorer. But no worries; after all, we’re all cobbers here, and if you want to use Firefox, well, we’re just going to answer your question and not be stickybeaks as to why you’re using Firefox rather than Internet Explorer.

And that’s no porky.

As you noted in your email, when your script fossicks through the All.js file it’s looking for these two lines of text:

We need a script – and a corker of a script, not a bodgy one – that can determine whether those two lines of text are already in the file. If they aren’t, the script needs to add those lines to All.js. In other words, we need a script that looks a lot like this:

Let’s yabber on a bit about how this script works. As you can see, we start out by defining a pair of constants, ForReading and ForAppending; we’ll need to use these two constants when working with the text file. We then assign the value True to a pair of variables (blnMissingA and blnMissingB). We’re going to use these two variables to keep track of the two lines of text in question, and to indicate whether or not either of those lines is missing. We set the initial value to True because we haven’t found either value yet (at least in part because we haven’t started looking yet).

And if you’re thinking, “This script doesn’t stand a Buckley’s chance of working,” well, be patient. By the time we finish, she’ll be right.

When you look at the next two lines of code you might think, “Holy dooley! The Scripting Guys have come a gutser here!” Actually, what we’re doing here is pretty simple: in fact, all we’re doing is assigning the two target lines of text to the variables strTextA and strTextB. It just looks like we were a bit stonkered when we wrote this code; that’s because we need to insert double quote marks inside the strings. In fact, each instance of Chr(34) represents a double quote mark :

In other words, in line 1 we’re combining these elements and assigning the resulting string to the variable strTextA:



, 0);

When you get right down to it, that’s pretty bog standard, eh?

After we’ve defined the constants and variables our next step is to open the file All.js (which, for the purposes of this script, we’ve stored in the folder C:\Scripts). These two lines of code create an instance of the Scripting.FileSystemObject, then open the file C:\Scripts\All.js for reading:

At this point all you blokes and sheilas can probably guess what comes next: we next set up a Do Until loop designed to read the file line-by-line:

Inside that loop, we use the ReadLine method to read the first line in the file, assigning that value to a variable named strLine:

We then use the InStr function to see if this line of text contains the string value stored in the variable strTextA:

If we happen to find the target text we then change the value of blnMissingA to False:

Why do we do that? That’s right: because it turns out that our first target string hasn’t gone walkabout after all. Instead, we found the target value in the file All.js.

We then do a similar check for the string value stored in the variable strTextB. Once that’s done we go back to the top of the loop and repeat the process with the next line in the text file. As soon as we’ve examined each and every line in the text file we close that file:

So what comes next? Well, next we check to see if the value of blnMissingA is True. If it is, we then add the value of strTextA to a brand-new variable, a variable named strAdder. That’s what this block of code is for:

Why do we do this? Well, if strTextA can’t be found in All.js that means two things: 1) blnMissingA will still be true; and, 2) we need to add the value of strTextA to the text file. We then do a similar check for blnMissingB; if blnMissingB is true we add the value of strTextB to the variable strAdder:

What does all that mean? Well, if neither of our two target lines of text were found that means that strAdder will be equal to this:

At this point we’re within cooee of wrapping things up. To begin with, we check to see if strAdder is an empty string:

If it is, that can mean only one thing: both our target strings were already in the file All.js after all. If that’s the case, then we’re done. If it’s not the case (that is, if strAdder is not an empty string) then we execute this block of code:

All we’re doing here is reopening the file All.js, this time for appending. We use the Write method to append the value of strAdder to the file, then we close the file.

In our opinion this script is bottler, but you might be wondering if it will work as advertised. Let’s put our answer in terms that everyone can understand: it’s London to a brick that this is going to work. Like we said, the Scripting Guys might look like dags, and we might act like we aren’t worth a zack, but we’re really just larrikins, and our answers are almost always fair dinkum.

But, then again, you already knew that, didn’t you?

And now it’s time for us to do the Harold Holt. Ta, mates; see you all tomorrow.


No Comment.