I have a list of folders, where "C:\Source\hidden" and "C:\Source\hidden\hidden" are subFolder of "C:\Source" and "C:\Shit\hidden" and "C:\Shit\hidden\hidden" are subFolder of C:\Shit\", what i want is remove all subFolder from a lists and keep the folders "C:\Source\" and "C:\Shit\" only.
folder structure:
- C:\Source\
- C:\Source\hidden
- C:\Source\hidden\hidden
- C:\Shit\
- C:\Shit\hidden
- C:\Shit\hidden\hidden
Solution:
Module VBModule
Sub Main()
Dim dirs As System.Collections.Generic.List(Of String) = new System.Collections.Generic.List(Of String)
Dim res As System.Collections.Generic.List(Of String) = new System.Collections.Generic.List(Of String)
dirs.Add("C:\Source\")
dirs.Add("C:\Source\hidden")
dirs.Add("C:\Source\hidden\hidden")
dirs.Add("C:\Shit\")
dirs.Add("C:\Shit\hidden")
dirs.Add("C:\Shit\hidden\hidden")
for each dir as String in getRootFolders(dirs)
Console.WriteLine(dir)
Next
End Sub
private function getRootFolders(ByRef dirs As System.Collections.Generic.List(Of String))As System.Collections.Generic.List(Of String)
Dim res As System.Collections.Generic.List(Of String) = new System.Collections.Generic.List(Of String)
If dirs.Count = 0 then return res
dirs.sort()
res.add(dirs(0))
for i as integer = 1 To dirs.count - 1
Dim cur As String = dirs(i)
Dim prev As String = res(res.Count - 1)
Dim curLen As integer = cur.length
Dim prevLen As integer = prev.length
' Path.DirectorySeparatorChar = "\"
If curLen > prevLen AndAlso cur(prevLen-1) = "\" AndAlso prev.Equals(cur.subString(0, prevLen)) Then
continue For
Else
res.add(cur)
End if
Next
return res
End Function
End Module
C:\Shit\C:\Source\
No comments :
Post a Comment