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

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...

Related

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.

Keep only parent directories inside a listbox Or a list(Of String)

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

Use .Contains() to match on a property of a property of <T> in LINQ query

Looking for help on how to perform a LINQ query using the .Contains() method of a List(Of T) to get back elements that are not contained in a second List(Of T) based on a property of a property of T in the first List(Of T).
Here is some sample code that I wrote up, this scenario is ficticious, but the concept is still there.
Module Module1
Sub Main()
' Get all Files in a directory that contain `.mp` in the name
Dim AllFiles As List(Of IO.FileInfo) = New IO.DirectoryInfo("C:\Test\Path").GetFiles("*.mp*").ToList
Dim ValidFiles As New List(Of fileStruct)
' Get all Files that actually have an extension of `.mp3`
AllFiles.ForEach(Sub(x) If x.Extension.Contains("mp3") Then ValidFiles.Add(New fileStruct(prop1:=x.Name, path:=x.FullName)))
' Attempting the get all files that are not listed in the Valid files list
Dim InvalidFiles As IO.FileInfo() = From file As IO.FileInfo In AllFiles Where Not ValidFiles.Contains(Function(x As fileStruct) x.fleInfo.FullName = file.FullName) Select file
' Errors on the `.Contains()` method because I have no idea what I'm doing and I am basically guessing at this point
'Here is the same but instead using the `.Any()` Method
Dim InvalidFiles As IO.FileInfo() = From file As IO.FileInfo In AllFiles Where Not ValidFiles.Any(Function(x As fileStruct) x.fleInfo.FullName = file.FullName) Select file
' This doesn't error out, but all files are returned
End Sub
Public Structure fileStruct
Private _filePath As String
Private _property1 As String
Public ReadOnly Property property1 As String
Get
Return _property1
End Get
End Property
Public ReadOnly Property fleInfo As IO.FileInfo
Get
Return New IO.FileInfo(_filePath)
End Get
End Property
Public Sub New(ByVal prop1 As String, ByVal path As String)
_property1 = prop1
_filePath = path
End Sub
End Structure
End Module
This is a more or less direct implementation of the MP3 files list in the question. I did use a FileItem class instead of a structure. The good part is afterwards:
' note: EnumerateFiles
Dim AllFiles As List(Of IO.FileInfo) = New IO.DirectoryInfo("M:\Music").
EnumerateFiles("*.mp*", IO.SearchOption.AllDirectories).ToList()
Dim goofyFilter As String() = {"g", "h", "s", "a"}
' filter All files to those starting with the above (lots of
' Aerosmith, Steely Dan and Heart)
Dim ValidFiles As List(Of FileItem) = AllFiles.
Where(Function(w) goofyFilter.Contains((w.Name.ToLower)(0))).
Select(Function(s) New FileItem(s.FullName)).ToList()
Dim invalid As List(Of FileInfo)
invalid = AllFiles.Where(Function(w) Not ValidFiles.
Any(Function(a) w.FullName = a.FilePath)).ToList()
This is much the same as Sam's answer except with your file/mp3 usage. AllFiles has 809 items, ValidFiles has 274. The resulting invalid list is 535.
Now, lets speed it up 50-60x:
Same starting code for AllFiles and ValidFiles:
Dim FileItemValid = Function(s As String)
Dim valid As Boolean = False
For Each fi As FileItem In ValidFiles
If fi.FilePath = s Then
valid = True
Exit For
End If
Next
Return valid
End Function
invalid = AllFiles.Where(Function(w) FileItemValid(w.FullName) = False).ToList()
With a Stopwatch, the results are:
Where/Any count: 535, time: 572ms
FileItemValid count: 535, time: 9ms
You get similar results with a plain old For/Each loop that calls an IsValid function.
If you do not need other FileInfo, you could create your AllFiles as a list of the same structure as you are receiving so you can do property vs property compares, use Except and Contains:
AllFiles2 = Directory.EnumerateFiles("M:\Music", "*.mp3", IO.SearchOption.AllDirectories).
Select(Function(s) New FileItem(s)).ToList()
Now you can use Contains with middling results:
invalid2 = AllFiles2.Where(Function(w) Not ValidFiles.Contains(w)).ToList()
This also allows you to use Except which is simpler and faster:
invalid2 = AllFiles2.Except(ValidFiles).ToList()
Where/Contains count: 535, time: 74ms
Except count: 535, time: 3ms
Even if you need other items from FileInfo, you can easily fetch them given the filename
As others have noted, .Except() is a better approach but here is an answer to your question:
List<int> list1 = new List<int> { 1, 2, 3 };
List<int> list2 = new List<int> { 3, 4, 5 };
List<int> list3 = list1.Where(list1value => !list2.Contains(list1value)).ToList(); // 1, 2
Based on comments here as an example using different types. This query use .Any()
List<Product> list1 = new List<Produc> { ... };
List<Vendor> list2 = new List<Vendor> { ... };
List<Product> list3 = list1.Where(product => !list2.Any(vendor => product.VendorID == vendor.ID)).ToList();
// list3 will contain products with a vendorID that does not match the ID of any vendor in list2.
Simply use Except as CraigW suggested. You have to do some projections (select) to get it done.
Dim InvalidFiles as IO.FileInfo() = AllFiles.Select(Function(p) p.FullName).Except(ValidFiles.Select(Function(x) x.fleInfo.FullName)).Select(Function(fullName) New IO.FileInfo(fullName)).ToArray()
Note: This code is not really efficient and also not very readable but works.
But i would go for something like this:
Dim AllFiles As List(Of IO.FileInfo) = New IO.DirectoryInfo("C:\MyFiles").GetFiles("*.mp*").ToList
Dim ValidFiles As New List(Of fileStruct)
Dim InvalidFiles as New List(Of FileInfo)
For Each fileInfo As FileInfo In AllFiles
If fileInfo.Extension.Contains("mp3") Then
ValidFiles.Add(New fileStruct(prop1:=fileInfo.Name, path:=fileInfo.FullName))
Else
InvalidFiles.Add(fileInfo)
End If
Next
Simple, fast and readable.

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

