Question Trouble with Arrays

windymiller

Member
Joined
Feb 10, 2009
Messages
5
Programming Experience
Beginner
What I am trying to do is search through a list of file names looking for files with the same File name
the following code is run from a button on the form
The code seem to work ok up to the Array the line marked with ### is the line that the code stops at run time. Can someone explain where I am going wrong
VB.NET:
Dpath = cboFolder.SelectedItem

Dim filelist As System.Collection.ObjectMode1.ReadOnlyCollection(of String)
Dim csvfile as String
Dim nofiles as Integer
filelist = My.Computer.FileSystem.GetFiles(Dpath, FileIO.SearchOption.SearchAllSubDirectories, filter)
sw = IO.File.AppendText("FileDuplicate.csv")
noFiles = filelist.Count


MsgBox(noFiles) This gives the correct number of files 

For duplicates As Integer = 0 To noFiles 
      For Each foundfile as String in filelist
          Dim fi as FileInfo = New FileInfo(foundfile)

          For duplicatez as Integer = duplicates To noFiles - 2
               If (fi.Name(duplicates)) = (fi.Name(duplicatez - 1)) Then  '####
                   csvfile = fi.Directory & "," & fi.name
                   sw.WriteLine(csvfile)
              End If
           Next
       Next
   Next
       sw.close()
 
Last edited by a moderator:
The error would be helpful, but I imagine it is index out of range. That means you are trying to access a item that doesnt belong in an array. Step through your code. on the first iteration 'duplicates' is zero. you are then setting 'duplicatez' to the same value. Then on the line that you are getting the error you are subtracting one from zero. Thats -1 and that is not a valid index number.

Something that might be a little bit easier to code would be using the 'Contains' method. It does all the searching for you.
 
rcombs4 is right, you should visualize what will happen for each iteration of your code, especially what happens at the loop boundaries.

Before that you should have a clear algorithm figured out, you don't need to write any code for this, just write down in plain text how you intend to compare items, when you know what items to compare you can translate that into code. The laymans (unprecise) algorithm in this case is as follows:
  • compare all the items with all the other items.
Since you don't need to compare both item A with B and B with A (ie previous item) you can modify the algorithm:
  • compare all the items with all subsequent items.
Since you don't intend to compare the last item with itself you can make the algorithm more precise (you need to be accurate when telling a computer what to do):
  • compare all items except last one with all subsequent items.
Now it should hold water, and you can start translating to code.

First some basic language knowledge, arrays in VB is indexed where first item is 0, so last index is array length/count -1. It is also possible to use "files.GetUpperBound(0)" instead of "files.Length -1"

So to loop "all items except last one" you can write a For loop like this:
VB.NET:
For i As Integer = 0 To files.Length - 2
Comparing "all subsequent items" requires a nested For loop that goes from current index (i) + 1 to last item, the code is given:
VB.NET:
For ii As Integer = i + 1 To files.Length - 1
Now comparing each item at files(i) with each item at files(ii) will fulfill the algorithm sketched out. I'll post a complete code sample, including use of two variables currentName and compareName for the indexed files so it is easier to read what is happening in the code:
VB.NET:
Dim dir As New IO.DirectoryInfo("c:\temp")
Dim files() As IO.FileInfo = dir.GetFiles("*.*", IO.SearchOption.AllDirectories)        
For i As Integer = 0 To files.Count - 2
    Dim currentName As String = files(i).Name
    For ii As Integer = i + 1 To files.Count - 1
        Dim compareName As String = files(ii).Name
        If currentName = compareName Then
            'same name
        End If
    Next
Next
I also used DirectoryInfo.Getfiles to get a FileInfo array directly instead of getting a FileInfo for each comparison, which is more convenient here I think.
 
Thankyou JohnH the explaination helped me understand the principals of arrays better.
I copied your code so I can now save any duplicate files to a spreadsheet and decide on the appropriate file to delete.
Thankyou again for your help
WindyMiller
 
Back
Top