【问题标题】:Rename the specific files in folder: Changing only first numbers重命名文件夹中的特定文件:仅更改第一个数字
【发布时间】:2021-04-10 06:24:06
【问题描述】:

在文件夹中,我有不同类型的文件。我需要更改特定类型文件的编号。 其中一些看起来像:01 sasasasa; 02 爸爸等 我需要在前两位数上加 5。例:06 筱筱; 07爸爸爸爸... 但 在其他文件夹中重命名的项目看起来像 1 bububu ; 2木木木等 需要加 5 并在第一位加一个 0。例如:06 布布布; 07 mumumu...当我达到 10+ 时,它不能是:010。

试过这个简单的方法:

Get-ChildItem -Path E:\bongbong -Filter *.txt |
%{
Rename-Item -NewName { $_.Name -replace '01','06' } |
Rename-Item -NewName { $_.Name -replace '02','07' } |
Rename-Item -NewName { $_.Name -replace '03','08' } |
Rename-Item -NewName { $_.Name -replace '04','09' } |
Rename-Item -NewName { $_.Name -replace '05','10' }
}

它不适用于第一个示例。在 powershell 中找不到类似 -if 的东西。

【问题讨论】:

    标签: powershell rename


    【解决方案1】:

    其他解决方案:

    Get-ChildItem "E:\bongbong" -file -Filter "*.txt" | where Name -match "\d{1,2} ." | %{
    #split by space
    $Array=$_.ToString().Split(' ')
    
    #take number and add 5
    $Number=[int]$Array[0] + 5
    
    #take part of name without number
    $PartRight=$Array[1..$Array.Count] -join ' '
    
    #build new name
    $NewName="0{0} {1}" -f $Number, $PartRight
    
    #rename file
    Rename-Item $_.FullName -NewName $NewName -WhatIf
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试更动态的方法:

      (Get-ChildItem -Path 'E:\bongbong' -Filter '*.txt' -File) | 
          Where-Object { $_.Name -match '^(\d{1,2})(.*)' } |
          Rename-Item -NewName {'{0:D2}{1}' -f ([int]$Matches[1] + 5), $Matches[2]}
      

      【讨论】:

      • 它正在使用 1 的编号; 2; 3 等当我在 01 上运行它时; 02 ; 03 一切正常,但 01 到 11。但是,谢谢!
      猜你喜欢
      • 2018-07-17
      • 2021-10-21
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      相关资源
      最近更新 更多