vb.net get filename list from wildcard

I have string say "c:\debug\ *.txt"
In Debug folder there are severeal .txt files , say test1.txt test2.txt test3.txt .
How can I get from this string c:\debug\ *.txt an array of wildcard files?
a(0)=c:\debug\test1.txt
a(1)=c:\debug\test2.txt
a(2)=c:\debug\test3.txt
It is also possible that the string would be something like "C:\logs\12*\ *.log"
a(0)=C:\logs\120114\01.log
a(0)=C:\logs\120114\02.log
a(0)=C:\logs\120114\03.log
etc.
Anyone have any ideas on this?
I use the following code:
Dim Path As String = "C:\debug"
Dim Dir As New DirectoryInfo(Path)
Dim q = (From x In Dir.GetFiles("*.txt", SearchOption.AllDirectories) Select x.FullName).ToArray
You might need to
Import System.IO
Import System.Linq
Basically your key for the requirement is SearchOption.AllDirectories which iterates through sub directories as well.
This should do it for you. It'll handle wildcards in directory part and filename part
Private Function GetFiles(ByVal Path As String) As List(Of String)
Dim drivePart As String, dirPart As String, filePart As String
drivePart = Path.Substring(0, Path.IndexOf("\") + 1)
dirPart = Path.Substring(Path.IndexOf("\") + 1, Path.LastIndexOf("\") - Path.IndexOf("\") - 1)
filePart = Path.Substring(Path.LastIndexOf("\") + 1)
Dim directories As New List(Of String)
Dim files As New List(Of String)
'' Walk directory tree finding matches
'' This should handle wildcards in any part of the path
Dim currentIndex As Integer = 0
Dim directoryMatch As String() = dirPart.Split("\")
For Each directory As String In directoryMatch
WalkDirectories(drivePart, directories, directoryMatch, currentIndex)
currentIndex += 1
Next
For Each directory As String In directories
files.AddRange(System.IO.Directory.GetFiles(directory, filePart))
Next
Return files
End Function
Private Sub WalkDirectories(ByVal dirPart As String, ByVal directories As List(Of String), ByVal directoryMatch As String(), ByVal currentIndex As Integer)
If currentIndex = directoryMatch.Length Then Return
For Each d As String In System.IO.Directory.GetDirectories(dirPart, directoryMatch(currentIndex))
directories.Add(d)
WalkDirectories(System.IO.Path.Combine(dirPart, d), directories, directoryMatch, currentIndex + 1)
Next
End Sub
Edit: just noticed that it wont handle UNC paths but it should be pretty easy to modify for that if you need to
Editted again to handle multiple directory levels and wildcards at multiple levels (eg C:\debug\12*\log1*\errors*.txt
Use the GetFiles from My.Computer.System and the ReadOnlyCollection(of String) from the system.collections.objectModel import and a searchoption as desired (top or all)
sPath = "C:\debug" ' your desired path
sFile1 = "t*.txt" ' your desired search pattern with * wildcard
sFile2 = "test?.txt" ' your desired search pattern with ? wildcard
dim lstFiles as system.collections.ObjectModel.ReadOnlyCollection(of String) = My.Computer.Filesystem.GetFiles(sPath, FileIO.SearchOption.SearchTopLevelOnly, sFile1)
'lstfiles contains all the files that match your selection
'if you really need an array you can convert the list to array here
dim i as integer = 0
for each sFile as string in lstfiles
a(i)=sfile
i+=1
next
You could use the 'Like' keyword:
' For your example, call this function with root = "C:\logs" and wild = "12*\*.log"
Friend Function GetMyFiles(root As String, wild As String, Optional allowsub As Boolean = True) As List(Of String)
Dim a As New List(Of String), pattern As String
' ensure root ends with a \
If Not root.EndsWith("\") Then root &= "\"
' the extra * allows for subdirectories in between, if required
pattern = root & If(allowsub, "*", "") & wild
For Each f As String In My.Computer.FileSystem.GetFiles(root, FileIO.SearchOption.SearchAllSubDirectories)
If f Like pattern Then a.Add(f)
Next
Return a
End Function
Of course, if you hit a protected system directory, it'll fail.
This function is just to demonstrate the 'Like' keyword.
It will work if 'root' isn't a drive root (e.g. C:).
Done properly, a separate function would collect directories first, each tested for access permissions in a Try/Catch block. Here's how that looks:
Friend Function GetAllAccessibleDirs(ByRef Dir As String, Optional inclDir As Boolean = True, Optional Sort As Boolean = False) As List(Of String)
Dim D As New List(Of String), Q As New Queue(Of String), dummy As DirectoryInfo, s As String
If inclDir Then D.Add(Dir)
Q.Enqueue(Dir)
While Q.Count
For Each s In GetTopLevelDirs(Q.Dequeue)
Try
dummy = My.Computer.FileSystem.GetDirectoryInfo(s)
D.Add(s)
Q.Enqueue(s)
Catch
' Inaccessible folder
End Try
Next
End While
If Sort AndAlso D.Count Then D.Sort()
Return D
End Function
Friend Function GetTopLevelDirs(ByRef dir As String) As List(Of String)
Try
Return My.Computer.FileSystem.GetDirectories(dir, FileIO.SearchOption.SearchTopLevelOnly).ToList
Catch
Return New List(Of String)
End Try
End Function