优化 For 循环和 Do Until 循环的最佳方法

Best way to optimise For Loops and Do Until Loops

我有以下代码在 DataGridView table 中搜索文件夹目录,并将所有所需格式的文件放入列表中,它还收集了它们的最后修改日期列表以供以后在应用程序。

代码有效,但眼睛疼。我想整理以下循环以提高效率——我的意思是我在创建文件名列表的 For 循环中有一个 For 循环,然后我有两个单独的 Do Until 循环从头到尾搜索列表挑出需要调整的文件名。

我非常有兴趣学习一种更好的方法来实现相同的结果,因为我对编码效率的了解非常初级。基本上,这可以在一个或两个循环中完成吗,因为循环遍历列表两次的想法似乎效率低下?

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

    Dim CorrectType As Boolean = False

    '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
                    CorrectType = True                                   
                End If
            End If
            If CorrectType = True Then
                FileNames.Add(FileNameCheck)
                FileDates.Add(IO.File.GetLastWriteTime(file))
            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

代码肯定很难看。

For Each rw循环不使用rw。您可以将其替换为循环,例如:

For k = 1 to Background.Table1.Rows.Count
    ' Do things here
Next k

您分配 rowCheck 并使用它一次,但您错过了在 For Each file 行中重用它的机会。

在你有 CorrectType = True 的地方,你可以轻松地放置相应的代码。

        If FileNameCheck.Substring(0, FileNameCheck.IndexOf("-")) = ProjectNo AndAlso CountLetters(DocNo) = 3 Then
            CorrectType = True                                   
        End If
    End If
    If CorrectType = True Then
        FileNames.Add(FileNameCheck)
        FileDates.Add(IO.File.GetLastWriteTime(file))
    End If

变为:

        If FileNameCheck.Substring(0, FileNameCheck.IndexOf("-")) = ProjectNo AndAlso CountLetters(DocNo) = 3 Then
            FileNames.Add(FileNameCheck)
            FileDates.Add(IO.File.GetLastWriteTime(file))
        End If

我必须承认,接下来的两个循环让我的眼睛流血了(比喻,而不是字面意思)。

 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

变成

 for i = 1 to FileNames.Count
     str = FileNames(i)
     If str.Contains("-00-") Then                           
         FileNames(i) = RemoveChar(str, "-00-") ' RemoveChar is a function that replaces "-00-" with a "-"
     End If
 Next I

    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

变成

 '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

 For j = FileNames.Count - 1 to 1 Step -1 ' Check my counting here - stop at 1, 2 or 0?

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

     If st1 = st2 Then          
         FileNames.RemoveAt(j)
         FileDates.RemoveAt(j)
     End If
 Next j