Hi can someone point me in the right direction of searching multiple drives and subdirectories for files.
It would be good if it was not a so accurate search eg.
if I entered bobby in the text box, it would still display bobbybrown.mp3 in the text box. so it like will find words that are similer. |
| djshades2004 Monday, January 22, 2007 3:50 PM |
Try using this search function:
void FileSearch(string sDir,string SearchKey)
{
try
{
// the searchKey can contains a "*",
//e.g. "*.exe",means you can search any file whose name ends with ".exe"
foreach (string f in Directory.GetFiles(sDir, SearchKey))
{
//lstFilesFound is an IList object;
lstFilesFound.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
FileSearch(d,SearchKey);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
Best Regards.
Ye |
| Zhi-Xin Ye Tuesday, January 23, 2007 9:20 AM |
| djshades2004 wrote: | |
Hi thanks for the reply zhi.
Im getting Directory as undeclared? what should I use in place of Directory
|
|
Just import the System.IO namespace.
Namespace Samples Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.Forms Imports System.Collections Imports System.IO Public Class FileSearch Inherits Form Private lstFilesFound As IList Public Sub New() MyBase.New InitializeComponent End Sub Private Sub FileSearch_Load(ByVal sender As Object, ByVal e As EventArgs) Me.lstFilesFound = New ArrayList End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim dirToSearch As String = "c:\\dll" Me.DirSearch(dirToSearch, "*.exe") End Sub Private Sub FileSearch(ByVal sDir As String, ByVal SearchKey As String) Try ' the searchKey can contains a "*", 'e.g. "*.exe",means you can search any file whose name ends with ".exe" For Each f As String In Directory.GetFiles(sDir, SearchKey) 'lstFilesFound is an IList object; lstFilesFound.Add(f) Me.listBox1.Items.Add(f) Next For Each d As String In Directory.GetDirectories(sDir) DirSearch(d) Next Catch excpt As System.Exception Console.WriteLine(excpt.Message) End Try End Sub End Class End Namespace
|
| Zhi-Xin Ye Friday, January 26, 2007 1:57 AM |
Where are you currently at as far as searching a single drive? |
| xr280xr Monday, January 22, 2007 9:39 PM |
|
| Gavin Jin - MSFT Tuesday, January 23, 2007 8:53 AM |
Try using this search function:
void FileSearch(string sDir,string SearchKey)
{
try
{
// the searchKey can contains a "*",
//e.g. "*.exe",means you can search any file whose name ends with ".exe"
foreach (string f in Directory.GetFiles(sDir, SearchKey))
{
//lstFilesFound is an IList object;
lstFilesFound.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
FileSearch(d,SearchKey);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
Best Regards.
Ye |
| Zhi-Xin Ye Tuesday, January 23, 2007 9:20 AM |
Hi Thanks For The replys,
Zhi-Xin Ye : I'm having problems with the code, vb doesn't recongnise the Void command. |
| djshades2004 Tuesday, January 23, 2007 11:05 AM |
| djshades2004 wrote: | |
Hi Thanks For The replys,
Zhi-Xin Ye : I'm having problems with the code, vb doesn't recongnise the Void command.
|
|
Void is not a command, in c# ,it means not return value, so in VB, write this function as:
Private Sub FileSearch(ByVal sDir as string,ByVal SearchKey as string)
.....
END SUB |
| Zhi-Xin Ye Tuesday, January 23, 2007 11:27 AM |
Private Sub FileSearch(ByVal sDir As String, ByVal SearchKey As String)
Try
for each (string f in Directory.GetFiles(sDir, SearchKey))
lstFilesFound is an IList object
lstFilesFound.Add(f)
foreach (string d in Directory.GetDirectories(sDir))
FileSearch(d, SearchKey)
catch (System.Exception excpt)
Console.WriteLine(excpt.Message)
End Sub
What problems are there with this code please?
|
| djshades2004 Thursday, January 25, 2007 12:41 PM |
Oooh,no, what I just gave you is a clue, you must translate all my c# code to VB.net code,let metranslate it for you.
PrivateSubFileSearch(ByValsDirAsString,ByValSearchKeyAsString) Try 'thesearchKeycancontainsa"*", 'e.g."*.exe",meansyoucansearchanyfilewhosenameendswith".exe" ForEachfAsStringInDirectory.GetFiles(sDir,SearchKey) 'lstFilesFoundisanIListobject;You must define it outside this function. lstFilesFound.Add(f) Next ForEachdAsStringInDirectory.GetDirectories(sDir) FileSearch(d,SearchKey) Next CatchexcptAsSystem.Exception Console.WriteLine(excpt.Message) EndTry EndSub
And. I recommend a web site for you to translate between c# and vb code, see this site.
Best Regards.
Ye |
| Zhi-Xin Ye Thursday, January 25, 2007 2:27 PM |
Hi thanks for the reply zhi.
Im getting Directory as undeclared? what should I use in place of Directory |
| djshades2004 Thursday, January 25, 2007 2:48 PM |
| djshades2004 wrote: | |
Hi thanks for the reply zhi.
Im getting Directory as undeclared? what should I use in place of Directory
|
|
Just import the System.IO namespace.
Namespace Samples Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Text Imports System.Windows.Forms Imports System.Collections Imports System.IO Public Class FileSearch Inherits Form Private lstFilesFound As IList Public Sub New() MyBase.New InitializeComponent End Sub Private Sub FileSearch_Load(ByVal sender As Object, ByVal e As EventArgs) Me.lstFilesFound = New ArrayList End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Dim dirToSearch As String = "c:\\dll" Me.DirSearch(dirToSearch, "*.exe") End Sub Private Sub FileSearch(ByVal sDir As String, ByVal SearchKey As String) Try ' the searchKey can contains a "*", 'e.g. "*.exe",means you can search any file whose name ends with ".exe" For Each f As String In Directory.GetFiles(sDir, SearchKey) 'lstFilesFound is an IList object; lstFilesFound.Add(f) Me.listBox1.Items.Add(f) Next For Each d As String In Directory.GetDirectories(sDir) DirSearch(d) Next Catch excpt As System.Exception Console.WriteLine(excpt.Message) End Try End Sub End Class End Namespace
|
| Zhi-Xin Ye Friday, January 26, 2007 1:57 AM |
I think you mean
PrivateSubFileSearch(ByValsDirAsString,ByValSearchKeyAsString)
PrivateSubDirSearch(ByValsDirAsString,ByValSearchKeyAsString) . . . ForEachdAsStringInDirectory.GetDirectories(sDir) DirSearch(d, SearchKey) Next |
| JRQ Friday, January 26, 2007 2:05 AM |