Keep only parent directories inside a listbox Or a list(Of String) - vb.net

I have a Listbox that contains a list of Directories Paths. I want to remove each subfolders (existing in the list) and keep only their parents (also existing in the list).
Original List:
D:\Folder_1
D:\Folder_1\Folder_98
D:\Folder_2\Folder_8
D:\Folder_2\Folder_8\Folder_12
D:\Folder_2\Folder_8\Folder_1\Folder_112\
D:\Folder_3\Folder_5
D:\Folder_3\Folder_9
D:\Folder_3\Folder_1
E:\Folder_4\Folder_0
E:\Folder_4\Folder_77
E:\Folder_4\Folder_1
E:\
E:\Folder_4\Folder_01\Folder_0
F:\
H:\
Final List:
D:\Folder_1
D:\Folder_2\Folder_8
D:\Folder_3\Folder_5
D:\Folder_3\Folder_9
D:\Folder_3\Folder_1
E:\
F:\
H:\
For example I tried:
Folders_List_In_Listbox = ListBox1.Items.Cast(Of String)().ToList()
For Each Fold In Folders_List_In_Listbox
Select Case True
Case Path.GetFullPath(Fold).StartsWith(Path.GetFullPath(Mon_Repertoire))
Folders_List_In_Listbox.Remove(Fold)
Case Path.GetFullPath(Mon_Repertoire).StartsWith(Path.GetFullPath(Fold))
Folders_List_In_Listbox_To_Remove.Add(Repertoire)
End Select
Next
ListBox1.DataSource = Folders_List_In_Listbox
How can we do that if our Directories are listed in a listbox Or inside a list(Of String)?
Thanks

