【发布时间】:2017-03-13 20:46:09
【问题描述】:
我希望 PowerShell 检查文件是否存在于两个位置 - 源文件夹和目标文件夹。如果两者都存在,我希望它将"voided" 附加到目标文件,然后将源文件移动到目标位置。
我发现下面的脚本有效,但它重命名了源文件,而不是目标文件夹中的文件。我已经搜索并厌倦了反转脚本,但在所有情况下我只能让它重命名源文件。我错过了什么?
$src = "c:\Temp\Invoice"
$dest = "c:\Temp\test"
$v = "voided"
Get-ChildItem -Path $src -Filter *.pdf -Recurse | ForEach-Object {
$nextName = Join-Path -Path $dest -ChildPath $_.name
while (Test-Path -Path $nextName) {
$nextName = Join-Path $dest ($_.BaseName + "_$v" + $_.Extension)
}
$_ | Move-Item -Destination $nextName
}
【问题讨论】:
标签: powershell