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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
$script:VerboseOutput = $false
function Get-FolderTree { [CmdletBinding()] param( [string]$RootPath = "source/_posts", [switch]$Silent = $false ) if (-not (Test-Path $RootPath)) { Write-Error "根路径不存在: $RootPath" return $null } $rootFullPath = (Get-Item $RootPath).FullName if (-not $rootFullPath.EndsWith([System.IO.Path]::DirectorySeparatorChar)) { $rootFullPath += [System.IO.Path]::DirectorySeparatorChar } if($script:VerboseOutput){ Write-Verbose "扫描目录: $rootFullPath" } $allFolders = @() try { $folders = Get-ChildItem -Path $rootFullPath -Directory -Recurse -ErrorAction Stop foreach ($folder in $folders) { $folderFullPath = $folder.FullName $relativePath = [System.IO.Path]::GetRelativePath($rootFullPath, $folderFullPath) if ($relativePath -ne ".") { $relativePath = $relativePath.Replace("\", "/") $allFolders += $relativePath } } if($script:VerboseOutput){ Write-Verbose "找到 $($allFolders.Count) 个文件夹" } $treeObject = [PSCustomObject]@{ AllFolders = $allFolders RootPath = $RootPath RootFullPath = $rootFullPath } $treeObject | Add-Member -MemberType ScriptMethod -Name Exists -Value { param([string]$RelativePath) $this.AllFolders -contains $RelativePath } $treeObject | Add-Member -MemberType ScriptMethod -Name Find -Value { param([string]$Pattern) $this.AllFolders | Where-Object { $_ -like "*$Pattern*" } } $treeObject | Add-Member -MemberType ScriptMethod -Name GetSubfolders -Value { param([string]$ParentPath) if (-not $ParentPath) { return $this.AllFolders | Where-Object { $_ -notmatch "/" } } $this.AllFolders | Where-Object { $_ -like "$ParentPath/*" -and $_ -ne $ParentPath } } return $treeObject } catch { Write-Error "扫描目录时出错: $_" return $null } }
function Show-FolderTree { [CmdletBinding()] param( [Parameter(Position=0)] [string]$RootPath = "source/_posts", [switch]$Silent = $false, [switch]$ShowSample = $false, [int]$SampleCount = 5 ) $script:VerboseOutput = -not $Silent
$tree = Get-FolderTree -RootPath $RootPath if ($tree) { if($script:VerboseOutput){ Write-Host "📁 扫描目录: $($tree.RootFullPath)" -ForegroundColor Gray Write-Host "📊 找到 $($tree.AllFolders.Count) 个文件夹" -ForegroundColor Green }
if ($ShowSample -and $tree.AllFolders.Count -gt 0) { Write-Host "📋 示例文件夹 ($SampleCount 个):" -ForegroundColor Gray $tree.AllFolders | Select-Object -First $SampleCount | ForEach-Object { Write-Host " • $_" -ForegroundColor DarkGray } if ($tree.AllFolders.Count -gt $SampleCount) { Write-Host " ... 还有 $($tree.AllFolders.Count - $SampleCount) 个" -ForegroundColor DarkGray } } return $tree } return $null }
Export-ModuleMember -Function ` Get-FolderTree, Show-FolderTree
|