VB Getfiles 排除
VB Getfiles exclude
我想在特定文件夹中搜索不包含给定模式的 png 文件,并将它们添加到列表框中。
所以我想找到名称中没有 _norm 或 _spec 的 png。 Paterrn 可以是大写或小写。
文件夹中的文件:
- a.png
- a_norm.png
- a_spec.png
- a_Spec.png
- a_Norm.png
希望列表框中的结果:
a.png
你不能使用 GetFiles
来做到这一点,它只能根据匹配掩码进行过滤,而不是不匹配它。您需要获取所有文件,然后通过适当的 String
比较丢弃不需要的文件。可能看起来像这样:
Dim folderPath = "folder path here"
Dim filePaths = Directory.GetFiles("*.png").
Where(Function(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Return Not fileName.EndsWith("_norm", StringComparison.InvariantCultureIgnoreCase) AndAlso
Not fileName.EndsWith("_spec", StringComparison.InvariantCultureIgnoreCase)
End Function).
ToArray()
这假定这些是您正在谈论的后缀。如果它们可以在名称中的任何位置,那么您可以相应地进行调整。
编辑:LINQ 代码可以像这样更简洁一点:
Dim folderPath = "folder path here"
Dim filePaths = Directory.GetFiles("*.png").
Where(Function(filePath) {"_norm", "_spec"}.All(Function(suffix) Not Path.GetFileNameWithoutExtension(filePath).EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase))).
ToArray()
不过,那些不太熟悉 LINQ 的人可能不太清楚。
使用 GetFiles
时,没有排除文件的选项,只有一种包含文件的模式,因此必须首先获取所有符合该模式的文件,然后排除不需要的文件。
以下方法排除包含 exclusions
中提供的字符串之一的文件并忽略大小写:
Function ExcludeFiles(files As String(), exclusions As String()) As String()
Return files.Where(Function(s) Not exclusions.
Any(Function(e) s.IndexOf(e, StringComparison.CurrentCultureIgnoreCase) > 0)).
ToArray()
End Function
用法:
Sub DoSomething()
Dim path As String = "C:\WhatEver"
Dim allPngFiles As String() = System.IO.Directory.GetFiles(path, "*.png")
Dim filtered As String() = ExcludeFiles(allPngFiles, {"_norm", "_spec"})
'Do something with the filtered files...
End Sub
我想在特定文件夹中搜索不包含给定模式的 png 文件,并将它们添加到列表框中。 所以我想找到名称中没有 _norm 或 _spec 的 png。 Paterrn 可以是大写或小写。
文件夹中的文件:
- a.png
- a_norm.png
- a_spec.png
- a_Spec.png
- a_Norm.png
希望列表框中的结果:
a.png
你不能使用 GetFiles
来做到这一点,它只能根据匹配掩码进行过滤,而不是不匹配它。您需要获取所有文件,然后通过适当的 String
比较丢弃不需要的文件。可能看起来像这样:
Dim folderPath = "folder path here"
Dim filePaths = Directory.GetFiles("*.png").
Where(Function(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Return Not fileName.EndsWith("_norm", StringComparison.InvariantCultureIgnoreCase) AndAlso
Not fileName.EndsWith("_spec", StringComparison.InvariantCultureIgnoreCase)
End Function).
ToArray()
这假定这些是您正在谈论的后缀。如果它们可以在名称中的任何位置,那么您可以相应地进行调整。
编辑:LINQ 代码可以像这样更简洁一点:
Dim folderPath = "folder path here"
Dim filePaths = Directory.GetFiles("*.png").
Where(Function(filePath) {"_norm", "_spec"}.All(Function(suffix) Not Path.GetFileNameWithoutExtension(filePath).EndsWith(suffix, StringComparison.InvariantCultureIgnoreCase))).
ToArray()
不过,那些不太熟悉 LINQ 的人可能不太清楚。
使用 GetFiles
时,没有排除文件的选项,只有一种包含文件的模式,因此必须首先获取所有符合该模式的文件,然后排除不需要的文件。
以下方法排除包含 exclusions
中提供的字符串之一的文件并忽略大小写:
Function ExcludeFiles(files As String(), exclusions As String()) As String()
Return files.Where(Function(s) Not exclusions.
Any(Function(e) s.IndexOf(e, StringComparison.CurrentCultureIgnoreCase) > 0)).
ToArray()
End Function
用法:
Sub DoSomething()
Dim path As String = "C:\WhatEver"
Dim allPngFiles As String() = System.IO.Directory.GetFiles(path, "*.png")
Dim filtered As String() = ExcludeFiles(allPngFiles, {"_norm", "_spec"})
'Do something with the filtered files...
End Sub