locked
Specific file search RRS feed

  • Question

  • Hello Everyone,

    I would like to make a super-specific search in my computer.

    Meaning, I have a file here : C:\Users\MyUser\Desktop\MyFiles\Mytext.txt , and at one point, the user decides to move the Mytext.txt to another folder anywhere in the hard drive.

    How can i find it's new location? I want to search for the EXACT same name "Mytext.txt" , no similar files or a file containing this name or anything else.

    Is there an easy way to do this with .net libs or do i have to make a custom function?

    Thanks in advance :)


    TsopTsop

    Sunday, November 18, 2012 12:37 PM

Answers

  • Thanks for the code John, it may come in use, i developed my custom search while waiting for an answer

        Public Function Search(ByVal sFile As String)
    
            Dim TopLevel As New List(Of String)
            Dim NextLevel As New List(Of String)
    
            'Get A list of all Available SubDirectories of C:\Users\MyUser
            For Each Dir As String In My.Computer.FileSystem.GetDirectories("C:\Users\" & UserName(), FileIO.SearchOption.SearchTopLevelOnly)
                If Not Dir.Contains("AppData") Then TopLevel.Add(Dir)
            Next
    
            Do Until TopLevel.Count = 0
                For Each TopDir As String In TopLevel
                    On Error Resume Next
                    For Each Dir As String In My.Computer.FileSystem.GetDirectories(TopDir, FileIO.SearchOption.SearchTopLevelOnly)
                        NextLevel.Add(Dir)
                        For Each item As String In My.Computer.FileSystem.GetFiles(Dir)
                            Dim d As String = My.Computer.FileSystem.GetFileInfo(item).Name
                            If d = sFile Then
                                Return item
                            End If
                        Next
                    Next
                Next
                TopLevel.Clear()
                For Each item As String In NextLevel
                    TopLevel.Add(item)
                Next
                NextLevel.Clear()
            Loop
    
            Return ErrorType.SongNotFound
    
        End Function

    Function UserName() returns : My.User.Name.Substring(My.User.Name.IndexOf("\") + 1, My.User.Name.Count - My.User.Name.IndexOf("\") - 1)


    TsopTsop

    • Marked as answer by TsopTsop Sunday, November 18, 2012 5:25 PM
    Sunday, November 18, 2012 5:25 PM

All replies

  • File names are only unique to the containing directory.  No matter what you do, if you do not know something else specific to the file you're after, you will get a list of all files having the same name.  You can write your own search.  Start with all files in the root directory and a directory diver.  Or you can just use Windows' file search.
    Sunday, November 18, 2012 3:44 PM
  • Either you write a own file search routine (recommandet because you can move a file with the explorer) or you store the location as my.Stting. This only works when you change the destination with your application or you create a permanent working service which watch files destinations and operations also when your application is not working. The last guess is not really a beginner job.

    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 and VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful. c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Sunday, November 18, 2012 3:55 PM
  • The second option is nice but quite hard for the nature of my project,

    Ok more specific,

    I have a database of songs, and the application keeps some information for each one, so i want to keep each song data as low as possible.

    My application save the paths of the songs. If the user wants to move the song, i want my application to detect it  searching in C:\Users\MyUser like that:

    For Each file As String in My.Computer.Filesystem.GetFiles("C:\Users\MyUser", SearchAllSubDirectories)
    
         If My.Computer.FileSystem.GetFileInfo(file).Name = FILE_TO_FIND Then
    
               return file
    
        End If
    
    Next

    But some directories deny access and stop my search, and command On Error Resume Next, doesn't solve my problem...

    TsopTsop

    Sunday, November 18, 2012 4:10 PM
  • Use windows' file search through vb.net?

    I don't mind the existance of many same files ( not likely for song files ). The ability to let the user choose what is the correct file, is fine :) . Please also check the answer i gave to Giftzwockel


    TsopTsop

    Sunday, November 18, 2012 4:12 PM
  • To avoid IO errors that will stop your search, code your own search.  Here's one that  searches in the  background:

    Imports System.IO
    Imports System.ComponentModel
    Class SearchForFile
      Inherits System.ComponentModel.BackgroundWorker
      Sub New(ByVal FileName As String, ByVal RootFolder As String)
        Me.WorkerReportsProgress = True
        Me.WorkerSupportsCancellation = True
        Me.RunWorkerAsync(New String() {FileName, RootFolder})
      End Sub
      Protected Overrides Sub OnDoWork(ByVal e As System.ComponentModel.DoWorkEventArgs)
        MyBase.OnDoWork(e)
        Dim Params() As String = DirectCast(e.Argument, String())
        Dim Stack As New Stack(Of String)(New String() {Params(1)})
        Dim FoundFiles As New List(Of String)
        While Stack.Count > 0
          If Me.CancellationPending Then e.Cancel = True : Return
          Dim Dir As String = Stack.Pop
          Try
            For Each D As String In Directory.GetDirectories(Dir)
              Stack.Push(D)
            Next
          Catch
            'IO error.  Typically permission denied.
            Continue While
          End Try
          For Each F As String In Directory.GetFiles(Dir)
            If Path.GetFileName(F).Equals(Params(0)) Then FoundFiles.Add(F)
          Next
        End While
        e.Result = FoundFiles
      End Sub
    End Class

    Sunday, November 18, 2012 4:22 PM
  • This thread has an answer on how to scan all files on the drive:

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/05072df6-41b0-425f-b5c5-2929de0b0f66/

    You of course would have to modify the program to filter your search query from the results, but most of the hard work is done...

    I want to add, what you want to do might be possible if you had an md5 checksum of the file, you could then first find matching file names, then compare md5 checksums.


    If you want something you've never had, you need to do something you've never done.
    Everyone(not just the thread starter) should take the time to mark helpful posts, propose answers, and mark answers to questions.


    Sunday, November 18, 2012 4:33 PM
    Moderator
  • Thanks for the code John, it may come in use, i developed my custom search while waiting for an answer

        Public Function Search(ByVal sFile As String)
    
            Dim TopLevel As New List(Of String)
            Dim NextLevel As New List(Of String)
    
            'Get A list of all Available SubDirectories of C:\Users\MyUser
            For Each Dir As String In My.Computer.FileSystem.GetDirectories("C:\Users\" & UserName(), FileIO.SearchOption.SearchTopLevelOnly)
                If Not Dir.Contains("AppData") Then TopLevel.Add(Dir)
            Next
    
            Do Until TopLevel.Count = 0
                For Each TopDir As String In TopLevel
                    On Error Resume Next
                    For Each Dir As String In My.Computer.FileSystem.GetDirectories(TopDir, FileIO.SearchOption.SearchTopLevelOnly)
                        NextLevel.Add(Dir)
                        For Each item As String In My.Computer.FileSystem.GetFiles(Dir)
                            Dim d As String = My.Computer.FileSystem.GetFileInfo(item).Name
                            If d = sFile Then
                                Return item
                            End If
                        Next
                    Next
                Next
                TopLevel.Clear()
                For Each item As String In NextLevel
                    TopLevel.Add(item)
                Next
                NextLevel.Clear()
            Loop
    
            Return ErrorType.SongNotFound
    
        End Function

    Function UserName() returns : My.User.Name.Substring(My.User.Name.IndexOf("\") + 1, My.User.Name.Count - My.User.Name.IndexOf("\") - 1)


    TsopTsop

    • Marked as answer by TsopTsop Sunday, November 18, 2012 5:25 PM
    Sunday, November 18, 2012 5:25 PM
  • thanks for the link :)

    TsopTsop

    Sunday, November 18, 2012 5:25 PM
  • You're welcome ;)

    If you want something you've never had, you need to do something you've never done.
    Everyone(not just the thread starter) should take the time to mark helpful posts, propose answers, and mark answers to questions.

    Monday, November 19, 2012 11:50 PM
    Moderator