以下代码可按要求工作,但在扩展名“ .txt”前紧跟一个多余的空格。我不明白为什么。
$source = 'C:\Users\xzy\Desktop\source\'
$dest = 'C:\Users\xzy\Desktop\dest\'
$mydate = (get-date).AddDays($DateOffset).Tostring('ddMMMyy')
Get-ChildItem -Path $source | Where {$_.Name -match "abc (\b\d{2}\b)"} | ForEach-Object {
Copy-Item -Path $_.FullName -Destination "$dest\$($_.BaseName,$mydate,$_.Extension)"
}
必需的文件名:abc 83 09Apr19.txt
当前结果:abc 83 09Apr19 .txt
答案 0 :(得分:0)
由于复制项接受管道输入,因此ForEach-Object
并不是必需的:
$DateOffset = -1
$source = 'C:\Users\xzy\Desktop\source\'
$dest = 'C:\Users\xzy\Desktop\dest\'
$mydate = (get-date).AddDays($DateOffset).Tostring('ddMMMyy')
Get-ChildItem -Path $source |
Where Name -match "abc (\b\d{2}\b)" |
Copy-Item -Destination {Join-Path $dest ("{0} {1}{2}" -f $_.BaseName,$mydate,$_.Extension)}
}