-
Notifications
You must be signed in to change notification settings - Fork 0
/
folderTree.ps1
48 lines (40 loc) · 1.1 KB
/
folderTree.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
param(
[string]$root = ".",
[string[]]$exclusions = @(),
[switch]$foldersOnly = $false
)
# Check for unknown arguments
$validArgs = 'root', 'exclusions', 'foldersOnly'
$argsList = $PSBoundParameters.Keys
foreach ($arg in $argsList) {
if ($arg -notin $validArgs) {
Write-Error "Unknown argument: -$arg"
exit 1
}
}
if (-not (Test-Path $root)) {
Write-Error "The specified folder $root does not exist."
exit 1
}
function Print-FolderStructure {
param(
[string]$root,
[int]$level = 0,
[string[]]$exclusions,
[switch]$foldersOnly
)
$indent = " " * $level * 4
$items = Get-ChildItem -Path $root
foreach ($item in $items) {
if ($item.PSIsContainer) {
if ($item.Name -in $exclusions) {
continue
}
Write-Host ("{0}{1}/" -f $indent, $item.Name)
Print-FolderStructure -root $item.FullName -level ($level + 1) -exclusions $exclusions -foldersOnly:$foldersOnly
} elseif (-not $foldersOnly) {
Write-Host ("{0}{1}" -f $indent, $item.Name)
}
}
}
Print-FolderStructure -root $root -exclusions $exclusions -foldersOnly:$foldersOnly