Powershell 批量重命名文件中含有 [] 导致报错

下载的所有文件中都包含了 “[下载网站地址]”, 按照常规方法

Get-ChildItem "D:\Bluey\" -Recurse |ForEach-Object{Rename-Item -Path $_.FullName -NewName $_.FullName.Replace('old','new')}

一直报告无法发现源文件,查阅后得知当文件名中包含特殊字符,需要使用 -LiteralPath 参数。

Get-ChildItem "D:\Bluey\" -Recurse |
Where-Object {$_.Name -match '\[.+\]' } |
foreach {
    Rename-Item -LiteralPath $_.FullName -NewName $($_.Name -replace '\[.+\]','-')
}

将[]和其间字符替换为-。

猜你喜欢

转载自blog.51cto.com/41084/2463237