在下面的脚本中,由于方括号的原因,该行$comparisonFile=Get-Item ... \E")
不会替换文件$_
。如何才能有效地替换文件名中的文本?Q:\Backup Comparison\E\Files\file[1].txt
get-childitem -file -recurse | ForEach-Object {
#file is: Q:\Backup Comparison\E\Files\file[1].txt
$comparisonFile=Get-Item ($_.FullName -replace 'Q:\\Backup Comparison\\E',"V:\Q Dup Check Needed\E")
$comparisonFileSize=$comparisonFile.Length
$newPath=($_.FullName -replace 'Q:\\Backup Comparison\\E',"V:\Deleteable\Q Dup Check Needed\E")
$createNewPath=Split-Path $newPath -Parent
Write-Host $_.FullName # result is: Q:\Backup Comparison\E\Files\file[1].txt
Write-Host $comparisonFile # result is null
if ((Test-Path -LiteralPath $comparisonFile) && $_.Length -eq $comparisonFileSize) {
$comparisonHash=(Get-FileHash -algorithm md5 $comparisonFile).Hash
$sourceHash=((Get-FileHash -algorithm md5 $_).Hash)
if ($sourceHash -eq $comparisonHash) {
Write-Host $newPath
mkdir $createNewPath
mv $comparisonFile $newPath
Write-Host ""
}
}
}
答案1
问题与方括号有关。但是,它与New-Item
and有关mv
。我通过添加以下内容调整了这些行LiteralPath
:
$comparisonFile=Get-Item -LiteralPath ($_.FullName -replace 'Q:\\Backup Comparison\\E',"V:\Q Dup Check Needed\E")
Move-Item -LiteralPath $comparisonFile $newPath