开发者

What is the regex for replacing "123456-name" pattern?

开发者 https://www.devze.com 2023-01-17 02:58 出处:网络
I am trying to rename multiple files in windows using powershell. And I want to rename replacing this pattern:

I am trying to rename multiple files in windows using powershell. And I want to rename replacing this pattern:

"123456-the_other_part_of_the_string". Example:

409873-doc1.txt
378234-doc2.txt
1230-doc3.txt

Basical开发者_如何学Pythonly I want to crop the numbers + '-' thing.


$variable -replace "^\d+-", ""


Get-ChildItem . *.txt | Where {$_.Name -match '^\d+-(.*)'} | 
                        Rename-Item -NewName {$matches[1]}

or with aliases:

gci . *.txt | ?{$_.Name -match '^\d+-(.*)'} | rni -new {$matches[1]}


[0-9]*?-[^.]*

I'd recommend you take some time to learn regex, though, instead of just using answers from SO. You will run into all sorts of unusual file naming that may throw your program off in the real world and you woln't be able to fix these issues without understanding regex.

EDIT: Not sure if you also want to remove the 'name' part. If not, use this instead:

[0-9]*?-
0

精彩评论

暂无评论...
验证码 换一张
取 消