Looping through files to compare

J

Justin Fancy

Hi Everyone,

I'm lookin for a very confusing loop (to me), to compare two files.
Here it is. I have two arrays with paths stored in both. example:
/en/aviation/you.htm.

I need to search array two for all the paths in array 1. If it finds
it, send it to array 3.

Here's my code to send it to the array.

Dim sr As IO.StreamReader = IO.File.OpenText(File1)

'Loop through Directory Listing file to load filepaths into an
array
While sr.Peek <> -1

'Count the number of lines (for the arrays), and read in
the lines
counter += 1
lineFile1 = sr.ReadLine

'Re-Dimension the array to hold "counter" number of
elements
ReDim InternetDirectoryArray(counter)

'String Manipulation (Replace some unneeded text)
MyString = lineFile1

Dim sb As New System.Text.StringBuilder(MyString)
sb.Replace("C:", "")
sb.Replace("x:", "")
sb.Replace("\", "/")
sb.Replace("wwwroot", "")
MyString = sb.ToString()

InternetDirectoryArray(counter) = MyString

End While
'Close the first file
sr.Close()


'Open the second file
sr = IO.File.OpenText(File2)
i = 0
i2 = 0
counter2 = 0

Do While sr.Peek <> -1

counter2 += 1
lineFile2 = sr.ReadLine
MyString2 = lineFile2
Dim sb2 As New System.Text.StringBuilder(MyString2)
sb2.Replace("C:", "")
sb2.Replace("x:", "")
sb2.Replace("\", "/")
sb2.Replace("wwwroot", "")
MyString2 = sb2.ToString()
ReDim Preserve IISLogArray(counter2)
IISLogArray(counter2) = MyString2

Loop

--------------------------------------------------------------------

I've tried for hours on how to get the logic down, but its confusing me
too much. Maybe someone know's what to do.

Thanks for your help.

Justin
 
J

Justin Fancy

One more thing, Here's the code i've tried to tamper with, and it looks
right? Just not executing the way I want it to?

For i = 0 To counter
If MyString2 <> InternetDirectoryArray(i) Then
MsgBox("This is not working")
End If
Next
'lstDisplay.Items.Add(ZeroHits(i2))

This code is inside the second Do While loop, comparing it to the 1st
array. I've tested it by putting msgbox's in between the if statement,
but it doesn't work. How do i know that? Well, I put two of the same
text files i'm reading in the same. Each line in each file are
identical. So, if the code was executing the way I wanted it to, it
wouldn't display the message box. But it does, repeatidly.

Justin
 
S

Steve Long

Justin,
basically what I'm hearing is that you're going to have to do the following:
Load array1 with paths from a file
Load array2 with paths from a file
for every item in array1, loop through array2 to see if there is a match.

That sounds really slow to me. I think I'd load the items into a dictionary
or collection of some kind to make the searchs faster.
Load file1 into a collection
load file2 into a collection

for every item in collection1,
if collection2.item(item) then
do something with it
end if
next

Just a thought
Steve
 
S

Steve Long

Opps, this code:
if collection2.item(item) then

should have read like this:

if not collection2.item(item) is nothing then
'do something
end if

S
 
J

Justin Fancy

Sorry, I'm not up on using collections at all.

I dont have the code with me now, but I did get the logic down, but I
can't seem to get it working.

I'll message tomorrow morning. Thanks for your help Steve. Much
appreciated.

J
 
H

Hugh Janus

Steve said:
Justin,
basically what I'm hearing is that you're going to have to do the following:
Load array1 with paths from a file
Load array2 with paths from a file
for every item in array1, loop through array2 to see if there is a match.

Load array1 into a hashtable. then load array2 into the same table.
If you get an error 5 it means that the item already exists so you can
add that item to array 3.

Hugh
 
S

Steve Long

Right Hugh, but, the problem with this method is that a exception is
thrown--which is very expensive in terms of processor time. However, the
other method of checking to see if an item exists in a collection should be
faster. Maybe.... ????

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top