Powershell 更换周期

标签 powershell powershell-2.0

谁能告诉我他们是否认为这个 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/

相关文章:

powershell - 如何从不同位置删除超过 X 天的多个文件?

powershell - 添加 DhcpServerv4Reservation FullyQualifiedErrorId : WIN32 1753

powershell - 使用Powershell将信息发送到网站而无需打开Web浏览器

powershell - 如何使用powershell导入证书

POWERSHELL:$Image.DrawString 居中字符串,左右填充

php - 从 php 脚本解密在 powershell 中使用 Rijndael 方法创建的文本

PowerShell:查找确切的文件夹名称

powershell - Powershell脚本重新索引WSUS DB和电子邮件结果

powershell - 无法比较创建日期和当前日期

Powershell - 用户输入的数组范围