PowerShell - 使用计数器和重命名文件

问题描述 投票:0回答:3

请帮忙解决以下情况:

有很多照片需要根据目录文件夹的名称重命名,它们所在的位置+在每个新名称的末尾添加索引,格式为PHOTO1000_NAMEA_1,PHOTO1001_NAMEA_2,PHOTO1002_NAMEB_1,PHOTO1003_NAMEB_2,PHOTO1004_NAMEC_1,PHOTO1005_NAMEC_2等

地点:

a) PHOTO1000 (1001,1002) = 重命名照片总数计数器; b) _1 (_2,_3) = 项目具体新名称的索引号。

在我的 a) 部分代码版本中,计数器没有计算“1000”中的项目,i++。

$count=1000
Get-ChildItem -Recurse -Include *.jpg | ForEach-Object -Process { Rename-Item -LiteralPath $_ -NewName ("PHOTO_{0}_{1}{2}" -f $script:count, $_.Directory.Name,$_.Extension) -WhatIf }

请帮助正确的代码格式。

谢谢!!

powershell syntax count
3个回答
0
投票

我已经提供了一个解决方案,因为我理解你的问题。 如果对所提供的内容感到满意,请确保使用投票机制标记正确答案。

# Set the path to the directory containing the photos to rename
$dirPath = "C:\Temp\Photo"

$counter = 1000

# Get all directories in the specified path
$dirs = Get-ChildItem -Path $dirPath -Directory -Recurse

# Loop through each directory
foreach ($dir in $dirs) {
    # Get all files in the current directory
    $files = Get-ChildItem -Path $dir.FullName -File -Filter '*.jpg'

    # Initialize a counter for the current file index
    $index = 1

    # Loop through each file in the current directory
    foreach ($file in $files) {
        # Build the new file name
        # ("PHOTO_{0}_{1}{2}" -f $script:count, $_.Directory.Name,$_.Extension)
        if ($file.BaseName.StartsWith('PHOTO')) {
            Write-Warning ('File {0} has alread been renamed' -f $file.FullName)
            Continue
        }
        $newName ='PHOTO{0}_{1}_{2}{3}' -f  $counter,$file.BaseName,$index, $file.Extension

        # Rename the file
        Rename-Item -Path $file.FullName -NewName $newName

        # Increment the file index
        $index++
        $counter++
    }
}

0
投票

您可以使用两个 hashtables 结合 delay-bind 脚本块 来实现您的任务:

$totalCount  = @{ value = 0 } # count of renamed files overall
$countPerDir = @{ } # count of renamed files per directory name
Get-ChildItem -Recurse -Include *.jpg | 
  Rename-Item -NewName {
    'PHOTO_{0}_{1}_{2}_{3}' -f ++$totalCount.value, 
                               $_.Directory.Name, 
                               ++$countPerDir[$_.Directory.Name], 
                               $_.Extension 
  } -WhatIf

注意:上面命令中的

-WhatIf
常用参数previews操作。删除
-WhatIf
并在您确定操作将执行您想要的操作后重新执行。

注:

  • 通过使用

    Rename-Item
    ,您实际上是在就地重命名文件,而不是在单个平面目标目录中;如果您想要后者,请使用
    Move-Item
    及其
    -Destination
    参数。

  • $totalCount
    是哈希表而不是简单的
    [int]
    变量的唯一原因是延迟绑定脚本块在调用者的 child 范围内运行(不同于
    ForEach-Object
    Where-Object
    脚本块),并且从子范围引用哈希表可以跨范围边界更新其条目。

  • $countPerDir
    哈希表跟踪每个目录名称 的任何文件是如何被重命名的。 ++$countPerDir[$_.Directory.Name]
    利用了两种行为:

      分配给以前不存在的条目隐式
    • creates这样的条目(使用给定的键)。
    • ++
       应用于刚刚创建的新条目有效地对待它像 
      0
       并返回 
      1
      .

0
投票
我没看到你打电话给$counter。你启动它,但不要在 foreach 中再次调用它。

你有 2 个计数器 - 所有照片的总数,然后是基于 NAME_ 的索引,它看起来像目录名称。

第一个计数器很简单——只需在每次迭代后添加 $counter++。 $index 只需要基于目录 - 可能最简单的方法是将目录中的所有内容分组,然后 $index++

看-我的答案

$jpgs = Get-ChildItem -Recurse -Include *.jpg | group -Property Directory foreach ($group in $jpgs) { $index = 1 foreach ($item in $group.Group) { rename-item -LiteralPath $item -NewName ("PHOTO{0}_{1}_{2}{3}" -f $counter, $item.Directory.Name,$index ,$item.Extension) - WhatIf;$counter++;$index++}}
看-我的结果

Performing the operation "Rename File" on target "Item: C:\Users\Doco\Downloads\20220721_140129.jpg Destination: C:\Users\Doco\Downloads\PHOTO1000_Downloads_1.jpg". What if: Performing the operation "Rename File" on target "Item: C:\Users\Doco\Downloads\IMG_20220720_211337_720.jpg Destination: C:\Users\Doco\Downloads\PHOTO1001_Downloads_2.jpg". What if: Performing the operation "Rename File" on target "Item: C:\Users\Doco\Downloads\IMG_20220720_211337_773.jpg Destination: C:\Users\Doco\Downloads\PHOTO1002_Downloads_3.jpg". What if: Performing the operation "Rename File" on target "Item: C:\Users\Doco\Downloads\IMG_20220721_203852_286.jpg Destination: C:\Users\Doco\Downloads\PHOTO1003_Downloads_4.jpg". What if: Performing the operation "Rename File" on target "Item: C:\Users\Doco\OneDrive\Desktop\cora 7 birthday invite.jpg Destination: C:\Users\Doco\OneDrive\Desktop\PHOTO1004_Desktop_1.jpg". What if: Performing the operation "Rename File" on target "Item: C:\Users\Doco\OneDrive\Desktop\sedona.jpg Destination: C:\Users\Doco\OneDrive\Desktop\PHOTO1005_Desktop_2.jpg".
现在只需删除-whatif。显然

© www.soinside.com 2019 - 2024. All rights reserved.