awolarczuk
asked on
vb/net app to search for a file
Hi guys, what i would like to work out it a simple search engine in my software to search for a file, it only can be one file name, what i need to know is the full path name, thanks guys
ASKER
mate i know about the windows search, but i need my app to start and then if my if statement fails it will ask do you want me to find it for you, then the app will go fine the file i am looking for
so, do you need to specify the folder or base path to search or, search all paths?
ASKER
it is just search all local drives
ASKER
i think it would be to much to search over the network wouldnt
may i know the extension of the file? so i could just filter the file first before checking it
ASKER
yea mate it is MDB thanks
What version VB.Net?
ASKER
2005 mate sorry for not saying before
See http://msdn.microsoft.com/en-us/library/t71ykwhb.aspx
You can pass in a start folder, the name of the file, and whether to search subfolders...
You can pass in a start folder, the name of the file, and whether to search subfolders...
ASKER
mate do you have a demo of this code in action
This finds all .jpg files under the "My Documents" folder:
For Each match As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments, FileIO.SearchOption.SearchAllSubDirectories, New String() {"*.jpg"})
Debug.Print(match)
Next
ASKER
all i need it to do is fine one file i will alway kjnow the name of the file, just need to know the path of that file
Try this way:
Imports System.IO
Public Class Form1
Sub SearchFile(ByVal strRootPath As String, ByVal strFileName As String)
Try
Dim x As Integer = 0
Dim FullDir() As String = IO.Directory.GetDirectories(strRootPath)
For Each Dir As String In FullDir
If Dir <> (strRootPath & "System Volume Information") Then
Dim FullFiles() As String = IO.Directory.GetFiles(Dir, strFileName, SearchOption.AllDirectories)
For Each File As String In FullFiles
Dim fi As New FileInfo(File)
Debug.WriteLine(fi.DirectoryName)
' If you think that you will have more then
' one file, turn the next line as comment
Exit Sub
Next
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call SearchFile("c:\", Me.TextBox1.Text)
End Sub
End Class
You can delete: Dim x As Integer = 0
It was just for counting :)
It was just for counting :)
please try this
Module Module1
Dim filepath As String
Sub Main()
subDir("test.jpg")
Console.ReadLine()
Debug.WriteLine("-----------------------------")
Debug.WriteLine(filepath)
Debug.WriteLine("-----------------------------")
Console.ReadLine()
End Sub
Private Sub subDir(ByVal filename As String)
Try
Dim x As Integer = 0
Dim FullDir() As IO.DriveInfo = IO.DriveInfo.GetDrives()
For Each drive As IO.DriveInfo In FullDir
Try
For Each file As IO.FileInfo In drive.RootDirectory.GetFiles(filename)
filepath = file.DirectoryName()
Exit Sub
Next
For Each folder As IO.DirectoryInfo In drive.RootDirectory.GetDirectories()
If Not folder.FullName.EndsWith("System Volume Information") Then
subdirs(folder.FullName, filename)
End If
Next
Catch ex2 As Exception
Debug.WriteLine(ex2.ToString())
End Try
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString())
End Try
End Sub
Private Sub subdirs(ByVal sPath As String, ByVal f As String)
Dim di As New System.IO.DirectoryInfo(sPath)
If Not di.FullName.EndsWith("System Volume Information") Then
For Each file As IO.FileInfo In di.GetFiles(f)
filepath = file.DirectoryName()
Debug.WriteLine(file.FullName)
Exit Sub
Next
For Each sdir As IO.DirectoryInfo In di.GetDirectories()
Debug.WriteLine(sdir)
subdirs(sdir.FullName, f)
Next
End If
End Sub
End Module
ASKER
thanks mate just having a chancd to look at it now, i am guessing i use it as a modual, when i do a modual and try to call it i get a "can not be use as expression" any thoughts i tihnk i am missing a public some where or i could be doing it wrong
What code have you used ? Can you show us what you have ?
ASKER
Sure mate i used the 2nd one
Option Explicit On
Module databasefind
Dim filepath As String
Sub Main()
subDir("stars.mdb")
Console.ReadLine()
Debug.WriteLine("-----------------------------")
Debug.WriteLine(filepath)
Debug.WriteLine("-----------------------------")
Console.ReadLine()
End Sub
Private Sub subDir(ByVal filename As String)
Try
Dim x As Integer = 0
Dim FullDir() As IO.DriveInfo = IO.DriveInfo.GetDrives()
For Each drive As IO.DriveInfo In FullDir
Try
For Each file As IO.FileInfo In drive.RootDirectory.GetFiles(filename)
filepath = file.DirectoryName()
Exit Sub
Next
For Each folder As IO.DirectoryInfo In drive.RootDirectory.GetDirectories()
If Not folder.FullName.EndsWith("System Volume Information") Then
subdirs(folder.FullName, filename)
End If
Next
Catch ex2 As Exception
Debug.WriteLine(ex2.ToString())
End Try
Next
Catch ex As Exception
Debug.WriteLine(ex.ToString())
End Try
End Sub
Private Sub subdirs(ByVal sPath As String, ByVal f As String)
Dim di As New System.IO.DirectoryInfo(sPath)
If Not di.FullName.EndsWith("System Volume Information") Then
For Each file As IO.FileInfo In di.GetFiles(f)
filepath = file.DirectoryName()
Debug.WriteLine(file.FullName)
Exit Sub
Next
For Each sdir As IO.DirectoryInfo In di.GetDirectories()
Debug.WriteLine(sdir)
subdirs(sdir.FullName, f)
Next
End If
End Sub
End Module
Have you tried my code ? ID:22097971
You just need to add it to your form as use it as the example
Call SearchFile("c:\", Me.TextBox1.Text)
You just need to add it to your form as use it as the example
Call SearchFile("c:\", Me.TextBox1.Text)
ASKER
narr mate i didnt sorry i rthought both o them was from you
so vasicly i add the first fir to the formload and then call it
would it work if i put the first bit in a modal and then call it with a button click
so vasicly i add the first fir to the formload and then call it
would it work if i put the first bit in a modal and then call it with a button click
If you want to use that function SearchFile() everywhere you just need to place it on a module and declare it as public
Public Sub SearchFile(ByVal strRootPath As String, ByVal strFileName As String)
' code
End Sub
Public Sub SearchFile(ByVal strRootPath As String, ByVal strFileName As String)
' code
End Sub
ASKER
thanks mate
how are you doing?.. what happened when you tried my code?..
ASKER
i havent tried it yet mate i will give it a go and let you know as soon as i do thanks so much
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks mate works like a treat thanks so much sorry it took so long to get back to you, giving yo0u all the point mate, hey hoping you could help me out a big more, i am going to write a very simply report in .net and went to do it and coudlt find data report for .net and the other one there i have no idea how to use any chance you could help me out here
ASKER
Very Great to deal with very patient thanks so much for hte help
what kind of report is it?
have you created a new question? if so, please post the link
have you created a new question? if so, please post the link
ASKER
mate here is the link i hope you can help here i am in real need for it
You can now use this to search local and network drives, as well as emails and other such stuff.
I would suggest that you download the code samples from the microsoft website: http://www.microsoft.com/downloads/details.aspx?FamilyID=645300AE-5E7A-4CE7-95F0-49793F8F76E8&displaylang=en
You can also check out:
Overview: http://msdn.microsoft.com/en-us/library/aa965362(VS.85).aspx
Programmatic Query: http://msdn.microsoft.com/en-us/library/bb266517(VS.85).aspx
If you want I can provide you a very simple program that leverage's this technology that you can incorporate into your application.
I must warn you before dedicating yourself to this path, that it does not function 100% at the moment, and certain built-in features need to be custom programmed, however, for your application I think that using WDS or WDS 2.X will work perfect.