Results 1 to 5 of 5

Thread: Searching path for specific file, and if that file exists, getting the path of it

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2017
    Posts
    22

    Searching path for specific file, and if that file exists, getting the path of it

    Hey guys!
    I want in vb.net to search for a specific named file in a path, and if that file exists, i need to get the path of the file.
    But i don't know how to code something like this..
    I tried things like Directory.GetFiles(path, "*test.txt"), but they don't work...
    Any help?

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Searching path for specific file, and if that file exists, getting the path of it

    What do you mean by "they don't work..."? An error occur? Found other files but not the wanted file? Something else?

    Maybe you are searching the top directory, instead search all directories
    vb.net Code:
    1. Dim files() As String = IO.Directory.GetFiles("your\path", "*test.text", IO.SearchOption.AllDirectories)
    2.         For j = 0 To files.Length - 1
    3.             Debug.WriteLine(files(j))
    4.             If IO.Path.GetFileName(files(j)).ToLower = "your file name" Then
    5.                 'Do something useful
    6.                 MessageBox.Show(Me, "Found it in: " & IO.Path.GetDirectoryName(files(j)), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
    7.             End If
    8.         Next
    Last edited by 4x2y; Dec 25th, 2017 at 09:45 PM.



  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2017
    Posts
    22

    Re: Searching path for specific file, and if that file exists, getting the path of it

    What you have posted is working well for me,
    but i wanted the full path with the filename but that is no problem for me just adding & "text.txt" if it gots found.

    I ment with: "they don't work" that an error eccured but i didnt knew what it means xd
    Nvm u helped me enough.


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

    Re: Searching path for specific file, and if that file exists, getting the path of it

    Quote Originally Posted by Proudyy View Post
    but i wanted the full path with the filename but that is no problem for me just adding & "text.txt" if it gots found.
    You don't need to add anything to anything. The code example provided specifically REMOVES the file name from the path to get just the folder path. If you don't want just the folder path then don't remove the file name in the first place and definitely don't remove it and then add it back again.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Searching path for specific file, and if that file exists, getting the path of it

    Quote Originally Posted by Proudyy View Post
    What you have posted is working well for me,
    but i wanted the full path with the filename but that is no problem for me just adding & "text.txt" if it gots found.
    files array contains the full path, i was think you need to know the parent directory of the file!



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