文件夹扫描模块

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

# ============================================
# 《意识之道》智能文章创建脚本
# 作者:道祖
# 版本:v1
# 日期:2026-02-04 00:10
# 更新日期:2026-02-04 00:10
# ============================================
# 文件夹扫描模块
# 功能:扫描指定目录并返回文件夹树结构


$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