A COBOL programming example of how to create and access data files with variable length records. This sample includes the source code. May 24, 2007. Wscript.Echo 'Addresses without current devices: ' & vbCrLf & strNotCurrent. Set objFile3 = objFSO.CreateTextFile('C: Scripts Differences.txt'). 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.
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
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:
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:ScriptsCurrent.txt, which lists the MAC addresses for all our currently-managed devices. That file looks something like this:
We also have a second text file (C:ScriptsAddresses.txt), which lists all the MAC addresses acquired during a network scan:
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:ScriptsCurrent.txt for reading:
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:ScriptsAddresses.txt, also for reading:
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:
Using our sample file, that means strAddress is equal to this:
That brings us to this line of code:
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:
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:
Based on our sample text files, that’s going to result in the following information appearing onscreen:
And then, because SR wanted to save the results to a third file, we then execute this final block of code:
What we’re doing here is first creating a new text file, one named C:ScriptsDifferences.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:
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?
PermalinkGitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up' extensions: xls;xlsx;xlsm;xlsb;xlam |
' |
' TortoiseSVN Diff script for Excel files |
' |
' Copyright (C) 2004-2008 the TortoiseSVN team |
' This file is distributed under the same license as TortoiseSVN |
' |
' Last commit by: |
' $Author: tortoisesvn $ |
' $Date: 2009-10-18 09:25:24 +0200 (So, 18. Okt 2009) $ |
' $Rev: 17472 $ |
' |
' Authors: |
' Michael Joras <michael@joras.net>, 2008 |
' Suraj Barkale, 2006 |
' |
dimobjExcelApp,objArgs,objScript,objBaseDoc,objNewDoc,objWorkSheet,i |
SetobjArgs=WScript.Arguments |
num=objArgs.Count |
ifnum<2then |
MsgBox'Usage: [CScript WScript] compare.vbs base.doc new.doc',vbExclamation,'Invalid arguments' |
WScript.Quit1 |
endif |
sBaseDoc=objArgs(0) |
sNewDoc=objArgs(1) |
SetobjScript=CreateObject('Scripting.FileSystemObject') |
IfobjScript.FileExists(sBaseDoc)=FalseThen |
MsgBox'File '+sBaseDoc+' does not exist. Cannot compare the documents.',vbExclamation,'File not found' |
Wscript.Quit1 |
EndIf |
IfobjScript.FileExists(sNewDoc)=FalseThen |
MsgBox'File '+sNewDoc+' does not exist. Cannot compare the documents.',vbExclamation,'File not found' |
Wscript.Quit1 |
EndIf |
SetobjScript=Nothing |
OnErrorResumeNext |
SetobjExcelApp=Wscript.CreateObject('Excel.Application') |
IfErr.Number<>0Then |
Wscript.Echo'You must have Excel installed to perform this operation.' |
Wscript.Quit1 |
EndIf |
'Open base excel sheet |
objExcelApp.Workbooks.OpensBaseDoc |
'Open new excel sheet |
objExcelApp.Workbooks.OpensNewDoc |
'Show Excel window |
objExcelApp.Visible=True |
'Create a compare side by side view |
objExcelApp.Windows.CompareSideBySideWith(objExcelApp.Windows(2).Caption) |
IfErr.Number<>0Then |
objExcelApp.Application.WindowState=xlMaximized |
objExcelApp.Windows.Arrange(-4128) |
EndIf |
'Mark differences in sNewDoc red |
i=1 |
ForEachobjWorkSheetInobjExcelApp.Workbooks(2).Worksheets |
objworksheet.Cells.FormatConditions.Delete |
objExcelApp.Workbooks(1).Sheets(i).Copy,objExcelApp.Workbooks(2).Sheets(objExcelApp.Workbooks(2).Sheets.Count) |
objExcelApp.Workbooks(2).Sheets(objExcelApp.Workbooks(2).Sheets.Count).Name='Dummy_for_Comparison'&i |
objworksheet.Activate |
'To create a local formula the cell A1 is used |
original_content=objworksheet.Cells(1,1).Formula |
StringsFormula |
'objworksheet.Cells(1,1).Formula = '=INDIRECT('' & objExcelApp.Workbooks(2).Sheets(i).name & ' (2)'& '!'&ADDRESS(ROW(),COLUMN()))' |
objworksheet.Cells(1,1).Formula='=INDIRECT('Dummy_for_Comparison'&i&'!'&ADDRESS(ROW(),COLUMN()))' |
sFormula=objworksheet.Cells(1,1).FormulaLocal |
objworksheet.Cells(1,1).Formula=original_content |
'with the local formula the conditional formatting is used to mark the cells that are different |
constxlCellValue=1 |
constxlNotEqual=4 |
objworksheet.Cells.FormatConditions.AddxlCellValue,xlNotEqual,sFormula |
objworksheet.Cells.FormatConditions(1).Interior.ColorIndex=3 |
i=i+1 |
next |