Solved

Need vbscript to compare text files

Posted on 2004-09-14
6
2,811 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 1 hour left to enroll

623 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