How Can I Compare the Contents of Two Text Files?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have two separate text files. One file contains a list of MAC addresses taken from a network scan; the other contains a list of MAC addresses for our currently-managed devices. How can I compare these two files, and output a list of addresses that have no corresponding device to a third text file?

— SR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SR. You know, last night the Scripting Guy who writes this column was watching TV when – hey, get out of here! Shoo! Shoo! Go on, get out!

Sorry; the goats wandered through the office again. We really need to put a door on this building, don’t we?

Anyway, last night the Scripting Guy who writes this column was watching TV when a segment about the NBA draft came on. As it turns out, in the NBA draft lottery the Portland Trailblazers drew the first pick in the draft and the Seattle Supersonics drew the second pick. No big deal, right?

Wrong, at least according to commentator Stephen A. Smith. Smith was absolutely outraged by this turn of events, labeling it a black day for the NBA. “You’ve got the two most exciting prospects to come around in years headed for the NBA,” he fumed. “And now both of them are going to end up in the Pacific Northwest, where no one will ever see them!”

Well, needless to say, the Scripting Guy who writes this column was outraged by Stephen A. Smith. What, we don’t have civilization up here in the Pacific Northwest? To tell you the truth, his first thought was to pick up the phone, call Stephen A. Smith, and give him what-for. And he would have, too … assuming we actually had telephones in the Pacific Northwest.

Note. In case you’re wondering, Stephen A. Smith hails from Philadelphia. The fact of the matter is that we hicks out here in the Pacific Northwest can’t hold a candle to someone from Philadelphia.

Mainly because we don’t actually have candles in the Pacific Northwest. But someday .…

At any rate, on behalf of the entire Pacific Northwest, the Scripting Guys would like to apologize to everyone who still cares about the NBA. (Yes, both of you.) And we’d like to make it up to everyone. Granted, those of us in the Pacific Northwest might not have fancy, modern-day conveniences like wall-to-wall carpeting and frozen yogurt, but we do have scripts that can compare two text files and report back any differences:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFile1 = objFSO.OpenTextFile(“C:\Scripts\Current.txt”, ForReading)

strCurrentDevices = objFile1.ReadAll objFile1.Close

Set objFile2 = objFSO.OpenTextFile(“C:\Scripts\Addresses.txt”, ForReading)

Do Until objFile2.AtEndOfStream strAddress = objFile2.ReadLine If InStr(strCurrentDevices, strAddress) = 0 Then strNotCurrent = strNotCurrent & strAddress & vbCrLf End If Loop

objFile2.Close

Wscript.Echo “Addresses without current devices: ” & vbCrLf & strNotCurrent

Set objFile3 = objFSO.CreateTextFile(“C:\Scripts\Differences.txt”)

objFile3.WriteLine strNotCurrent objFile3.Close

Note. We apologize for using a stylus to inscribe today’s column into a stone tablet. Unfortunately that’s the best we can do out in the God-forsaken wasteland we call the Pacific Northwest. We hope this doesn’t inconvenience anyone. And it shouldn’t, provided your firewall will allow stone tablets to pass through.

All right, let’s see if we can figure out how this script works. (Needless to say, we have to do that right away, before the sun goes down.) To begin with, we have a text file named C:\Scripts\Current.txt, which lists the MAC addresses for all our currently-managed devices. That file looks something like this:

00:14:A5:4D:AC:01
00:14:A5:4D:AC:03
00:14:A5:4D:AC:04

We also have a second text file (C:\Scripts\Addresses.txt), which lists all the MAC addresses acquired during a network scan:

00:14:A5:4D:AC:01
00:14:A5:4D:AC:02
00:14:A5:4D:AC:03
00:14:A5:4D:AC:04
00:14:A5:4D:AC:05

What we want to do is compare the two files and see if there are devices on the network (identified using the MAC address) that do not appear in our list of currently-managed devices. In other words, we want to generate a list of unmanaged/unknown devices. So, what the heck: let’s go ahead and generate such a list.

Hey, drop that! Drop it, I say!

Sorry; raccoons tried to walk off with our text files again. Dirty, low-down, no-good varmints ….

In the script itself we start out by defining a constant named ForReading; we’ll need this constant to open and read from our two text files. We next create an instance of the Scripting.FileSystemObject, then use this line of code to open the file C:\Scripts\Current.txt for reading:

