Solved

Need vbscript to compare text files

Posted on 2004-09-14
6
2,805 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now