Tidying and deduplicating files listed in a DataGridView I have the following code that searches through folder directories in a DataGridView table, and puts all files of the wanted format into a list, it also gathers a list of their last modified date for later use in the application.

The code works, but it is sore on the eyes. I want to tidy up the following loops to improve efficiency - what I mean is that I have a For loop within a For loop that creates the list of filenames, then I have two separate Do Until loops that search through the list from start to finish to pick out file names that need adjustment.

I would be very interested to learn a better way of achieving the same result, as my knowledge of efficiency in coding is quite elementary. Basically, can this be done in one or two loops, as the idea of looping through the Lists twice seems inefficient?

Public Class
Private Sub btnDirectory_Click(sender As Object, e As EventArgs) Handles btnDirectory.Click

Dim FileNames As New List(Of String)
Dim FileDates As New List(Of Date)

Dim DocNo As String
Dim rowCheck As String
Dim ProjectNo As String = "1111"
Dim FileNameCheck As String
Dim str As String

Dim k As Integer = 0 
Dim i As Integer
Dim j As Integer

'The first loop grabs all files of the wanted format from a datagridview table containing all directories to be checked

For Each rw In Background.Table1.Rows

    rowCheck = Background.Table1(0, k).Value
    If Not String.IsNullOrEmpty(rowCheck) Then

    For Each file As String In My.Computer.FileSystem.GetFiles(Background.Table1(0, k).Value)
        CorrectType = False
        FileNameCheck = IO.Path.GetFileNameWithoutExtension(file)
        If FileNameCheck.Contains(ProjectNo) AndAlso FileNameCheck.Contains("-") AndAlso Not String.IsNullOrEmpty(FileNameCheck) AndAlso FileNameCheck.Contains(" ") Then
            DocNo = FileNameCheck.Substring(0, FileNameCheck.IndexOf(" "))
            If FileNameCheck.Substring(0, FileNameCheck.IndexOf("-")) = ProjectNo AndAlso CountLetters(DocNo) = 3 Then
                FileNames.Add(FileNameCheck)
                FileDates.Add(IO.File.GetLastWriteTime(file))                                
            End If
        End If


    Next
    End If

    k += 1

Next

'The next loop tidies up the file formats that contain a "-00-" in their names

 j = FileNames.Count
 i = 0
 Do
     str = FileNames(i)
     If str.Contains("-00-") Then                           
         FileNames(i) = RemoveChar(str, "-00-") ' RemoveChar is a function that replaces "-00-" with a "-"
     End If
     i += 1
 Loop Until i = j

 i = 0
 j = FileNames.Count


 'Finally, this loop checks that no two files have the exact same name, and gets rid of one of them if that is the case

 Do

     Dim st1 As String = FileNames(j - 1)
     Dim st2 As String = FileNames(j - 2)

     If st1 = st2 Then          
         FileNames.RemoveAt(j - 1)
         FileDates.RemoveAt(j - 1)
     End If
     j -=  1
 Loop Until j = 1


 End Sub


End Class
I found some code that you could tidy up a bit right here


    j = FileNames.Count
    i = 0
    Do
        str = FileNames(i)
        If str.Contains("-00-") Then
            FileNames(i) = RemoveChar(str, "-00-") ' RemoveChar is a function that replaces "-00-" with a "-"
        End If
        i += 1
    Loop Until i = j



that seems like a lot of work for such a simple task. 

use a for loop that goes from the base index to the FileNames.Count value.

Then use a Replace instead of creating a function that does a replace.  here is some code that does the same thing

keep in mind that a replace will only replace a string that it can find, so you don't have to check for the substring in your string before you call the Replace function on the string.

For i As Integer = 0 To FileNames.Count
    FileNames(i).Replace("-00-", "-")
Next