0

サブディレクトリ内のすべてのファイルを取得するには、この関数にファイルの再帰性を実装する必要があります。

関数の効率を維持したい (フォルダー内で大きなループを使用しないようにするため)

どうすればそれができますか?

編集:また、再帰性を有効にするかどうかを選択できるように、ブール引数を配置する必要があります。

' For Each file In Get_Files("C:\Windows", {".dll", ".ini"}) : MsgBox(file.Name) : Next

Private Function Get_Files(ByVal Path As String, ParamArray exts() As String) As List(Of IO.FileInfo)
    Return New IO.DirectoryInfo(Path).GetFiles.Where(Function(o) exts.Contains(o.Extension)).ToList
End Function

アップデート

文字列のリストではなく IO.FileInfo のリストを返す元のコードの効率を dotINSolution の変更で保持する必要があります。

Private Function Get_Files(ByVal Path As String, ByVal Recursive As Boolean, ParamArray exts() As String) As List(Of String) ' As List(Of IO.FileInfo)
    Select Case Recursive
        Case True : Return IO.Directory.GetFiles(Path, "*.*", IO.SearchOption.AllDirectories).Where(Function(o) exts.Contains(IO.Path.GetExtension(o))).ToList
        Case False : Return IO.Directory.GetFiles(Path, "*.*", IO.SearchOption.TopDirectoryOnly).Where(Function(o) exts.Contains(IO.Path.GetExtension(o))).ToList
    End Select
End Function

これは、大きな変更を行ったり、結果を複数回変換したりすることなく可能ですか?

4

3 に答える 3

4

これでうまくいくはずです:)

Private Function Get_Files(ByVal Path As String, ParamArray exts() As String) As List(Of IO.FileInfo)
    Return Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories).Where(Function(o) exts.Contains(Path.GetExtension(o))).ToList
End Function

編集、オプションの再帰性あり:

Private Function Get_Files(ByVal Path As String, Byval searchOption As System.IO.SearchOption, ParamArray exts() As String) As List(Of IO.FileInfo)
    Return Directory.GetFiles(Path, "*.*", searchOption).Where(Function(o) exts.Contains(Path.GetExtension(o))).ToList
End Function

トップ ディレクトリのみを検索するには:

For Each file In Get_Files("C:\Windows", SearchOption.TopDirectoryOnly, {".dll", ".ini"}) : MsgBox(file.Name) : Next

再帰検索を行うには:

For Each file In Get_Files("C:\Windows", SearchOption.AllDirectories, {".dll", ".ini"}) : MsgBox(file.Name) : Next
于 2013-06-09T09:11:30.093 に答える
0

最後に、@Andrew Morton と @dotINSolution の変更のおかげで、関数の使用を改善するために 2 つのオーバーロードを追加した汎用関数です。

#Region " Get Files "

    ' [ Get Files Function ]
    '
    ' Examples :
    '
    ' For Each file In Get_Files("C:\Windows", False) : MsgBox(file.Name) : Next
    '
    ' For Each file In Get_Files("C:\Windows", True, "dll")   : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", True, ".dll")  : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", True, "*.dll") : MsgBox(file.Name) : Next
    '
    ' For Each file In Get_Files("C:\Windows", False, {"dll", "ini"})     : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", False, {".dll", ".ini"})   : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", False, {"*.dll", "*.ini"}) : MsgBox(file.Name) : Next

    ' Get Files {directory} {recursive}
    Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean) As List(Of IO.FileInfo)
        Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
        Return IO.Directory.GetFiles(directory, "*", searchOpt).Select(Function(p) New IO.FileInfo(p)).ToList
    End Function

    ' Get Files {directory} {recursive} {ext}
    Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean, ext As String) As List(Of IO.FileInfo)

        If ext.StartsWith("*") Then
            ext = ext.Substring(1, ext.Length - 1)
        ElseIf Not ext = "*" AndAlso Not ext.StartsWith(".") Then
            ext = ("." & ext)
        ElseIf ext = "*" Then
            ext = Nothing
        End If

        Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
        Return IO.Directory.GetFiles(directory, "*" & ext, searchOpt).Select(Function(p) New IO.FileInfo(p)).ToList

    End Function

    ' Get Files {directory} {recursive} {exts()}
    Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean, ParamArray exts() As String) As List(Of IO.FileInfo)

        Dim FileExtensions(exts.Count) As String

        Dim ExtCount As Int64 = 0

        For Each ext In exts
            If ext.StartsWith("*") Then
                FileExtensions(ExtCount) = ext.Substring(1, ext.Length - 1)
            ElseIf Not ext = "*" AndAlso Not ext.StartsWith(".") Then
                FileExtensions(ExtCount) = ("." & ext)
            ElseIf Not ext = "*" AndAlso ext.StartsWith(".") Then
                FileExtensions(ExtCount) = ext
            ElseIf ext = "*" Then
                FileExtensions(ExtCount) = Nothing
            End If
            ExtCount += 1
        Next

        Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
        Dim filenameExtComparer As New FilenameExtensionComparer
        Return IO.Directory.GetFiles(directory, "*.*", searchOpt).Where(Function(o) FileExtensions.Contains(IO.Path.GetExtension(o), filenameExtComparer)).Select(Function(p) New IO.FileInfo(p)).ToList

    End Function

    ' FilenameExtensionComparer
    Public Class FilenameExtensionComparer : Implements IEqualityComparer(Of String)

        Public Function Equals1(s As String, t As String) As Boolean Implements IEqualityComparer(Of String).Equals
            Return String.Compare(s, t, StringComparison.OrdinalIgnoreCase) = 0
        End Function

        Public Function GetHashCode1(s As String) As Integer Implements IEqualityComparer(Of String).GetHashCode
            Return s.GetHashCode()
        End Function

    End Class

#End Region
于 2013-06-11T09:41:39.957 に答える