Results 1 to 6 of 6

Thread: [RESOLVED]VB.NET - Search for specific file in Directory and subdirectories

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Thumbs up [RESOLVED]VB.NET - Search for specific file in Directory and subdirectories

    I'm trying to program a program that looks for a specific file in a folder. If it finds the file, it has to start it

    My not working code
    Code:
      Dim directory = "C:\Users\Multi\Desktop\"
    
    
            For Each filename As String In IO.Directory.GetFiles(directory, "*.exe", IO.SearchOption.AllDirectories)
    
                If filename = "test.exe" Then
    
                    Process.Start(filename)
    
                End If
            Next
    Could someone give me a help?

    Thanks in advance
    Last edited by Stephen_Hawking; Sep 1st, 2018 at 06:26 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VB.NET - Search for specific file in Directory and subdirectories

    That code is not working because 'filename' contains the full path of the file, not just the name. You shouldn't need that pointed out because you should have looked for yourself.

    Also, if you want a file named "test.exe" then why look for "*.exe"? Why not look for "test.exe"? If you're going to do that though, there's no point calling Directory.GetFiles at all. You should just call File.Exists.
    vb.net Code:
    1. Dim filePath = "C:\Users\Multi\Desktop\test.exe"
    2.  
    3. If File.Exists(filePath) Then
    4.     Process.Start(filePath)
    5. End If

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VB.NET - Search for specific file in Directory and subdirectories

    Ah, I just noticed that you are searching subfolders too. In that case, you would need to stick with Directory.GetFiles:
    vb.net Code:
    1. Dim folderPath = "C:\Users\Multi\Desktop"
    2. Dim filePath = Directory.GetFiles(folderPath,
    3.                                   "test.exe",
    4.                                   SearchOption.AllDirectories).FirstOrDefault()
    5.  
    6. If filePath IsNot Nothing Then
    7.     Process.Start(filePath)
    8. End If
    Just be aware that, theoretically at least, there could be multiple files with that name in different folders. If you wanted to start each one of multiple then you can do this:
    vb.net Code:
    1. Dim folderPath = "C:\Users\Multi\Desktop"
    2.  
    3. For Each filePath In Directory.GetFiles(folderPath,
    4.                                         "test.exe",
    5.                                         SearchOption.AllDirectories)
    6.     Process.Start(filePath)
    7. Next

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Re: VB.NET - Search for specific file in Directory and subdirectories

    Maybe I explained wrong. "Test.exe" could be everywhere in the folder Desktop. What I'm trying to di is, search in all folders for the file "test.exe"

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VB.NET - Search for specific file in Directory and subdirectories

    Quote Originally Posted by Stephen_Hawking View Post
    What I'm trying to di is, search in all folders for the file "test.exe"
    See my second response. I missed that the first time around.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Re: VB.NET - Search for specific file in Directory and subdirectories

    thank you very much! This works !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width