I've been working on it for hours, but I can't find the solution. Probably easy, so a question in the round.
I have a list(of String) with hundreds of directorystring in it, like this:
"c:\temp\in",
"c:\temp\in - Kopie",
"c:\temp\in - Kopie\1",
"c:\temp\in - Kopie\1\yyy",
"c:\temp\in - Kopie\1\yyy\yyyyy",
"c:\temp\in - Kopie\2",
"c:\temp\in2",
"c:\temp\ipadb",
"c:\temp\out",
"c:\temp\out2",
"c:\temp\Processes2",
"c:\temp\Processes2\Kassenbelege",
"c:\temp\Processes2\Kassenbelege\images",
"c:\temp\Processes2\Posteingang",
"c:\temp\Processes2\Posteingang\images",
"c:\temp\Processes2\Rechnungen",
"c:\temp\Processes2\Rechnungen\images",
"c:\temp\Processes2\Rechnungen\images\backup",
"c:\temp\test"
I have a class that I want to fill from this list.
Public Class _Folder
Public Property Name As String
Public ReadOnly Property SubFolders As List(Of _Folder) = New List(Of _Folder)()
End Class
Any help is appreciated. Thank you.
This code should get you there.
Dim Folders As New List(Of String)({"c:\temp\in",
"c:\temp\in - Kopie",
"c:\temp\in - Kopie\1",
"c:\temp\in - Kopie\1\yyy",
"c:\temp\in - Kopie\1\yyy\yyyyy",
"c:\temp\in - Kopie\2",
"c:\temp\in2",
"c:\temp\ipadb",
"c:\temp\out",
"c:\temp\out2",
"c:\temp\Processes2",
"c:\temp\Processes2\Kassenbelege",
"c:\temp\Processes2\Kassenbelege\images",
"c:\temp\Processes2\Posteingang",
"c:\temp\Processes2\Posteingang\images",
"c:\temp\Processes2\Rechnungen",
"c:\temp\Processes2\Rechnungen\images",
"c:\temp\Processes2\Rechnungen\images\backup",
"c:\temp\test"})
Dim RootFolder As New _Folder
For Each f In Folders
Dim drive = f.Split(":")(0)
Dim path = f.Split(":")(1)
Dim pathSplit() As String = path.Split(System.IO.Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)
Dim currentFolder As _Folder = RootFolder
For Each level In pathSplit
Dim foundFolder = (From x In currentFolder.SubFolders Where x.Name = level Select x).FirstOrDefault
If foundFolder Is Nothing Then
foundFolder = New _Folder
foundFolder.Name = level
currentFolder.SubFolders.Add(foundFolder)
End If
currentFolder = foundFolder
Next
Next
thank you for your help. This was the final solution. I added some code to make it also usable for network shares and I added the folderpath in the class to acces this information while browsing in the treeview.
Private Sub lblFilter_TextChanged(sender As Object, e As TextChangedEventArgs) Handles lblFilter.TextChanged
Dim lstFolders As New List(Of _Folder)
Dim RootFolder As New _Folder
For Each f In _items
If f.Contains("\\") Then f = f.Replace("\\", ":")
Dim drive = f.Split(":")(0)
Dim path = f.Split(":")(1)
'If String.IsNullOrEmpty(drive) Then Continue For
RootFolder.Name = drive
Dim charSeparators As Char() = New Char() {System.IO.Path.DirectorySeparatorChar}
Dim pathSplit() As String = path.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)
Dim currentFolder As _Folder = RootFolder
For Each level In pathSplit
If String.IsNullOrEmpty(level) Then Continue For
Dim foundFolder = (From x In currentFolder.SubFolders Where x.Name = level Select x).FirstOrDefault
If foundFolder Is Nothing Then
foundFolder = New _Folder
foundFolder.Name = level
If f.StartsWith(":") Then 'Netzwerkpfad
foundFolder.FullPath = (f.Substring(0, f.IndexOf(level)) & level).Replace(":", "\\")
Else 'Pfad mit Laufwerksbuchstaben
foundFolder.FullPath = f.Substring(0, f.IndexOf(level)) & level
End If
currentFolder.SubFolders.Add(foundFolder)
End If
currentFolder = foundFolder
Next
Next
lstFolders.Add(RootFolder)
tvw.DataContext = lstFolders
End Sub
This class is filled:
Public Class _Folder
Public Property Name As String
Public Property FullPath As String
Public ReadOnly Property SubFolders As List(Of _Folder) = New List(Of _Folder)()
End Class
Related
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...
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
I have two folders say A and B having exactly same no .of files with same name but different modified date and size. I want to compare each file of A to its respective file of folder B and get the newly modified file in a List box. If newly modified file belongs to folder A then it will come to ListBox1 and if its belongs to Folder B, then comes to listbox2.
I have started the code but m struck..!! can anyone of you help me?
Dim PathA As String= "" 'path for folder A
Dim PathB As String= "" 'path for folder B
Dim Dir1 As New System.Io.DirectoryInfo(PathA)
Dim Dir2 As New System.Io.DirectoryInfo(PathB)
Dim List1 = Dir1.GetFiles(*.*,System.IO.SearchOption.AllDirectories)
Dim List2 = Dir2.GetFiles(*.*,System.IO.SearchOption.AllDirectories)
Dim myFileCompare As New fileCompare
Dim areIdentical As Boolean = List1.sequence Equals (List2,myFileCompare)
What to do now???
Need to implement a FileInfoComparer and then call Intersect enumerable extension
Sub Main
Dim PathA As String= "D:\temp" 'path for folder A
Dim PathB As String= "D:\temp2" 'path for folder B
Dim Dir1 As New System.Io.DirectoryInfo(PathA)
Dim Dir2 As New System.Io.DirectoryInfo(PathB)
Dim List1 = Dir1.GetFiles("*.*")
Dim List2 = Dir2.GetFiles("*.*")
Dim List3 = List1.Intersect(List2, new FileComparer)
For Each info in List3
Console.WriteLine(info.Name)
Next
End Sub
Public Class FileComparer
Implements IEqualityComparer(Of FileInfo)
Public Function Equals1(ByVal x As FileInfo, ByVal y As FileInfo) As Boolean _
Implements IEqualityComparer(Of FileInfo).Equals
If x Is y Then Return True
If x Is Nothing OrElse y Is Nothing Then Return False
Return (x.Name = y.Name) AndAlso (x.Length = y.Length) AndAlso (x.LastWriteTime = y.LastWriteTime)
End Function
Public Function GetHashCode1(ByVal info As FileInfo) As Integer _
Implements IEqualityComparer(Of FileInfo).GetHashCode
If info Is Nothing Then Return 0
Return info.Name.GetHashCode()
End Function
End Class
What is the fastest way to retrieve all the files within a directory (including sub folders). Currently I am using this function:
Public Function FindFiles(path As String, Recursive As Boolean) As Boolean
Dim dirInfo As New IO.DirectoryInfo(path)
Dim fileObject As FileSystemInfo
If Recursive = True Then
For Each fileObject In dirInfo.GetFileSystemInfos()
If System.IO.Directory.Exists(fileObject.FullName) Then
FindFiles(fileObject.FullName, Recursive)
Else
'fileObject.FullName - found file
End If
Next
Else
For Each fileObject In dirInfo.GetFileSystemInfos()
If Not System.IO.Directory.Exists(fileObject.FullName) Then
'fileObject.FullName - found file
End If
Next
End If
End Function
Thanks
try this
Private Function getAllFolders(ByVal directory As String) As String()
Dim fi As New IO.DirectoryInfo(directory) 'Create object
Dim path() As String = {} 'Array to store paths
'Loop through subfolders
For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
Array.Resize(path, path.Length + 1) 'Add this folders name
path(path.Length - 1) = subfolder.FullName
'Recall function with each subdirectory
For Each s As String In getAllFolders(subfolder.FullName)
Array.Resize(path, path.Length + 1)
path(path.Length - 1) = s
Next
Next
Return path
End Function
.net 3.5, VS 2010... this is for an asp.net website.
I have an class called Agency. there is a second class called Agency_Queries. Agency_Queries inhertis the Agency class. I'm trying to create a function that will copy the like properties in Agency to Agency_Queries. I figured out how to do that.. but when i try to make it more generic so that i can pass in my class name and lists i'm doing something wrong.
So if there is an list(of Agency) that needs to be copied to list(of Agency_Queries) i've got something like the following.
Dim AgencyS As List(Of Agency) = Nothing
Dim Oc As New Agency_Controller
AgencyS = Oc.GetAgencyData(0)
Dim AgencyQueriesS As New List(Of Agency_Queries)
Dim _itemProperties() As Reflection.PropertyInfo = AgencyS.Item(0).GetType.GetProperties()
For Each item In AgencyS
Dim X As NewAgency_Queries
_itemProperties = item.GetType().GetProperties()
For Each p As Reflection.PropertyInfo In _itemProperties
For Each s As Reflection.PropertyInfo In X.GetType().GetProperties()
If p.Name = s.Name Then
s.SetValue(X, p.GetValue(item, Nothing), Nothing)
End If
Next
Next
AgencyQueriesS.Add(X)
Next
the problem is when i go to make this generic by the Dim X as new Agency_Queries. How do i go about creating a new instance of the class in a generic sense. I need to have it be a new instance or each time it gets added to teh AgencyQueriesS list all the objects have the same data values.
Here is the generic version... not working
Shared Function CopyObject(ByVal inputList As Object, ByVal OutputClass As Object, ByVal outputList As Object) As Object
Dim _itemProperties() As Reflection.PropertyInfo = inputList.Item(0).GetType.GetProperties()
For Each item In inputList
Dim oClean As Object = OutputClass
For Each p As Reflection.PropertyInfo In _itemProperties
For Each s As Reflection.PropertyInfo In oClean.GetType().GetProperties()
If p.Name = s.Name Then
s.SetValue(oClean, p.GetValue(item, Nothing), Nothing)
End If
Next
Next
outputList.Add(oClean)
Next
Return outputList
End Function
thanks
shannon
I took a little of this and that and came up with this:
Imports System.Reflection
Public Class ObjectHelper
' Creates a copy of an object
Public Shared Function GetCopy(Of SourceType As {Class, New})(ByVal Source As SourceType) As SourceType
Dim ReturnValue As New SourceType
Dim sourceProperties() As PropertyInfo = Source.GetType().GetProperties()
For Each sourceProp As PropertyInfo In sourceProperties
sourceProp.SetValue(ReturnValue, sourceProp.GetValue(Source, Nothing), Nothing)
Next
Return ReturnValue
End Function
End Class
I use the following:
<Extension>
Public Sub CopyPropertiesByName(Of T1, T2)(dest As T1, src As T2)
Dim srcProps = src.GetType().GetProperties()
Dim destProps = dest.GetType().GetProperties()
For Each loSrcProp In srcProps
If loSrcProp.CanRead Then
Dim loDestProp = destProps.FirstOrDefault(Function(x) x.Name = loSrcProp.Name)
If loDestProp IsNot Nothing AndAlso loDestProp.CanWrite Then
Dim loVal = loSrcProp.GetValue(src, Nothing)
loDestProp.SetValue(dest, loVal, Nothing)
End If
End If
Next
End Sub