使用Regex / Powershell重命名文件

时间:2021-02-23 10:33:34

I'm learning Regex but I don't know how to rename the following files to ones I want yet. Can you help me.

我正在学习正则表达式,但我不知道如何将以下文件重命名为我想要的文件。你可以帮我吗。

BTW, I find it is really useful to using Powershell to rename files, and it can accept Regex.

顺便说一句,我发现使用Powershell重命名文件非常有用,它可以接受正则表达式。

filename__Accelerated_C____Practical_Programming_by_Example.chm -> C Practical Programming by Example.chm

filename__Accelerated_C ____ Practical_Programming_by_Example.chm - > C Example.chm实用编程

filename__Python_Essential_Reference__2nd_Edition_.pdf -> Python Essential Reference 2nd Edition.pdf

filename__Python_Essential_Reference__2nd_Edition_.pdf - > Python Essential Reference 2nd Edition.pdf

filename__Text_Processing_in_Python.chm -> Text Processing in Python.chm

filename__Text_Processing_in_Python.chm - > Python.chm中的文本处理

I'm also including some free online Regex tools that I enjoy to use most, might be useful to others.

我还包括一些我最喜欢使用的免费在线Regex工具,可能对其他人有用。

http://gskinner.com/RegExr/

http://gskinner.com/RegExr/

http://www.rubular.com/

http://www.rubular.com/

and the cheatsheet

和cheatsheet

http://krijnhoetmer.nl/stuff/regex/cheat-sheet/

http://krijnhoetmer.nl/stuff/regex/cheat-sheet/

3 个解决方案

#1


15  

This should work:

这应该工作:

ls | %{ ren $_ $(($_.name -replace '^filename_+','') -replace '_+',' ') }

#2


28  

Try this one:

试试这个:

Get-ChildItem directory | Rename-Item -NewName { $_.Name -replace '^filename_+','' -replace '_+',' ' }

Note that I just pipe the objects to Rename-Item, it is not really needed to do it via Foreach-Object (alias is %).

请注意,我只是将对象传递给Rename-Item,实际上不需要通过Foreach-Object(别名为%)来实现。

Update

更新

I haven't anything documented about the 'magic' with scriptblocks. If I remember correctly, it can be used if the property is ValueFromPipelineByPropertyName=$true:

我没有任何关于脚本块的'魔术'的记录。如果我没记错的话,如果属性为ValueFromPipelineByPropertyName = $ true,则可以使用它:

function x{
    param(
        [Parameter(ValueFromPipeline=$true)]$o,
        [Parameter(ValueFromPipelineByPropertyName=$true)][string]$prefix)
    process {
        write-host $prefix $o
    }
}
gci d:\ | select -fir 10 | x -prefix { $_.LastWriteTime }

#3


0  

Try this out:

试试这个:

$filename = $filename -creplace '_+\.', '.'
$filename = $filename -creplace '_+', ' '

#1


15  

This should work:

这应该工作:

ls | %{ ren $_ $(($_.name -replace '^filename_+','') -replace '_+',' ') }

#2


28  

Try this one:

试试这个:

Get-ChildItem directory | Rename-Item -NewName { $_.Name -replace '^filename_+','' -replace '_+',' ' }

Note that I just pipe the objects to Rename-Item, it is not really needed to do it via Foreach-Object (alias is %).

请注意,我只是将对象传递给Rename-Item,实际上不需要通过Foreach-Object(别名为%)来实现。

Update

更新

I haven't anything documented about the 'magic' with scriptblocks. If I remember correctly, it can be used if the property is ValueFromPipelineByPropertyName=$true:

我没有任何关于脚本块的'魔术'的记录。如果我没记错的话,如果属性为ValueFromPipelineByPropertyName = $ true,则可以使用它:

function x{
    param(
        [Parameter(ValueFromPipeline=$true)]$o,
        [Parameter(ValueFromPipelineByPropertyName=$true)][string]$prefix)
    process {
        write-host $prefix $o
    }
}
gci d:\ | select -fir 10 | x -prefix { $_.LastWriteTime }

#3


0  

Try this out:

试试这个:

$filename = $filename -creplace '_+\.', '.'
$filename = $filename -creplace '_+', ' '