转自CSDN
'
'Invoke : funcSearchFile("e:\flash", "*bb*cc*.*")
' 参数说明:dir 路径名称,fileName 待查找类似文件名称
Private Function funcSearchFile( ByVal dir As String , ByVal fileName As String ) As Boolean
If Not dir .LastIndexOf( " \ " ) = dir .Length - 1 Then
dir = dir & " \ "
End If
Dim files() As String
Try
files = System.IO.Directory.GetFiles( dir )
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, " 出错信息 " )
Return False
End Try
For Each s As String In files
If funcCompareFileName(fileName, s, True ) Then
Console.WriteLine(s)
End If
Next
Dim folders() As String
folders = System.IO.Directory.GetDirectories( dir )
For Each s2 As String In folders
funcSearchFile(s2, fileName)
Next
End Function
Private Function funcCompareFileName( ByVal searchFile As String , ByVal dirFile As String , ByVal First As Boolean ) As Boolean
Dim searchFile2 As String = searchFile ' 备份
Dim dirFile2 As String ' 备份
dirFile2 = IIf (dirFile.IndexOf( " \ " ) >= 0 , dirFile.Substring(dirFile.LastIndexOf( " \ " ) + 1 ), dirFile)
' '全体文件
If searchFile2 = " *.* " Then
Return True
End If
Dim kk As Integer
Dim fileName As String
Dim col As New Collection
If searchFile2.IndexOf( " * " ) > 0 Then
fileName = searchFile2
If First Then ' 是否是第一次比较该文件
If fileName.Substring( 0 , fileName.IndexOf( " * " )) <> dirFile2.Substring( 0 , fileName.IndexOf( " * " )) Then
Return False
End If
End If
While fileName.IndexOf( " * " ) >= 0
kk = dirFile.IndexOf(fileName.Substring( 0 , fileName.IndexOf( " * " )))
If kk >= 0 Then
If kk - searchFile.IndexOf( " * " ) >= - 1 Then
fileName = fileName.Substring(fileName.IndexOf( " * " ) + 1 )
col.Add(kk)
Else
Return False
End If
Else
Return False
End If
End While
' '判断所有的匹配是否按升序排列
Dim i As Integer
For i = 1 To col.Count
If i > 1 Then
If col(i) < col(i - 1 ) Then
Return False
End If
End If
Next
Return True
ElseIf searchFile2.IndexOf( " * " ) = 0 Then
Return funcCompareFileName(searchFile2.Substring( 1 , searchFile2.Length - 1 ), dirFile2, False )
Else ' 完整匹配文件名称
If searchFile2 = dirFile2 Then
Return True
Else
Return False
End If
End If
End Function
' 参数说明:dir 路径名称,fileName 待查找类似文件名称
Private Function funcSearchFile( ByVal dir As String , ByVal fileName As String ) As Boolean
If Not dir .LastIndexOf( " \ " ) = dir .Length - 1 Then
dir = dir & " \ "
End If
Dim files() As String
Try
files = System.IO.Directory.GetFiles( dir )
Catch ex As Exception
MessageBox.Show(ex.Message.ToString, " 出错信息 " )
Return False
End Try
For Each s As String In files
If funcCompareFileName(fileName, s, True ) Then
Console.WriteLine(s)
End If
Next
Dim folders() As String
folders = System.IO.Directory.GetDirectories( dir )
For Each s2 As String In folders
funcSearchFile(s2, fileName)
Next
End Function
Private Function funcCompareFileName( ByVal searchFile As String , ByVal dirFile As String , ByVal First As Boolean ) As Boolean
Dim searchFile2 As String = searchFile ' 备份
Dim dirFile2 As String ' 备份
dirFile2 = IIf (dirFile.IndexOf( " \ " ) >= 0 , dirFile.Substring(dirFile.LastIndexOf( " \ " ) + 1 ), dirFile)
' '全体文件
If searchFile2 = " *.* " Then
Return True
End If
Dim kk As Integer
Dim fileName As String
Dim col As New Collection
If searchFile2.IndexOf( " * " ) > 0 Then
fileName = searchFile2
If First Then ' 是否是第一次比较该文件
If fileName.Substring( 0 , fileName.IndexOf( " * " )) <> dirFile2.Substring( 0 , fileName.IndexOf( " * " )) Then
Return False
End If
End If
While fileName.IndexOf( " * " ) >= 0
kk = dirFile.IndexOf(fileName.Substring( 0 , fileName.IndexOf( " * " )))
If kk >= 0 Then
If kk - searchFile.IndexOf( " * " ) >= - 1 Then
fileName = fileName.Substring(fileName.IndexOf( " * " ) + 1 )
col.Add(kk)
Else
Return False
End If
Else
Return False
End If
End While
' '判断所有的匹配是否按升序排列
Dim i As Integer
For i = 1 To col.Count
If i > 1 Then
If col(i) < col(i - 1 ) Then
Return False
End If
End If
Next
Return True
ElseIf searchFile2.IndexOf( " * " ) = 0 Then
Return funcCompareFileName(searchFile2.Substring( 1 , searchFile2.Length - 1 ), dirFile2, False )
Else ' 完整匹配文件名称
If searchFile2 = dirFile2 Then
Return True
Else
Return False
End If
End If
End Function