Set objFile1 = objFSO.OpenTextFile(“C:\Scripts\Current.txt”, ForReading)

From there we use the ReadAll method to read in the entire file, storing the contents in a variable named strCurrentDevices. And then, with no further need for Current.txt, we use the Close method to close the file.

We now have a list of the MAC addresses for all our managed devices. Our next step is to get the list of all MAC addresses found on the network and then check each of those addresses, one-by-one, to see if they show up in the list of managed devices.

To that end, the first thing we do is open the file C:\Scripts\Addresses.txt, also for reading:

Set objFile2 = objFSO.OpenTextFile(“C:\Scripts\Addresses.txt”, ForReading)

Because each MAC address is listed on a separate line in the file, we set up a Do Until loop that will run until we reach the end of the file. (Or, to be a little more technical, until the AtEndOfStream property is True.) Inside that loop, we use the ReadLine method to read the first line in the file (that is, the first MAC address) and store that value in a variable named strAddress:

strAddress = objFile2.ReadLine

Using our sample file, that means strAddress is equal to this:

00:14:A5:4D:AC:01

That brings us to this line of code:

If InStr(strCurrentDevices, strAddress) = 0 Then

What we’re doing here is using the InStr function to determine whether or not the first MAC address (00:14:A5:4D:AC:01) can be found anywhere in the list of managed devices. As you can see, InStr takes two parameters: the string we want to search (strCurrentDevices) and the string we’re searching for (strAddress). InStr returns the character position at which the search string is found; if the search string can’t be found then InStr returns 0.

In other words, if InStr returns 0 that means that the address is not in the list of managed devices. Therefore, we add that address to a running tally of unmanaged devices:

strNotCurrent = strNotCurrent & strAddress & vbCrLf

That bit of code is fairly straightforward: all we’re doing is assigning a value to a variable named strNotCurrent. And what value are we assigning this variable? We’re assigning it the existing value of strNotCurrent plus the MAC address we just failed to find (strAddress) plus a carriage return-linefeed (vbCrLf). And then we loop around and repeat this process with the next MAC address.

When we’ve finished checking all the MAC addresses we close the file, then use this line of code to echo back the results:

Wscript.Echo “Addresses without current devices: ” & vbCrLf & strNotCurrent

Based on our sample text files, that’s going to result in the following information appearing onscreen:

Addresses without current devices:
00:14:A5:4D:AC:02
00:14:A5:4D:AC:05

And then, because SR wanted to save the results to a third file, we then execute this final block of code:

Set objFile3 = objFSO.CreateTextFile(“C:\Scripts\Differences.txt”)

objFile3.WriteLine strNotCurrent objFile3.Close

What we’re doing here is first creating a new text file, one named C:\Scripts\Differences.txt. Once the file has been created we then call the WriteLine method to write the value of strNotCurrent to the new file. After that we close the file and call it good.

Of course, you could just as easily go the other direction and search for managed devices that don’t appear in the list of addresses. To accomplish that feat all you have to do is first read in the list of addresses and then search that list for each managed device. (Exactly the opposite of the task we just finished.) That code would look something like this:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFile1 = objFSO.OpenTextFile(“C:\Scripts\Addresses.txt”, ForReading)

strAddresses = objFile1.ReadAll objFile1.Close

Set objFile2 = objFSO.OpenTextFile(“C:\Scripts\Current.txt”, ForReading)

Do Until objFile2.AtEndOfStream strCurrent = objFile2.ReadLine If InStr(strAddresses, strCurrent) = 0 Then strNoAddress = strNoAddress & strCurrent & vbCrLf End If Loop

objFile2.Close

Wscript.Echo “Devices with no address: ” & vbCrLf & strNoAddress

Set objFile3 = objFSO.CreateTextFile(“C:\Scripts\Differences.txt”)

objFile3.WriteLine strNoAddress objFile3.Close

Needless to say, you could also combine these two scripts and end up looking for items that appear in File A but not in File B as well as items that appear in File B but not in File A. But we’ll let you take care of that yourself.

We hope that answers your question, SR. In the meantime, we Scripting Guys are getting ready to watch Stephen A. Smith’s TV show and – oh, wait a minute. Sorry, we forgot: Stephen A. Smith’s TV show was cancelled. What a shame, huh?

0 comments

Discussion is closed.

Feedback usabilla icon