-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint-tree.ps1
More file actions
54 lines (43 loc) · 1.43 KB
/
print-tree.ps1
File metadata and controls
54 lines (43 loc) · 1.43 KB
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
49
50
51
52
53
54
<#
.SYNOPSIS
Prints the folder and file structure in a tree-like format.
.DESCRIPTION
Displays the directory structure recursively in a tree-like layout.
When the -NoHidden switch is used, files and folders starting with '.' or '_'
are excluded from output.
#>
param(
[Parameter(Mandatory = $true)]
[string]$Path,
[switch]$NoHidden
)
function Show-Tree {
param(
[string]$Path,
[string]$Prefix = "",
[switch]$NoHidden
)
# Retrieve children
$folders = Get-ChildItem -Path $Path -Directory -ErrorAction SilentlyContinue
$files = Get-ChildItem -Path $Path -File -ErrorAction SilentlyContinue
# Apply NoHidden filter
if ($NoHidden) {
$folders = $folders | Where-Object { $_.Name -notmatch '^[._]' }
$files = $files | Where-Object { $_.Name -notmatch '^[._]' }
}
# Print children (but NOT the folder name again — parent already printed it)
if ($folders.Count -gt 0 -or $files.Count -gt 0) {
Write-Output "$Prefix|"
}
foreach ($folder in $folders) {
Write-Output "$Prefix|----$($folder.Name)"
Show-Tree -Path $folder.FullName -Prefix "$Prefix| " -NoHidden:$NoHidden
}
foreach ($file in $files) {
Write-Output "$Prefix|----$($file.Name)"
}
}
# Print the root folder name ONCE
$resolved = Resolve-Path $Path
Write-Output (Split-Path $resolved -Leaf)
Show-Tree -Path $resolved -NoHidden:$NoHidden