-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveFilesToFolders.ps1
More file actions
155 lines (123 loc) · 4.74 KB
/
MoveFilesToFolders.ps1
File metadata and controls
155 lines (123 loc) · 4.74 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<#
.SYNOPSIS
将匹配正则表达式的文件按顺序移动到对应的文件夹中
.DESCRIPTION
此脚本会:
1. 在指定目录中查找所有匹配文件正则表达式的文件
2. 在指定目录中查找所有匹配文件夹正则表达式的文件夹
3. 按名称排序后,将第n个文件移动到第n个文件夹
.PARAMETER Path
工作目录路径(默认为当前目录)
.PARAMETER FilePattern
匹配文件的正则表达式
.PARAMETER FolderPattern
匹配文件夹的正则表达式
.PARAMETER WhatIf
预览模式,只显示将要执行的操作而不实际移动
.EXAMPLE
.\MoveFilesToFolders.ps1 -Path "D:\data" -FilePattern "^protocol_.*\.csv$" -FolderPattern "^\d{2}-\d{2}-\d{2}$"
.EXAMPLE
.\MoveFilesToFolders.ps1 -Path "D:\data" -FilePattern "\.csv$" -FolderPattern "^\d{2}-\d{2}-\d{2}$" -WhatIf
#>
param(
[Parameter(Mandatory=$false)]
[string]$Path = ".",
[Parameter(Mandatory=$true)]
[string]$FilePattern,
[Parameter(Mandatory=$true)]
[string]$FolderPattern,
[Parameter(Mandatory=$false)]
[switch]$WhatIf
)
# 解析为绝对路径
$Path = Resolve-Path $Path -ErrorAction Stop
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "文件移动脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "工作目录: $Path"
Write-Host "文件匹配模式: $FilePattern"
Write-Host "文件夹匹配模式: $FolderPattern"
Write-Host ""
# 获取匹配的文件(按名称排序)
$files = Get-ChildItem -Path $Path -File |
Where-Object { $_.Name -match $FilePattern } |
Sort-Object Name
# 获取匹配的文件夹(按名称排序)
$folders = Get-ChildItem -Path $Path -Directory |
Where-Object { $_.Name -match $FolderPattern } |
Sort-Object Name
Write-Host "找到 $($files.Count) 个匹配的文件:" -ForegroundColor Yellow
$files | ForEach-Object { Write-Host " - $($_.Name)" }
Write-Host ""
Write-Host "找到 $($folders.Count) 个匹配的文件夹:" -ForegroundColor Yellow
$folders | ForEach-Object { Write-Host " - $($_.Name)" }
Write-Host ""
# 检查数量是否匹配
if ($files.Count -eq 0) {
Write-Host "错误: 没有找到匹配的文件!" -ForegroundColor Red
exit 1
}
if ($folders.Count -eq 0) {
Write-Host "错误: 没有找到匹配的文件夹!" -ForegroundColor Red
exit 1
}
if ($files.Count -ne $folders.Count) {
Write-Host "警告: 文件数量 ($($files.Count)) 与文件夹数量 ($($folders.Count)) 不匹配!" -ForegroundColor Yellow
$minCount = [Math]::Min($files.Count, $folders.Count)
Write-Host "将只处理前 $minCount 对文件和文件夹。" -ForegroundColor Yellow
Write-Host ""
$response = Read-Host "是否继续?(Y/N)"
if ($response -ne "Y" -and $response -ne "y") {
Write-Host "操作已取消。" -ForegroundColor Yellow
exit 0
}
} else {
$minCount = $files.Count
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "移动计划:" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
# 显示移动计划
for ($i = 0; $i -lt $minCount; $i++) {
$file = $files[$i]
$folder = $folders[$i]
$destination = Join-Path $folder.FullName $file.Name
Write-Host "[$($i+1)] $($file.Name)" -ForegroundColor Green -NoNewline
Write-Host " -> " -NoNewline
Write-Host "$($folder.Name)\" -ForegroundColor Blue
}
Write-Host ""
if ($WhatIf) {
Write-Host "预览模式 (-WhatIf): 以上操作未实际执行。" -ForegroundColor Magenta
exit 0
}
# 确认执行
$response = Read-Host "确认执行以上移动操作?(Y/N)"
if ($response -ne "Y" -and $response -ne "y") {
Write-Host "操作已取消。" -ForegroundColor Yellow
exit 0
}
Write-Host ""
Write-Host "开始移动文件..." -ForegroundColor Cyan
# 执行移动
$successCount = 0
$failCount = 0
for ($i = 0; $i -lt $minCount; $i++) {
$file = $files[$i]
$folder = $folders[$i]
$destination = Join-Path $folder.FullName $file.Name
try {
Move-Item -Path $file.FullName -Destination $destination -ErrorAction Stop
Write-Host "[成功] $($file.Name) -> $($folder.Name)\" -ForegroundColor Green
$successCount++
}
catch {
Write-Host "[失败] $($file.Name): $($_.Exception.Message)" -ForegroundColor Red
$failCount++
}
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "完成!成功: $successCount, 失败: $failCount" -ForegroundColor $(if ($failCount -eq 0) { "Green" } else { "Yellow" })
Write-Host "========================================" -ForegroundColor Cyan