Skip to content

Instantly share code, notes, and snippets.

@Stephanevg
Last active April 23, 2018 21:43
Show Gist options
  • Select an option

  • Save Stephanevg/2fdf0d8865463ab151901446d80acdbe to your computer and use it in GitHub Desktop.

Select an option

Save Stephanevg/2fdf0d8865463ab151901446d80acdbe to your computer and use it in GitHub Desktop.
Short code to get the folder depth of each child element from a particular node
Class FolderDepth{
[String]$Folder
[int]$level
FolderDepth([String]$Folder,[int]$Level){
$this.Folder = $Folder
$this.level = $Level
}
}
Class FolderInfo {
[System.IO.DirectoryInfo]$Root
[FolderDepth[]]$Folders
FolderInfo([System.IO.DirectoryInfo]$Root){
if (!($Root.Fullname.EndsWith("\"))){
$this.Root = $root.FullName + "\"
}else{
$this.Root = $Root.fullName
}
}
[FolderInfo]GetFolderDepth(){
$RootContent = Get-ChildItem -Path $this.Root
$objects = @()
Foreach($r in $RootContent){
$ci = Get-ChildItem $r.FullName -Recurse
if(!($ci)){
write-verbose "$($r.FullName) --> is of level 1"
$This.Folders += [FolderDepth]::New($r.FullName,1)
}
foreach($p in $ci){
#$p.fullNAme
#$p.FullName.Replace($Path,"").Split("\")
switch (($p.FullName.Replace($this.Root,"")).Split("\").count){
2 {
write-verbose "$($p.FullName) --> is of level 2"
$This.Folders += [FolderDepth]::New($p.FullName,2)
}
3 {
write-verbose "$($p.FullName) --> is of level 3"
$This.Folders += [FolderDepth]::New($p.FullName,3)
}
}
}
}
return $this
}
}
$Item = Get-Item "C:\Temp"
$Folder = [FolderInfo]$item
$Depth = $Folder.GetFolderDepth()
$Depth.Folders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment