文件搜索耗时太长

时间:2015-05-20 19:16:28

标签: vb.net

我们需要在我们的一个网络硬盘上腾出一些空间。有问题的目录有526GB的数据,13555个目录中的180914个文件。

所以我写了代码来查找文件的副本。用户指定要查找的文件,应用程序将查找该文件的所有实例。它工作正常,但需要几分钟。我对此并不感到惊讶,但在Windows资源管理器中进行的搜索速度更快。

我认为这是因为我运行我的应用程序然后在Windows资源管理器中执行相同的搜索,搜索同一文件夹中的相同文件。

有一点不同,我只是不知道它有多重要......

因为一些搜索到的文件具有相同的名称(比如OPERATIONS MANUAL.PDF),但是不同,我在比较时会比较大小。所以搜索找到一个文件,我检查文件大小只返回具有相同名称和文件大小的文件。

我在特定搜索中得到5,而资源管理器返回74。

我的应用需要2分钟,资源管理器需要1分钟。

我能做些什么来提高效率吗?我希望我能够近似探索者的表现。

这是代码......

'find all instances of the file
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'first clear the listbox
        LB_Files.Items.Clear()


        'set the lbel text for the list box
        Label1.Text = "Finding all instances of:  " & TB_File.Text & " in directory:  " & TB_Drive.Text & " and all sub-driectories... PLEASE STAND BY"
        'make sure the label updates
        Me.Refresh()

        'run through and find each file and add it to the listbox
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(TB_Drive.Text, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, TB_File.Text)

            'check if the file is a shortcut and if it is do nothing
            If foundFile.Contains(".lnk") Then

            Else

                Dim infoReader As System.IO.FileInfo
                infoReader = My.Computer.FileSystem.GetFileInfo(foundFile)
                newfilesize = infoReader.Length
                'MsgBox("The old file size is: " & oldfilesize & ",  and the new file size is:  " & newfilesize)
                'for each file that is not a shortcut, add it to the list box

                'check if the file sizes match, don't want to add a file if it is not the same file
                If newfilesize = oldfilesize Then

                    LB_Files.Items.Add(foundFile)
                    'update the file count
                    count = count + 1
                    newfilesize = 0

                End If
                'reset the newfilesize
                newfilesize = 0
            End If


        Next
        'once we find all the files, change the lable to say how many we have.
        Label1.Text = "Found " & count.ToString & " instances of " & TB_File.Text & "."

    End Sub

1 个答案:

答案 0 :(得分:0)

是Windows服务器吗?

如果是这样,那么我就不会厌烦迭代搜索了,并且会了解有关文件系统的更多信息。通过搜索索引,您可以更快地找到文件。

查看Fast Search On FAT32/NTFS File System

从接受的答案中,您将找到一个索引驱动器的sourceforge项目 - 将此部分转换为vb并预先搜索文件的超快速方式。或者,您可以使用众多免费工具中的一种来完成此操作。

相关问题