Solved

Need vbscript to compare text files

Posted on 2004-09-14
6
2,809 Views
Last Modified: 2008-05-07
This question is worth  500 Points  
 


I need a vbscript to compare a text file with a list of serial numbers as follows:

Textfile1
12345
23456
23457

and

Textfile2
12345
23456
etc...

If they are equal then exit
if they are Not equal then i will send and e-mail ( i have the e-mail script)

One list will be the constant, and the other will warn of changes.
0
Comment
Question by:chris_v
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:Michael_D
ID: 12058770
Do you need to know what is the difference?
Do you need compare binary or as text ?
If it is as text, case sensitive or not,
Is trailing extra spaces are ok?
0
 
LVL 2

Author Comment

by:chris_v
ID: 12058822
If they are different I'd like to know what is different.
Compare as text.
not case sensitive
trailing spaces are ok on same line

Thx
0
 
LVL 13

Expert Comment

by:Michael_D
ID: 12058884
What the aprox. size ?
How do you want to see differences? line numbers? or copy of lines?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:chris_v
ID: 12058933
I am tracking hard drive serial numbers from raid servers.  There are only 5 -14 drives on each server.  Serial numbers are gathered using a utility like amcli.exe.  The txt report contains more that just serial numbers.  I will need to filter "Serial Number = XXXXXXXXXXXXX" and compare the numbers to a known list of serial numbers.  If a hard drive is removed or changed the script will report a difference between txt files and send an e-mail to admins.
0
 
LVL 13

Accepted Solution

by:
Michael_D earned 500 total points
ID: 12059602
Here you go...
=========================

Dim fso, f1,f2, file1, file2
Dim i,j, s, report
Dim found
Dim Diff1 , Diff2, report      
Dim d1,d2

File1="C:\text1.txt"
File2="C:\text2.txt"

set d1 =createObject("Scripting.Dictionary")
set d2 =createObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.OpenTextFile(file1, 1,0)
Set f2 = fso.OpenTextFile(file2, 1,0)


Do While f1.AtEndOfStream = False
      s = UCase(Trim(f1.ReadLine))

      if s <>"" then
            d1.Add d1.count,s
      end if
loop


Do While f2.AtEndOfStream = False
      s = UCase(Trim(f2.ReadLine))
      if s <>"" then
            d2.Add d2.count,s
      end if
loop

Diff1=""
Diff2=""
found = false
For i = 0 To d1.Count -1
      found = false
      For j = 0 to d2.Count-1
      
            if d1(i)= d2(j) then
                  found=true
                  exit for
            end if
      next
      if not found then
            diff1= diff1 + d1(i) + " "
      end if
next
found = false
For i = 0 To d2.Count -1
      found = false
      For j = 0 to d1.Count-1
      
            if d2(i)= d1(j) then
                  found=true
                  exit for
            end if
      next
      if not found then
            diff2= diff2 + d2(i) + " "
      end if
next

Report=""
if diff1<>"" then
      Report = "Found only in " & file1 & "  " & diff1
end if

if diff2<>"" then
      Report = Report & Chr(13) & chr(10) & "Found only in " & file2 & "  " & diff2
end if



if report="" then
      msgbox "No difference"
else
      msgbox report
end if

===================
I hope it helps.
0
 
LVL 2

Author Comment

by:chris_v
ID: 12059946
Just what I needed...  Thanks.

Note:  report was defined twice....
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question