Solved

Need vbscript to compare text files

Posted on 2004-09-14
6
2,810 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

752 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