Search Files on hardrive
-
Friday, July 29, 2011 6:38 AM
I am using this code to get files, but I can not get it to search for all files on C: drive.
How can I get it to show all files Like *.pdf of the drive in the list boxImports System.IO Imports System Imports System.Drawing Imports System.Windows.Forms Public Class SearchAvi Private Sub BtnGetAviFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGetAviFiles.Click Dim dir1 As New IO.DirectoryInfo("c:") Dim dir2 As IO.FileInfo() = dir1.GetFiles() Dim files1 As IO.FileInfo For Each files1 In dir2 FrmListBox.Items.Add(files1) Next End Sub
All Replies
-
Friday, July 29, 2011 9:34 AM
Searching a drive is done by scanning each folder in it
This thread describes all that you need to search a Drive
Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click For Each strFolder As String In _ My.Computer.FileSystem.GetDirectories("C:\") If strFolder = "C:\$Recycle.Bin" Then ElseIf strFolder = "C:\System Volume Information" Then Else ListBox1.Items.Add(strFolder) End If Next End Sub Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click For i = 0 To ListBox1.Items.Count - 1 ListBox1.SelectedIndex = i ListBox2.Items.AddRange(Directory.GetFiles(ListBox1.SelectedItem.ToString & "\", "*.txt", SearchOption.AllDirectories)) Next End Sub End Class
Faraz- Proposed As Answer by Faraz Zone Friday, July 29, 2011 11:46 AM
- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Thursday, August 11, 2011 11:10 AM
-
Friday, July 29, 2011 10:18 AM
Thank you, got it working, but how can I also let it search for recent files
C:\Documents and Settings\Administrator\Recent
It does not show the files in this folder
-
Friday, July 29, 2011 10:25 AM
Thank you, got it working, but how can I also let it search for recent files
C:\Documents and Settings\Administrator\Recent
Which Windows are you using ? (XP or Vista\ win7)
In windows 7 the complete path to "Recent" folder is
dim path as string path = "C:\Users\faraz\AppData\Roaming\Microsoft\Windows\Recent" ListBox2.Items.AddRange(Directory.GetFiles(path & "\", "*.txt", SearchOption.AllDirectories))
Faraz- Proposed As Answer by Faraz Zone Friday, July 29, 2011 11:47 AM
- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Thursday, August 11, 2011 11:10 AM
-
Friday, July 29, 2011 10:35 AM
I am using Windows xp sp3
I Know I am doing something stupid here, but I can not get it to work
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i = 0 To FrmListBox.Items.Count - 1
Dim path As String
path = "C:\Documents and Settings\Administrator\Recent"
FrmListBox1.Items.AddRange(Directory.GetFiles(path & "\", "*.avi", SearchOption.AllDirectories)) Next End Sub
I need it to seach all diretories and the Recent folder as well
-
Friday, July 29, 2011 11:21 AM
Windows Xp does not have UAC restriction like win 7 and vista (do not know abou sp3 )
Look below to understand what the code is doing
1.
ListBox1.Items.AddRange(Directory.GetFiles("C:\", "*.txt", SearchOption.AllDirectories))
The above code search C: for files with extension ".txt". It does not search any directory in C: only the top files.
2.
For Each strFolder As String In _ My.Computer.FileSystem.GetDirectories("C:\") ListBox1.Items.Add(strFolder) Next
This code generates a list of all the Top folders in C: but does not search for any file3.
For i = 0 To ListBox1.Items.Count - 1 ListBox1.SelectedIndex = i ListBox2.Items.AddRange(Directory.GetFiles(ListBox1.SelectedItem.ToString & "\", "*.txt", SearchOption.AllDirectories)) Next
This part of the code uses the List of directories created in step 2 and search's for file in each of them and there sub directoryAs the "Recent" folder is in the "c:\Documents and Settings" so the code will first create a list of folder in step 2 (this list includes Documents and Settings folder ) and when it is the turn of Documents and Settings folder to be searched then it will search the Administrator folder and then the recent folder.
If you want separately view Recent folder then drag and drop another list box and code like this
Dim path As String path = "C:\Documents and Settings\Administrator\Recent" newListBox1.Items.AddRange(Directory.GetFiles(path & "\", "*.avi", SearchOption.AllDirectories))
Faraz- Proposed As Answer by Faraz Zone Friday, July 29, 2011 11:47 AM
- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Thursday, August 11, 2011 11:10 AM
-
Friday, July 29, 2011 11:37 AM
Faraz Zone
Thank you for the info, it help me quit a lot.
I am using number 3, but is does not show the recent files, but when I do number 4 it show it, it is a pitty that it would not show in number 3.
-
Friday, July 29, 2011 11:41 AM
It does show in step 3 but it is all mixed up with the files in other folders.
Please mark those posts as answer which answers your question.
Faraz -
Saturday, July 30, 2011 8:27 PM
Try this ,
Name of file in textbox2 and check this wich drive
Dim FileSearch2 As New FileSearch(TextBox2.Text, "C:\")
Imports System.IO
Public Class Form1
Private Sub FileFound(ByVal filePath As String)
'Wanneer een bestand gevonden is zal dit hier worden gemeld
'Het pad van het bestand dat is gevonden staat in de variable 'filePath'
If TextBox1.Text = "" Then
TextBox1.Text = filePath
Else
TextBox1.Text &= vbNewLine & filePath
End If
Label1.Text = TextBox1.Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim FileSearch2 As New FileSearch(TextBox2.Text, "C:\")
AddHandler FileSearch2.Found, AddressOf FileFound
FileSearch2.Search() 'Start het zoeken
Catch
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim myStream As New StreamWriter(OpenFileDialog1.FileName, True)
myStream.Write(Label1.Text)
myStream.Close()
End If
End Sub
End Class
Public Class FileSearch
Public Event Found(ByVal filePath As String)
Public ReadOnly Property SearchingDirectory()
Get
SearchingDirectory = SDir
End Get
End Property
Private WithEvents BW As New System.ComponentModel.BackgroundWorker
Private FilesFound As New List(Of String)
Private Errors As New List(Of Exception)
Private Keyword As String
Private StartPath As String
Private SDir As String
Public ReadOnly Property Searching() As Boolean
Get
Searching = BW.IsBusy
End Get
End Property
Public Sub New(ByVal fileName As String, ByVal path As String)
BW.WorkerSupportsCancellation = True
BW.WorkerReportsProgress = True
Keyword = fileName
StartPath = path
End Sub
Public Sub Search()
BW.RunWorkerAsync()
End Sub
Public Sub Cancel()
BW.CancelAsync()
End Sub
Private Sub findFile(ByVal path As String)
If Errors.Count = 0 Then
Try
SDir = path
For Each File As String In System.IO.Directory.GetFiles(path)
If System.IO.Path.GetFileNameWithoutExtension(File).ToLower.Replace(" ", "") = Keyword.ToLower.Replace(" ", "") Then
FilesFound.Add(File)
BW.ReportProgress(0)
End If
Next
For Each Dir As String In System.IO.Directory.GetDirectories(path)
findFile(Dir)
Next
Catch Access As UnauthorizedAccessException
'Nothing
Catch ex As Exception
If Errors.Count > 0 Then
If Errors.Item(Errors.Count - 1).Message <> ex.Message Then
Errors.Add(New Exception(ex.Message))
End If
Else
Errors.Add(New Exception(ex.Message))
End If
BW.ReportProgress(0)
End Try
End If
End Sub
Private Sub BW_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BW.DoWork
findFile(StartPath)
End Sub
Private Sub BW_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BW.ProgressChanged
For Each File As String In FilesFound
RaiseEvent Found(File)
Next
FilesFound.Clear()
End Sub
Private Sub BW_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BW.RunWorkerCompleted
If Errors.Count > 0 Then
For Each ex As Exception In Errors
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Next
Errors.Clear()
End If
End Sub
End Class
Bassies
- Proposed As Answer by Mike FengMicrosoft Contingent Staff, Moderator Monday, August 08, 2011 12:54 PM
- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Thursday, August 11, 2011 11:10 AM
-
Monday, August 01, 2011 6:50 AM
Hi Bassies
Dankie
Than you for the info I have done this, but geting an error on this line
Private Sub BW_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BW.ProgressChanged For Each File As String In FilesFound RaiseEvent Found(File) Next FilesFound.Clear() End Sub
RaiseEvent Found(File)invalidoperationexception was unhanled by user code
-
Monday, August 08, 2011 12:55 PMModerator
Hi Hendrik,
This code works for me, please check your code again.
Best regards,
Mike Feng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


