What Cgraus is describing would look like this
Imports System.IO
Public Class Form1
Protected filename As String
Protected LastDirectory As String
Protected LastFoundDirectory As String
Protected MatchString As New ArrayList
Protected bStop As Boolean
Protected fileObjectList As New ArrayList
Protected StringSegmentList As New ArrayList
Protected Threads As Integer
Protected FoundFiles As Long
Protected Numfiles As Long
Protected DriveInfo As System.IO.DriveInfo()
Protected sFilestring As String
Protected ExtDict As New Dictionary(Of Integer, String)
Protected NameDict As New Dictionary(Of Integer, String)
Protected bListAllFiles As Boolean
Protected bFullnameCompare As Boolean
Protected bExtCompare As Boolean
Protected bNameCompare As Boolean
Protected bNamescan As Boolean
Protected bExtscan As Boolean
Protected bRequiredActions As Byte
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SetupListview()
End Sub
Private Sub cbSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbSearch.Click
'fileObjectList.Clear()
If cbSearch.Text.Contains("Search") Then
StringSegmentList.Clear()
LastDirectory = ""
MatchString.Clear()
SetupListview()
cbSearch.Text = "Stop"
Application.DoEvents()
Parse()
ProcessAllDirectories("g:\")
Else
bStop = True
cbSearch.Text = "Search"
End If
End Sub
Private Sub SetupListview()
lv1.Clear()
With lv1
.View = View.Details
'Add a column with width 20 and left alignment
.Columns.Add("Files", 200, HorizontalAlignment.Center)
.Columns(0).Tag = "String"
.Columns.Add("Pid", 45, HorizontalAlignment.Right)
.Columns(1).Tag = "Numeric"
End With
End Sub
Private Sub ProcessAllDirectories(ByVal strPath As String)
Dim objException As Exception
Try
Dim objDirectoryInfo As New DirectoryInfo(strPath)
ProcessDirectory(objDirectoryInfo)
Catch objException
End Try
cbSearch.Text = "Search"
End Sub
Private Sub ProcessDirectory(ByVal objDirectoryInfo As DirectoryInfo)
'Static ListviewIndex
Static Drive As Char
Threads += 1
If bStop Then GoTo Common_Exit
Try
If objDirectoryInfo.GetFiles().Length > 0 Then
Dim objFiles As FileInfo()
Dim objFileInfo As FileInfo
objFiles = objDirectoryInfo.GetFiles()
If objDirectoryInfo.FullName <> LastDirectory Then
LastDirectory = objDirectoryInfo.FullName
End If
If bListAllFiles Then ListAllFiles(objFileInfo, objFiles)
If bFullnameCompare Then FullnameCompare(objFileInfo, objFiles)
End If
If objDirectoryInfo.GetDirectories().Length > 0 Then
Dim arrobjDirectoryInfo As DirectoryInfo()
arrobjDirectoryInfo = objDirectoryInfo.GetDirectories()
Dim objChildDirectory As DirectoryInfo
For Each objChildDirectory In arrobjDirectoryInfo
If bStop Then GoTo common_exit
slFiles.Text = "Files: " & Numfiles
slFound.Text = "Files Found: " & FoundFiles
slDirectory.Text = "Directory: " & LastDirectory
ProcessDirectory(objChildDirectory)
Next
End If
Catch objException As Exception
Throw (objException)
End Try
Common_Exit:
Application.DoEvents()
End Sub
Private Sub ListAllFiles(ByVal fileinfo As FileInfo, ByVal files As FileInfo())
For Each fileinfo In files
If bStop Then GoTo common_exit
Numfiles += 1
lv1.Items.Add(" " & fileinfo.Name)
FoundFiles += 1
slFiles.Text = "Files Found: " & FoundFiles
Next
Common_exit:
End Sub
Private Sub FullnameCompare(ByVal fileinfo As FileInfo, ByVal files As FileInfo())
For Each fileinfo In files
If bStop Then GoTo common_exit
Numfiles += 1
If fileinfo.Name.ToLower.Contains(sFilestring.ToLower) Then
If LastFoundDirectory <> LastDirectory Then
LastFoundDirectory = fileinfo.DirectoryName
lv1.Items.Add(LastFoundDirectory.Substring(0, 1).ToUpper & LastDirectory.Substring(1)) '.ForeColor = Color.AliceBlue
End If
lv1.Items.Add(" " & fileinfo.Name)
FoundFiles += 1
slFiles.Text = "Files Found: " & FoundFiles
End If
Next
'End If
Common_exit:
End Sub
Private Sub tb1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tb1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
cbSearch_Click(sender, New System.EventArgs)
e.Handled = True
End If
End Sub
Private Sub Parse()
Dim i As Integer
Dim illegalchars = "\/<>:;""'|^ "
bListAllFiles = False
bFullnameCompare = False
bExtCompare = False
bNameCompare = False
bNamescan = False
bExtscan = False
bRequiredActions = 0
sFilestring = tb1.Text.Trim
Dim ISlm1 As Short = sFilestring.Length - 1
'Scan for illegal characters
If sFilestring <> "" Then
For ii As Short = 0 To ISlm1
For i = 0 To illegalchars.length - 1
If sFilestring(ii) = illegalchars(i) Then
Dim status As Integer = MsgBox("'" & sFilestring(ii) & "' is an illegal filename character." & vbCrLf & "Do you want to continue?", MsgBoxStyle.YesNo, _
"Illegal Character")
If status = Microsoft.VisualBasic.MsgBoxResult.Yes Then
Exit For
Else
GoTo Common_Exit
End If
End If
Next
Next
Else
bListAllFiles = True
bRequiredActions += 1
GoTo Common_Exit
End If
' One contiguous asterisk please
While sFilestring.Contains("**")
sFilestring = sFilestring.Replace("**", "*")
End While
If ((sFilestring = "") Or (sFilestring = "*") Or sFilestring = ("*.*") Or sFilestring = ("*.") _
Or sFilestring = (".*")) Then
bListAllFiles = True
bRequiredActions += 1
GoTo Common_Exit
End If
Dim StarCount As Short
Dim DotCount As Short
ISlm1 = sFilestring.Length - 1 ' Recalculate
For i = 0 To ISlm1
If sFilestring(i) = "*" Then StarCount += 1
If sFilestring(i) = "." Then DotCount += 1
Next
If (((StarCount = 0) And (DotCount = 0)) And ISlm1 > 0) Then
bFullnameCompare = True
bRequiredActions += 1
GoTo Common_Exit
End If
Dim name As String = sFilestring.Substring(0, sFilestring.LastIndexOf("."))
Dim ext As String = sFilestring.Substring(sFilestring.LastIndexOf(".") + 1, ISlm1 - sFilestring.LastIndexOf("."))
If (Not name.Contains("*")) And (Not ext.Contains("*")) Then
bFullnameCompare = True
bRequiredActions += 1
GoTo Common_Exit
End If
If Not name.Contains("*") And ext = "*" Then
bNameCompare = True
bRequiredActions += 1
GoTo Common_Exit
End If
If Not ext.Contains("*") And name = "*" Then
bExtCompare = True
bRequiredActions += 1
GoTo Common_Exit
End If
If Not name.Contains("*") And name.Length > 0 Then
bNameCompare = True
bRequiredActions += 1
End If
If Not ext.Contains("*") And ext.Length > 0 Then
bExtCompare = True
bRequiredActions += 1
End If
'do a name scan
SplitI(name, "*", NameDict)
Select Case NameDict.Count
Case Is = 1
bNameCompare = True
Case Is > 1
bNamescan = True
bRequiredActions += 1
End Select
SplitI(ext, "*", ExtDict)
Select Case ExtDict.Count
Case Is = 1
bExtCompare = True
Case Is > 1
bExtscan = True
bRequiredActions += 1
End Select
Common_Exit:
End Sub
Private Sub SplitI(ByVal Instring As String, ByVal Splitchar As String, ByVal dictionary As Dictionary(Of Integer, String))
dictionary.Clear()
Dim TestArray() As String = Split(Instring, Splitchar)
' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
Dim LastNonEmpty As Integer = -1
For i As Integer = 0 To TestArray.Length - 1
If TestArray(i) <> "" Then
dictionary.Add(i, TestArray(i))
End If
Next
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
pnl.Height = Me.Height - (mnu.Height + ss.Height)
pnl.Width = Me.Width - 8
pnl.Top = mnu.Height
End Sub
Private Sub pnl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnl.Resize
lv1.Height = pnl.Height - 100
lv1.Width = pnl.Width
lv1.Top = Me.Top + mnu.Height
End Sub
Private Sub cbExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbExit.Click
End
End Sub
End Class