根据父级和父级目录重命名文件,并对多个文件进行顺序编号

时间:2019-05-21 11:03:50

标签: powershell copy rename sequential-number

我有一个文件,存储格式为

select year, genre, studio, title, revenue 
from (
    select year, genre, studio, title, revenue,
        dense_rank() over (partition by year order by genre) as genre_rank,
        dense_rank() over (partition by year, genre order by count_by_studio desc) as studio_rank,
        dense_rank() over (partition by year, genre, studio order by revenue_by_title desc) as title_rank
    from (
        select year,
            genre,
            studio,
            title,
            revenue,
            dense_rank() over (order by year desc) as year_rank,
            count(*) over (partition by year, genre, studio) as count_by_studio,
            sum(revenue) over (partition by year, genre, studio, title) as revenue_by_title
        from titles
    ) where year_rank <= 2
) where genre_rank <= 2
and studio_rank <= 2
and title_rank <= 2;

所需的输出是:

C:\Users\xx\test powershell script\studyID\number\filenamedfolder\example.jpg

#在“ filenamedfolder”中,jpg的每个实例的最后一位应按顺序编号。

我使用先前答案中给出的代码来查找,重命名文件并将其复制到根目录。

我正在使用Windows 10和Powershell:

studyID_number_1
Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      15063  1478

我尝试使用代码顺序命名文件,但是当'filenamedfolder'中存在多个jpg时,我的代码没有按顺序命名文件。相反,仅复制一个jpg并将其重命名为根文件夹。

我从来没有在Powershell中进行编码,而且我不确定自己做错了什么。我似乎找不到现有的答案。有人可以建议调整一下以使其正常工作吗?

1 个答案:

答案 0 :(得分:0)

当您要为每个目录重新编号时,我建议:

  • Set-Location个到BaseDir
  • 使用多个通配符进行Get-ChildItem,以保持相同的子目录深度
  • Directory分组,并且仅在该分组内重新编号

此示例树:

> tree /F
├───studyID1
│   ├───number1
│   │   └───filenamedfolder
│   │           example1.jpg
│   │           example2.jpg
│   │
│   └───number2
│       └───filenamedfolder
│               example1.jpg
│               example2.jpg
│
└───studyID2
    ├───number1
    │   └───filenamedfolder
    │           example1.jpg
    │           example2.jpg
    │
    └───number2
        └───filenamedfolder
                example1.jpg
                example2.jpg
                example3.jpg

由以下脚本处理:

## Q:\Test\2019\05\21\SO_56236909.ps1

$BaseDir = Join-Path $ENV:USERPROFILE "test powershell script"
Push-Location $BaseDir

Get-ChildItem -Path *\*\*\* -Filter *.jpg -File |
  Group-Object Directory | ForEach-Object {
    $Cnt=1
    $DirName = $_.Group[0].Directory.Name
    $Number  = $_.Group[0].Directory.Parent.Name
    $StudyID = $_.Group[0].Directory.Parent.Parent.Name
    $_.Group | Rename-Item -NewName {"{0}_{1}_{2}_{3}.jpg" -f `
                  $StudyID,$Number,$DirName,$Script:Cnt++ } -WhatIf
  }
Pop-Location

如果输出看起来正常,请删除-WhatIf

产生以下结果树:

> tree /F
├───studyID1
│   ├───number1
│   │   └───filenamedfolder
│   │           studyID1_number1_1.jpg
│   │           studyID1_number1_2.jpg
│   │
│   └───number2
│       └───filenamedfolder
│               studyID1_number2_1.jpg
│               studyID1_number2_2.jpg
│
└───studyID2
    ├───number1
    │   └───filenamedfolder
    │           studyID2_number1_1.jpg
    │           studyID2_number1_2.jpg
    │
    └───number2
        └───filenamedfolder
                studyID2_number2_1.jpg
                studyID2_number2_2.jpg
                studyID2_number2_3.jpg