如何更改一个文件夹及其子文件夹中多个文件的文件名中的某些字符(#、%、&、$)?
Ravi Thapliyal 回答了如何使用 PowerShell 执行此操作,但仅限于特定文件夹 ( Replace or delete certain characters from filenames of all files in a folder ) 中的文件。
我需要对所选文件夹中所有文件夹中的所有文件进行概括。
最佳答案
您自己的答案只会更改一个字符,这是一种一次性使用数组正则表达式的解决方案,从 $replace 中删除您想要保留的字符。
[char[]]$replace = '!@#$%^&*(){}[]":;,<>/|\+=`~ '''
$regex = ($replace | % {[regex]::Escape($_)}) -join '|'
Get-ChildItem -recurse |
ForEach {
if ($_.Name -match $RegEx){
Ren $_.Fullname -NewName $($_.Name -replace $RegEx, '_') -whatif
}
}
如果输出看起来正常,请删除 -whatif
编辑从 Get-ChildItem 中删除了 -File
选项,因为它需要最新的 Powershell 版本,但没有必要。
Edit2 我经常声明 Rename-Item
接受管道输入,因此这里有一个更直接的版本:
[char[]]$replace = '!@#$%^&*(){}[]":;,<>/|\+=`~ '''
$regex = ($replace | % {[regex]::Escape($_)}) -join '|'
Get-ChildItem -recurse |
Where-Object { $_.Name -match $RegEx} |
Rename-Item -NewName {$_.Name -replace $RegEx, '_'} -whatif
关于powershell - 替换文件夹及其子文件夹中所有文件的文件名中的某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40985612/