【发布时间】:2015-10-14 00:10:05
【问题描述】:
我有一个目录,其中包含许多文件名格式错误的文件。其中一些确实在文件名的末尾有“空格”。其他人在文件名字符串末尾的文件名中包含一些关键字。例如“xxx xxx xxx somewordEng .txt”
我试图用这个脚本摆脱它们,但它还不会。文件名(Basename)末尾的空格仍然存在,“Eng”关键字以某种方式添加到之前的单词中:
dir | Rename-Item -NewName { $_.BaseName.replace("Eng$","").replace(" {2,}"," ").replace("\s$","") + $_.Extension }
.replace("Eng$","") is supposed to remove the "Eng" keyword if it appears at the END of the filename (basename), seems not working so far.
.replace(" {2,}"," ") is supposed to replace 2 or more following spaces with just ONE space within the filename, seems not working so far.
.replace("\s$","") is supposed to remove spaces at the end of the filename, does not work neither.
我搜索了 powershell 正则表达式示例,但到目前为止对我来说似乎没有任何效果。 :( 还看不到问题。
【问题讨论】:
-
.replace不是-replace后者支持正则表达式。前者是简单的通配符。 -
我想补充一点,你完全可以使用
.Trim()方法来做到这一点。
标签: regex windows powershell scripting