Ok i was wonderin. Can I, and if so, how do you search or if you like 'Scan' a hard drive with vb for a filename such as lala.exe or what eva.
I'd love to know! ![]()
Ok i was wonderin. Can I, and if so, how do you search or if you like 'Scan' a hard drive with vb for a filename such as lala.exe or what eva.
I'd love to know! ![]()
Search
Vishantha
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
garfield1969
Tiaan
I tryed do a directory/file recursive search (from the root level) a while back using Directory.GetDirectories and Directory.GetFiles and kept running into specialized or system protected directories. So I moved on to other stuff.
Just tryed:
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dirar As String() = Directory.GetFiles("c:\", "*.jpg", SearchOption.AllDirectories) End Suband get: Access to the path 'c:\System Volume Information' is denied.
Sorry to butt in on the thread here but it is kind of on topic.
Is code as long-winded as Renee's example required to get around all the exceptions
RokitMan
thanks for all your help!
miznig
No, if you make your code recursive and call GetDirectories itself on all the sub directories, then all you'd need to do is catch that exception on each iteration. The problem with my code is, if you catch the exception, the whole search fails, not just for the subfolder.
Chips_in
Good Lord. That is totally not what I had in mind.
Directory.GetFiles takes a search string, I wouldn't expect to ask for all files and then filter it myself.
naffets
Dim files as String() = System.IO.Directory.GetFiles(tbRootDir.Text, tbFilter.Text, IO.SearchOption.AllDirectories)) Dim dir As String For Each dir In filesMessageBox.Show(dir)
NextThis code works fine, and the filter requires wildcard characters or complete names to work. The only problem is, if you start in your root dir, you'll get an error when you try to read into protected system folders. I'm not sure how to get around that.
Note - this code comes from a demo project that defines tbRootDir and tbFilter as text boxes. You'd replace this with code that contains teh strings in your project. I used the root dir of my development folders and *.csproj to test it, it worked fine.
Chris O
Directory.GetDirectories and Directory.GetFiles can be used to recurse over a directory structure and apply a search string.
w1z44rdy
My code also does that. It just needs to be made recursive so it iterates over subfolders on it's own, so that it can skip folders it's not allowed to read.
NeXuS_ITA
I just noticed something....
I cut and pasted twice...... I just eliminated half of the code.
My code is recursive.....
What makes my code long winded is the abilitiy to search for things like
*k*.*x*
jaypee68
I just looked up C# example of directory.getfiles here:
http://www.vbforums.com/showthread.php p=2156918#post2156918
The search engines have about the same amount of code. Additional code in mine id wildacard processing (no to mention the panel resizing code which I threw in for free.)