Powershell 将文件移动到其他文件夹位置
我正在尝试创建一个PowerShell脚本,根据文件名将文件列表移动到另一个文件夹中。文件名“Test-AMM-Report”的一个示例 原址 \\oltnas6uk\sd.test.com$\PROJECTS\Account Management\test AMM-报告 文件目的地 \\ammnasuk\rd.test-amm.com$\Business Management\Regular\ReportingPowershell 将文件移动到其他文件夹位置,powershell,file-location,Powershell,File Location,我正在尝试创建一个PowerShell脚本,根据文件名将文件列表移动到另一个文件夹中。文件名“Test-AMM-Report”的一个示例 原址 \\oltnas6uk\sd.test.com$\PROJECTS\Account Management\test AMM-报告 文件目的地 \\ammnasuk\rd.test-amm.com$\Business Management\Regular\Reporting 粗体突出显示了我可以用来确定文件将进入哪个位置的键盘。这样做非常简单 首先获取要移
粗体突出显示了我可以用来确定文件将进入哪个位置的键盘。这样做非常简单 首先获取要移动到名为$files的数组中的文件列表:
$files=get-childitem "\\oltnas6uk\sd.test.com$\PROJECTS\Account Management\"
然后使用foreach循环遍历数组,并在正则表达式模式下使用开关匹配文件上的模式并确定要执行的操作:
foreach ($f in $files) {
switch -regex ($f.Name) {
"AMM" {
try {
Move-Item -Path $f.FullName -Destination "\\ammnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" -Force
} catch {
Write-Host "Couldn't move file ($f.Name), error was $($_.Exception.Message)" -foregroundcolor red
}
break;
}
"BMM" {
try {
Move-Item -Path $f.FullName -Destination "\\bmmnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" -Force
} catch {
Write-Host "Couldn't move file ($f.Name), error was $($_.Exception.Message)" -foregroundcolor red
}
break;
}
}
}
}
我建议您要么在脚本中添加日志记录,要么花足够的时间确保模式匹配不会产生意外的结果。如果目标中有很多文件,还值得对所有预期匹配项进行比较,以查看有多少文件与任何规则都不匹配
$files=get-childitem "\\oltnas6uk\sd.test.com$\PROJECTS\Account Management\"
foreach ($f in $files) {
switch -regex ($f.Name) {
"AMM" {
try {
Move-Item -Path $f.FullName -Destination "\\ammnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" -Force
} catch {
Write-Host "Couldn't move file ($f.Name), error was $($_.Exception.Message)" -foregroundcolor red
}
break;
}
"BMM" {
try {
Move-Item -Path $f.FullName -Destination "\\bmmnasuk\rd.test-amm.com$\Business Management\Regular\Reporting" -Force
} catch {
Write-Host "Couldn't move file ($f.Name), error was $($_.Exception.Message)" -foregroundcolor red
}
break;
}
}
}