您的位置:首页 > 运维架构 > Shell

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

2019-12-31 00:36 1246 查看

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

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 '\[.+\]','-')
}

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: