Powershell script to rename multiple files in subfolders - Stack Overflow

I try to create a little powershell script to rename multiple files named like babebibobu (ver.2.0).xls

I try to create a little powershell script to rename multiple files named like babebibobu (ver.2.0).xlsx in a folder and subfolders.

I want to remove the (ver.X.X) from each filename.

I tried this one:

Get-ChildItem -Path "c:\test" -Recurse -Include "* (ver*.*).*" | 
    Rename-Item -NewName { $_.Name -replace " (ver*.*)","" }

but filename becomes babebibobu ().xlsx.

I try to create a little powershell script to rename multiple files named like babebibobu (ver.2.0).xlsx in a folder and subfolders.

I want to remove the (ver.X.X) from each filename.

I tried this one:

Get-ChildItem -Path "c:\test" -Recurse -Include "* (ver*.*).*" | 
    Rename-Item -NewName { $_.Name -replace " (ver*.*)","" }

but filename becomes babebibobu ().xlsx.

Share Improve this question edited 14 hours ago Jan 9,8256 gold badges20 silver badges33 bronze badges asked 14 hours ago Kermit67Kermit67 232 bronze badges New contributor Kermit67 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 0
Add a comment  | 

1 Answer 1

Reset to default 1

the -replace uses regex for evaluating what needs to be replaced. (...) defines a group not parenthesis characters, which need to be escaped.

Moreover, the regex inside the group is not so good.

Try this, based on a (ver.2.0) versioning :

Get-ChildItem -Path "c:\test" -Recurse -Include "* (ver*.*).*" |
    Rename-Item -NewName { $_.Name -replace "\s\(ver\.\d+\.\d+\)","" }

if you may have (ver 2.0) syntax, then the correct regex can be \s\((\.|\s)\d+\.\d+\)

\s for any space char and (\.|\s) is a group to say a dot (\.) or (|) any space char (\s)
\(and \) for escaped parenthesis

\d for any digit between 0 and 9 + for once or more this will include version or minor version > 10

\. escaped dot because you want a dot and not any character but line feed (. stands for any char but LF)

Be aware that regex are case sensitive, so \sand \d need to be lower case (upper case will be the opposite, all but space and all but digit)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743755228a4501637.html

相关推荐

  • Powershell script to rename multiple files in subfolders - Stack Overflow

    I try to create a little powershell script to rename multiple files named like babebibobu (ver.2.0).xls

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信