PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2017 VB.NET - Search for specific file in Directory and subdirectories-VBForums
Results 1 to 6 of 6

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    10

    Question 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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    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
    Last edited by jmcilhinney; Aug 10th, 2018 at 01:40 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    10

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    10

    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
  •  



Featured


Click Here to Expand Forum to Full Width