如何在 VB 中查找具有特殊字符的文件(就像 MySQL 一样)?

How to find files with special characters in VB (like MySQL does)?

如果我搜索例如MySql 中的“dëër”,它将 return 所有结果,如 dëer、deer、Déér 等。

我需要执行一个 file.delete(deer) ,所有可能性都将被删除。

这可以在 VB 完成吗?

假设您不关心文件的扩展名,您可以这样做:

定义一些静态字段来优化规范化:

Private Shared ReadOnly Encoder As Encoding = Encoding.GetEncoding("ISO-8859-8")
Private Shared ReadOnly Decoder As Encoding = Encoding.UTF8

定义一个函数来简化字符串(删除重音符号):

Private Shared Function Simplify(text As String) As String
    If (text Is Nothing) Then Return Nothing
    text = text.Normalize()
    Dim binary As Byte() = Encoder.GetBytes(text)
    Return Decoder.GetString(binary)
End Function

定义两个匹配文件名的重载函数:

Private Shared Function GetMatchingFilePath(folderPath As String, ParamArray includeFilter As String()) As IEnumerable(Of String)
    Dim myIncludeFilter As IEnumerable(Of String) = includeFilter
    Return GetMatchingFilePath(folderPath, myIncludeFilter)
End Function

Private Shared Iterator Function GetMatchingFilePath(folderPath As String, includeFilters As IEnumerable(Of String)) As IEnumerable(Of String)
    If (includeFilters Is Nothing) Then Return
    includeFilters = includeFilters.Where(Function(e) e IsNot Nothing).Select(Function(e) Simplify(e)).Distinct()
    For Each filePath As String In Directory.EnumerateFiles(folderPath)
        Dim fileName As String = Simplify(Path.GetFileNameWithoutExtension(filePath))
        For Each includeFilter In includeFilters
            If StringComparer.OrdinalIgnoreCase.Equals(fileName, includeFilter) Then
                Yield filePath
            End If
        Next
    Next
End Function

并像这样使用它:

For Each filePath As String In GetMatchingFilePath("C:\Temp", "dëër")
    File.Delete(filePath)
Next