谁能告诉我他们是否认为这个 Powershell 脚本有问题。
Dir |
where {$_.extension -eq ".txt"} |
Rename-Item –NewName { $_.name –replace “.“,”-” }
对于当前目录中的每个文本文件,我试图用连字符替换文件名中的所有句点。提前致谢。
最佳答案
正如其他人所说,-replace
使用正则表达式(“.”是正则表达式中的特殊字符)。然而,他们的解决方案忘记了文件扩展名,并且他们正在删除它。前任。 “test.txt”变为“test-txt”(无扩展名)。更好的解决方案是:
dir -Filter *.txt | Rename-Item -NewName { $_.BaseName.replace(".","-") + $_.Extension }
这也使用 -Filter 来只挑选出以“.txt”结尾的文件,这比与 where 相比更快。
关于Powershell 更换周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541980/