Find characters and rename file name using Powersh

2019-05-17 20:35发布

问题:

I have a large number of files in a directory with this type of naming convention: "1050_14447_Letter Extension.pdf", etc. I need to remove all characters before the 2nd underscore (including the 2nd underscore). So the new file name would be "Letter Extension.pdf". How can I iterate the single directory renaming accordingly?

回答1:

Here's an example to rename all pdf files in the current directory, removing all leading numbers or underscores from the file name:

Get-ChildItem -Filter *.pdf | Rename-Item -NewName {$_.Name -replace '^[0-9_]+'}


回答2:

Here's another method that doesn't rely on regex but assumes no underscores in the filepath:

Get-ChildItem 'C:\path2files' | %{
    Rename-Item $_.fullname $_.Name.Split("_")[2]
    }


回答3:

Given a string like "1050_14447_Letter Extension.pdf", here is one way:

$foo = "1050_14447_Letter Extension.pdf"
$foo -replace '^.*_.*_(.*)$', '$1'


回答4:

This would handle any number of underscores in filename (ignoring the path) and skip everything up to and including the last underscore when generating new name:-

Get-ChildItem 'C:\somedir' -Filter *.pdf |`
%{Rename-Item $_.Fullname ($_.Name.Split("_")|`
select -last 1)}

You can test using the -whatif switch on the rename-item command e.g

gci 'R:\TECH' -Filter *.pdf|`
%{rename-item -whatif $_.Fullname ($_.Name.Split("_")|`
select -last 1)}

Returns:

What if: Performing operation "Rename File" on Target "Item: R:\TECH\38415_JOBS121121155535240.pdf Destin ation: R:\TECH\JOBS121121155535240.pdf". What if: Performing operation "Rename File" on Target "Item: R:\TECH\CHG38415_JOB_121112153717550.pdf Des tination: R:\TECH\121112153717550.pdf". What if: Performing operation "Rename File" on Target "Item: R:\TECH\CHG38415_PREH_121113152548740.pdf De stination: R:\TECH\121113152548740.pdf". What if: Performing operation "Rename File" on Target "Item: R:\TECH\CHG38415_PREI_121113152600640.pdf De stination: R:\TECH\121113152600640.pdf". What if: Performing operation "Rename File" on Target "Item: R:\TECH\MCPTEST121125121201390.pdf Destinati on: R:\TECH\MCPTEST121125121201390.pdf". What if: Performing operation "Rename File" on Target "Item: R:\TECH\PREHAND_38415_121121154127000.pdf De stination: R:\TECH\121121154127000.pdf". What if: Performing operation "Rename File" on Target "Item: R:\TECH\PREIMP_38415_121121154140630.pdf Des tination: R:\TECH\121121154140630.pdf".



回答5:

Here is another way using the automated tools of StringSolver. After installation, the mv command is augmented with semi-automated decision procedures.

You would first do the following:

mv "1050_14447_Letter Extension.pdf" "Letter Extension.pdf"

Then you would ask what would be the generalized transformation:

mv --explain

It would tell you something like ``

concatenates both file name and its extension starting at the first word separated by '.'

which is not yet exactly what you want. What you need is to give another example, such as:

mv --explain "25771_144_ 17Your file_name.pdf" " 17Your file_name.pdf"

the file name starting after the second '_' + the constant string '.' + the extension

So now you can do:

mv

And it will perform the transformation.

DISCLAIMER: I am a co-author of this work for academic purposes. Other examples are available on youtube.