0

VB.net VS2012 を使用していますが、フィルターを使用してファイルのリストを取得するのに問題があります。

これが私のコードです:

Public Function SearchAndAddToListWithFilter(ByVal path As String, ByVal Recursive As Boolean, arrayListOfFilters As ArrayList, ByRef listOfFiles As List(Of FileInfo))
    If Not Directory.Exists(path) Then Exit Function

    Dim initDirInfo As New DirectoryInfo(path)

    For Each oFileInfo In initDirInfo.GetFiles
        Application.DoEvents()
        For x = 0 To arrayListOfFilters.Count - 1
            If (oFileInfo.Name Like arrayListOfFilters(x)) Then
                listOfFiles.Add(oFileInfo)
            End If
        Next
    Next

    If Recursive Then
        For Each oDirInfo In initDirInfo.GetDirectories
            SearchAndAddToListWithFilter(oDirInfo.FullName, True, arrayListOfFilters, listOfFiles)
        Next
    End If

End Function

そして、これを使用する方法の例を次に示します。

    Dim stringFilterList As String = "*.mp3, *.docx, *.mp3, *.txt"
    Dim arrayListOfFilenameFilters As New ArrayList(stringFilterList.Split(","))
    Dim stringFolderPath As String = "C:\temp\folder\"
    Dim booleanSearchSubFolders As Boolean = True

    Dim listOfFilesFoundViaSearch As New List(Of FileInfo)
    SearchAndAddToListWithFilter(stringFolderPath, booleanSearchSubFolders, arrayListOfFilenameFilters, listOfFilesFoundViaSearch)

    For x = 0 To listOfFilesFoundViaSearch.Count - 1
        MsgBox(listOfFilesFoundViaSearch(x).FullName)
    Next

何らかの理由で、コードはフィルターのリストの最初の条件を満たすファイルのみをリストに追加します。

このコードを機能させるために助けてもらえますか?

ありがとうございました。

4

3 に答える 3

1

@Steve が提供するソリューションは、タスクを実行する .NET の方法を実際に示しています。ただし、最大の深さおよび/または期間の定義が可能な再帰的なソリューションを使用しました。このトピックを完全にするために、コードを投稿したいと思います。

''' <summary>
''' Search files in directory and subdirectories 
''' </summary>
''' <param name="searchDir">Start Directory</param>
''' <param name="searchPattern">Search Pattern</param>
''' <param name="maxDepth">maximum depth; 0 for unlimited depth</param>
''' <param name="maxDurationMS">maximum duration; 0 for unlimited duration</param>
''' <returns>a list of filenames including the path</returns>
''' <remarks>
''' recursive use of   Sub dirS
''' 
''' wallner-novak@bemessung.at
''' </remarks>
Public Shared Function dirRecursively(searchDir As String, searchPattern As String, _
                                    Optional maxDepth As Integer = 0, _
                                    Optional maxDurationMS As Long = 0) As List(Of String)
    Dim fileList As New List(Of String)
    Dim depth As Integer = 0
    Dim sw As New Stopwatch

    dirS(searchDir, searchPattern, maxDepth, maxDurationMS, fileList, depth, sw)

    Return fileList

End Function

''' <summary>
''' Recursive file search
''' </summary>
''' <param name="searchDir">Start Directory</param>
''' <param name="searchPattern">Search Pattern</param>
''' <param name="maxDepth">maximum depth; 0 for unlimited depth</param>
''' <param name="maxDurationMS">maximum duration; 0 for unlimited duration</param>
''' <param name="fileList">Filelist to append to</param>
''' <param name="depth">current depth</param>
''' <param name="sw">stopwatch</param>
''' <param name="quit">boolean value to quit early (at given depth or duration)</param>
''' <remarks>
''' wallner-novak@bemessung.at
''' </remarks>
Private Shared Sub dirS(searchDir As String, searchPattern As String, _
                            Optional maxDepth As Integer = 0, _
                            Optional maxDurationMS As Long = 0, _
                            Optional ByRef fileList As List(Of String) = Nothing, _
                            Optional ByRef depth As Integer = 0, _
                            Optional ByRef sw As Stopwatch = Nothing, _
                            Optional ByRef quit As Boolean = False)

    If maxDurationMS > 0 Then
        If depth = 0 Then
            sw = New Stopwatch
            sw.Start()
        Else
            If sw.ElapsedMilliseconds > maxDurationMS Then
                quit = True
                Exit Sub
            End If
        End If
    End If

    If maxDepth > 0 Then
        If depth > maxDepth Then
            quit = True
            Exit Sub
        End If
    End If

    ' check if directory exists
    If Not Directory.Exists(searchDir) Then
        Exit Sub
    End If

    ' find files
    For Each myFile As String In Directory.GetFiles(searchDir, searchPattern)
        fileList.Add(myFile)
    Next

    ' recursively scan subdirectories 
    For Each myDir In Directory.GetDirectories(searchDir)
        depth += 1
        dirS(myDir, searchPattern, maxDepth, maxDurationMS, fileList, depth, sw, quit)
        If quit Then Exit For
        depth -= 1
    Next

End Sub
于 2015-01-13T15:19:15.490 に答える
1

関数は値を返します。値を ByRef に渡すことは、それを行う方法ではありません。

次の関数が機能します。

Private Function SearchAndAddToListWithFilter(ByVal path As String, ByVal filters As String(), ByVal searchSubFolders As Boolean) As List(Of IO.FileInfo)
    If Not IO.Directory.Exists(path) Then
        Throw New Exception("Path not found")
    End If

    Dim searchOptions As IO.SearchOption
    If searchSubFolders Then
        searchOptions = IO.SearchOption.AllDirectories
    Else
        searchOptions = IO.SearchOption.TopDirectoryOnly
    End If

    Return filters.SelectMany(Function(filter) New IO.DirectoryInfo(path).GetFiles(filter, searchOptions)).ToList
End Function

この関数を使用するには:

Dim filters As String() = {"*.mp3", "*.docx", "*.bmp", "*.txt"}
Dim path As String = "C:\temp\folder\"

Dim foundFiles As List(Of IO.FileInfo) = SearchAndAddToListWithFilter(path, filters, True)
于 2012-11-26T07:31:28.287 に答える