通过将字符串从文件名的后面移动到前面来重命名文件名

Renaming a filename by moving string from the back of the filename to the front

我正在尝试通过将字符串从文件名后面移到前面来重命名文件名:
PM - Prebills_10Aug20_Project _ 3359122596 H 至: 3359122596 下午 - Prebills_10Aug20_

我正在尝试使用此脚本,然后删除 'Project' 但无法正常工作:

Get-ChildItem *.pdf | Rename-Item -NewName { 
$_.Name -replace '(Project\d{*}) - (.*?)\.pdf$', ' - 
.pdf' } -WhatIf

将此调整到您的代码中:

Clear-Host
$FName = "PM - Prebills_10Aug20_Project_3359122596 H"
#-------- Start Here -------
$Parts = $FName.Split("_")
$FName = $Parts[3] + " " + $Parts[0] + "_" + $Parts[1]
#-------- End Here -----
$FName

结果:3359122596 H PM - Prebills_10Aug20

它抑制了尾部下划线,如果需要,可以重新添加。您还可以轻松删除 Prebills 和日期之间的下划线,并将其替换为 space.

HTH

我会这样做:

 foreach($file in (Get-ChildItem *.pdf )){
        $temp = $file -split '_'
        $newname= $temp[3] + " " + $temp[1] + "_" + $temp[2] + "_"
        Rename-item "$file" -NewName "$newname"
    }

这行得通。我不知道这种模式必须有多普遍。查看 https://regex101.com 以试用正则表达式。

Get-ChildItem *.pdf | 
  Rename-Item -NewName { $_.Name -replace '(.*)Project _ (\d{10} H)', ' ' } -WhatIf

What if: Performing the operation "Rename File" on target 
"Item:       C:\users\admin\PM - Prebills_10Aug20_Project _ 3359122596 H.pdf 
Destination: C:\users\admin59122596 H PM - Prebills_10Aug20_.pdf".