I've got some experience with C# but I'm quite new to VB.
My problem is as follows: I want to make a simple program in VB that searches in a specific directory, for a .dft file. I can't wrap my head around it about how I should do this.
Ultimately, I want something like this:
Dim inputdir = "C:\MyAwesomeFolder\test.par"
Depth / level search: X
X is the input level search limit.
For Each saveAsLocation In FormMain.documentRows 'i fetch the filename from here'
For Each foundDirectory As String In IO.Directory.GetDirectories(saveAsLocation.Filename, "*.*", IO.SearchOption.AllDirectories)
For x As Integer = 0 To LoadedSettings.DraftSearchValue Step 1 'i fetch the int limit from here'
For Each nestedDirectory In foundDirectory
Dim foundFiles = Directory.GetFiles(nestedDirectory)
For Each file In foundFiles
If file.EndsWith(".dft") Then
listOfSaveAsLocations.Add(file) 'public string list'
End If
Next
Next
Next
Next
Next
Ihe code stops at foundDirectory
saying that the directory is invalid, which is expected as it needs a directory and not a file
but I don't know how I would go around this.
I've also got an aching feeling I'm using way too many loops for this.
I also tried some good examples https://learn.microsoft.com/en-us/dotnet/api/system.io.searchoption?view=net-7.0
(C# example) Searching All Directories with SearchOption.AllDirectories
but I don't know how I would implement their method and the exact reason as to why.
EDIT: now with counter
For Each saveAsLocation In FormMain.documentRows
counter = 0
For Each foundDirectory As String In IO.Directory.GetDirectories(Path.GetDirectoryName(saveAsLocation.Filename), "*.*", IO.SearchOption.TopDirectoryOnly)
For Each nestedDirectory In foundDirectory
counter += 1
If counter > LoadedSettings.DraftSearchValue Then
Exit For
End If
Dim di As DirectoryInfo = New DirectoryInfo(foundDirectory)
Dim directories As DirectoryInfo() = di.GetDirectories("*.*", SearchOption.TopDirectoryOnly)
Dim files As FileInfo() = di.GetFiles(".dft", SearchOption.TopDirectoryOnly)
Next
Next
Next
went from SearchOption.AllDirectories to SearchOption.TopDirectoryOnly, but now a new problem has arisen: instead of iterating through the current directory to go deeper, it does the opposite instead—now it goes out of the given directory to read all others.