I created a code to solve my problem:
Dim List1 As List(Of String) = ListBox1.Items.Cast(Of String)().ToList()
Dim List2 As List(Of String) = ListBox1.Items.Cast(Of String)().ToList(
For Each Mon_Repertoire In List1
If List2.Any(Function(X) Path.GetFullPath(Mon_Repertoire).StartsWith(Path.GetFullPath(X)) _
And Path.GetFullPath(X).Length <> Path.GetFullPath(Mon_Repertoire).Length) Then
List2.Remove(Mon_Repertoire)
End If
Next
ListBox1.DataSource = Nothing
ListBox1.DataSource = List2

Related

Two List(Of String), when I add a value to one it gets added to the other

I'm getting the folders in two different directories and putting their names into two listviews, but if one folder name exist in only one directory I would like to enter nothing into the other list and vice versa, unless the folder name is in both, then I just want the name in both lists.
Here is the code I am using:
Private folders As New List(Of String), oFolders As New List(Of String), AllFoldrs As New List(Of DirectoryInfo), CurFdr As DirectoryInfo
Private Sub Get_LVItems()
folders.Clear() : oFolders.Clear() : CurLV.Items.Clear() : AllFoldrs.Clear() 'Empty folders List and CurrentLV items
Dim LV As ListView = CurLV, oLV As ListView = OtherLv
Dim Pth As String = CurTV.SelectedNode.Name, oPth As String = ""
Dim Dinfo As New DirectoryInfo(Pth), oDinfo As DirectoryInfo = Nothing
Dim TmpFoldrs As New List(Of DirectoryInfo), oTmpFoldrs As New List(Of DirectoryInfo)
If Not IsNothing(OtherTV.SelectedNode) Then
End If
If TVs_Syncd Then
oPth = OtherTV.SelectedNode.Name : oDinfo = New DirectoryInfo(oPth)
TmpFoldrs.AddRange(Dinfo.GetDirectories) : oTmpFoldrs.AddRange(oDinfo.GetDirectories)
AllFoldrs.AddRange(Dinfo.GetDirectories) : AllFoldrs.AddRange(oDinfo.GetDirectories)
AllFoldrs.Sort(AddressOf SrtAllFdrs)
Do While AllFoldrs.Count > 0
CurFdr = AllFoldrs(0)
Dim Found_fdr As DirectoryInfo = Nothing, oFound_fdr As DirectoryInfo = Nothing
For Each fdr As DirectoryInfo In TmpFoldrs
If fdr.Name = CurFdr.Name Then Found_fdr = fdr : Exit For
Next
If IsNothing(Found_fdr) Then folders.Add(Nothing) Else folders.Add(Found_fdr.FullName)
For Each ofdr As DirectoryInfo In oTmpFoldrs
If ofdr.Name = CurFdr.Name Then oFound_fdr = ofdr : Exit For
Next
If IsNothing(oFound_fdr) Then oFolders.Add(Nothing) Else oFolders.Add(oFound_fdr.FullName)
AllFoldrs.RemoveAll(AddressOf RemDirs)'After adding a folder to both collections (folders & oFolders) all instances of that folder get removed from AllFolders
Loop
LoadListView(oLV)
folders.Clear()
folders = oFolders
LoadListView(LV)
Else
folders.AddRange(Directory.GetDirectories(Pth))
LoadListView(LV)
End If
End Sub
two functions:
for removing all instances of the folder name just processed and
the sort function for sorting AllFoldrs after adding all the folder names to it:
Public Function RemDirs(dir As DirectoryInfo) As Boolean
Return dir.Name = CurFdr.Name
End Function
Public Function SrtAllFdrs(ByVal X As DirectoryInfo, ByVal Y As DirectoryInfo) As Integer
Return X.Name.CompareTo(Y.Name)
End Function
I add both directories folders to AllFolders and each directories folders to their own Tmp List.
In the first For Each loop, I see if the current folder name exists in tmp list, if it does I put the value into Found_fdr then when I add it to the list(Of String) I am using to fill list A, it gets added to the other list(Of String) at the same time. I's baffling me!
This is much more to the point than my last question that got no answers, which really didn't surprise me. Anyone? Please...

Linq How to combine List Items to other List items

I have multiple list of string with some items
want to combine every items together like below
Dim _rsltitm = Nothing
For Each _itm1 In _lst1
For Each _itm2 In _lst2
For Each _itm3 In _lst3
_rsltitm &= vbNewLine & _itm1 + _itm2 + _itm3
Next
Next
Next
above code is working fine but i have more than 8 lists or sometimes 11
so i need linq to combine multiple list of string items together
i am trying like this but i could not
Dim _rslt = From itm In _lst1 Select (Function(x) From itm2 In _lst2 Select (Function(d) x & d))
I just tested this code and it seems to do what you want:
Dim list1 As New List(Of String) From {"1", "2", "3"}
Dim list2 As New List(Of String) From {"A", "B", "C"}
Dim list3 As New List(Of String) From {"7", "8", "9"}
Dim list4 As New List(Of String) From {"X", "Y", "Z"}
Dim lists = {list1, list2, list3, list4}
Dim result = lists.Aggregate(Function(current, list)
Dim combinedList As New List(Of String)
For Each prefix In current
combinedList.AddRange(From suffix In list Select prefix & suffix)
Next
Return combinedList
End Function)
You just add all your lists to that lists array and result should end up containing the desired result.
I feel like that Lambda body should be able to be LINQified a bit more but my initial attempts didn't work so I gave up quickly. If you want to put some more time into it, you're welcome to.
EDIT:
Here's that in a function:
Private Function CombineLists(ParamArray lists As List(Of String)()) As List(Of String)
Return lists.Aggregate(Function(current, list)
Dim combinedList As New List(Of String)
For Each prefix In current
combinedList.AddRange(From suffix In list Select prefix & suffix)
Next
Return combinedList
End Function)
End Function
In my example, I could either call that like so:
Dim result = CombineLists(list1, list2, list3, list4)
or like so:
Dim lists = {list1, list2, list3, list4}
Dim result = CombineLists(lists)

Getfile with multiple extension filter and order by file name

i am working on vb.net desktop application.now i need that files coming from directory is in with extension .txt and .sql and also need that files coming in order by folder name. in need both together how to do it?
Try
Dim s As String = Txtfolder.Text
Dim files As List(Of String) = New List(Of String)()
Try
For Each f As String In Directory.GetFiles(s, "*.*").Where(Function(f1) f1.EndsWith(".sql") OrElse f1.EndsWith(".txt")).OrderBy(Function(f) f.LastWriteTime).First()
files.Add(f)
Next
For Each d As String In Directory.GetDirectories(s)
files.AddRange(DirSearch(d))
Next
Catch excpt As System.Exception
MessageBox.Show(excpt.Message)
End Try
Private Function DirSearch(ByVal sDir As String) As List(Of String)
Dim files As List(Of String) = New List(Of String)()
Try
For Each f As String In Directory.GetFiles(sDir, "*.*").Where(Function(f1) f1.EndsWith(".sql") OrElse f1.EndsWith(".txt"))
files.Add(f)
Next
For Each d As String In Directory.GetDirectories(sDir)
files.AddRange(DirSearch(d))
Next
Catch excpt As System.Exception
MessageBox.Show(excpt.Message)
End Try
Return files
End Function
Here is an example of option 1 from my comment, i.e. get all file paths and filter yourself:
Dim folderPath = "folder path here"
Dim filePaths = Directory.GetFiles(folderPath).
Where(Function(s) {".txt", ".sql"}.Contains(Path.GetExtension(s))).
OrderBy(Function(s) Path.GetFileName(s)).
ToArray()
Here's an example of option 2, i.e. get paths by extension and combine:
Dim folderPath = "folder path here"
Dim filePaths = Directory.GetFiles(folderPath, "*.txt").
Concat(Directory.GetFiles(folderPath, "*.sql")).
OrderBy(Function(s) Path.GetFileName(s)).
ToArray()
An alternative method, which allows searching for multiple directories and filtering the results using multiple search patterns.
It returns an ordered List(Of String):
Private Function DirSearch(ByVal sDirList As String(), SearchPatter As String()) As List(Of String)
Return sDirList.SelectMany(
Function(dir) SearchPatter.SelectMany(
Function(filter)
Return Directory.GetFiles(dir, filter, SearchOption.AllDirectories)
End Function).OrderBy(Function(xDir) xDir)).ToList()
End Function
You can pass the method a list of paths and a list of extensions:
Dim SearchPaths As String() = New String() {"[Directory1]", "[Directory2]"}
Dim ItemSearchPattern As String() = New String() {"*.txt", "*.sql", "*.jpg"}
Dim DirListing As List(Of String) = DirSearch(SearchPaths, ItemSearchPattern)
Extract the content of a sigle directory with:
Dim FilesInDir As List(Of String) = DirListing.
Where(Function(entry) entry.ToUpper().
Contains("[DirectoryName]".ToUpper())).ToList()
This is a case insensitive filter. Remove (ToUpper()) for a case sensitive one.

List all folders that are in any 3rd subdirectory from current

I would need to make an array list, displaying all folders that are in the 3rd subfolder from the current one.
Folder1/sub1folder/sub2folder/sub3folder
It has to be recursive. what I need is an array of strings that contains all the strings like above.
I do know how to look recursively into folders, but I do not know how to limit the search to the 3rd subfolder.
Thanks!
Here's my stab at it. I tested it and it works for me:
Dim resultList as List(Of String) = DirectorySearch(baseDirectoryPath, 0)
Function DirectorySearch(directoryPath As String, level As Integer) As List(Of String)
level += 1
Dim directories As String() = IO.Directory.GetDirectories(directoryPath)
Dim resultList As New List(Of String)
If level = 3 Then
For Each subDirectoryPath In directories
Dim result As String = GetFinalResult(subDirectoryPath)
resultList.Add(result)
Next
Else
For Each subDirectoryPath In directories
Dim partialResultList As List(Of String) = DirectorySearch(subDirectoryPath, level)
resultList.AddRange(partialResultList)
Next
End If
Return resultList
End Function
Private Function GetFinalResult(directoryPath As String) As String
Dim directoryInfo As New IO.DirectoryInfo(directoryPath)
Return String.Format("{0}/{1}/{2}/{3}",
directoryInfo.Parent.Parent.Parent.Name,
directoryInfo.Parent.Parent.Name,
directoryInfo.Parent.Name,
directoryInfo.Name)
End Function
If you had a recursive function which began at the current folder:
Public Function recurse(Optional depth As Integer = 0) As String()
Dim folderList As String()
If (depth < 3) Then
depth += 1
folderList = recurse(depth)
Else
'Do third subfolder analysis and set the output to folderList
Return folderList
End If
End Sub

List of lists in vb.net

I am trying to create lists, that are then inserted into another list.
For some reason the latter list gets overwritten each time I try to add new list item to it.
For the code below, first I want to add items to Temp list and after certain conditions have been met, add the Temp list as an item to the Comp list. After that, the cycle repeats, new and different Temp list should be created and the added to the Comp list as next item. So each item in Comp list should be different.
But in the end I get a Comp list that is filled with Temp lists that are all identical to the last Temp list added.
What am I doing wrong?
Function UniqueValueList2(ByVal InputObject As List(Of Object)) As List(Of List(Of Object))
Dim vc As Integer = InputObject.Count
Dim i As Integer = 1
Dim Temp As New List(Of Object)
Dim Comp As New List(Of List(Of Object))
Dim CurrentObj As String
Dim PrevObj As String
Temp.Add(InputObject(0))
Do While i < vc
CurrentObj = InputObject(i).fieldName
PrevObj = InputObject(i-1).fieldName
If CurrentObj = PrevObj Then
Temp.Add(InputObject(i))
Else
Comp.Add(Temp)
Temp.Clear()
Temp.Add(InputObject(i))
End If
i = i + 1
Loop
Comp.Add(Temp)
UniqueValueList2 = Comp
End Function
Temp is holding the same reference. so making changes on it will change it.And you add and modify the same List
Comp.Add(Temp) 'the same Temp List
Temp.Clear() 'You will clear the same List
Temp.Add(InputObject(i))
So How you should do :
Comp.Add(Temp) 'we add old List
Temp=New List(Of Object) 'Temp now holds reference to new List
Temp.Add(InputObject(i))
This will work:
Comp.Add(Temp.ToList())
Temp.Clear()
Temp.Add(InputObject(i))