-
Dec 25th, 2017, 06:41 PM
#1
Thread Starter
Junior Member
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?
-
Dec 25th, 2017, 09:31 PM
#2
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:
Dim files() As String = IO.Directory.GetFiles("your\path", "*test.text", IO.SearchOption.AllDirectories) For j = 0 To files.Length - 1 Debug.WriteLine(files(j)) If IO.Path.GetFileName(files(j)).ToLower = "your file name" Then 'Do something useful MessageBox.Show(Me, "Found it in: " & IO.Path.GetDirectoryName(files(j)), My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information) End If Next
Last edited by 4x2y; Dec 25th, 2017 at 09:45 PM.
-
Dec 26th, 2017, 08:40 AM
#3
Thread Starter
Junior Member
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.
-
Dec 26th, 2017, 09:10 AM
#4
Re: Searching path for specific file, and if that file exists, getting the path of it
Originally Posted by Proudyy
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.
-
Dec 26th, 2017, 02:33 PM
#5
Re: Searching path for specific file, and if that file exists, getting the path of it
Originally Posted by Proudyy
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|