【问题标题】:Renaming a filename by moving string from the back of the filename to the front通过将字符串从文件名的后面移动到前面来重命名文件名
【发布时间】:2020-12-06 00:30:30
【问题描述】:

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

我正在尝试使用此脚本,然后删除“项目”,但无法使其工作:

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

【问题讨论】:

  • 请改进示例的格式,例如与backticks

标签: powershell rename filenames


【解决方案1】:

将此调整到您的代码中:

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 和日期之间的下划线并用空格替换它。

HTH

【讨论】:

    【解决方案2】:

    我会这样做:

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

    【讨论】:

      【解决方案3】:

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

      Get-ChildItem *.pdf | 
        Rename-Item -NewName { $_.Name -replace '(.*)Project _ (\d{10} H)', '$2 $1' } -WhatIf
      
      What if: Performing the operation "Rename File" on target 
      "Item:       C:\users\admin\PM - Prebills_10Aug20_Project _ 3359122596 H.pdf 
      Destination: C:\users\admin\3359122596 H PM - Prebills_10Aug20_.pdf".
      

      【讨论】:

        猜你喜欢
        • 2018-04-10
        • 2019-07-29
        • 2021-04-02
        • 2021-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-27
        • 2019-07-21
        相关资源
        最近更新 更多