只保留最新的 .var 文件及其相关文件

huangapple go评论133阅读模式
英文:

Only keep the latest .var and it's associated files

问题

AcidBubbles.Animations_Presets.1.jpg
AcidBubbles.BlendShapes.2.var
AcidBubbles.ChloeTouch.1.jpg
AcidBubbles.ColliderEditor.38.var
AcidBubbles.ColliderTuner.13.jpg
Acidbubbles.Cornwall.2.jpg
AcidBubbles.Cornwall.2.var
AcidBubbles.Embody.58.var
AcidBubbles.FloatingUIComponents.3.jpg
AcidBubbles.Glance.20.var
AcidBubbles.RubberLeash.2.jpg
AcidBubbles.RubberLeash.4.var
AcidBubbles.Timeline.200.jpg
AcidBubbles.Timeline.280.var
AcidBubbles.Utilities.6.var
英文:

i have a lot of files in subfolders with names like

\dave\dave.look1.1.var
\dave\dave.look1.1.jpg
\dave\dave.look1.1.txt
\dave\dave.look1.2.var
\dave\dave.look1.2.jpg
\dave\dave.look1.3.var
\dave\dave.look1.3.jpg
\dave\dave.look2.1.var
\dave\dave.look3.1.var

\bert\bert.look1.1.var
\bert\bert.look1.2.var
\bert\bert.look1.2.png
\bert\bert.look1.4.var

\fred\fred.look1.1.var
\fred\fred.look1.1.jpg
\fred\fred.look1.2.var
\fred\fred.look1.3.var
\fred\fred.look3.4.var
\fred\fred.look3.4.jpg
\fred\fred.look3.5.var
\fred\fred.look3.5.jpg

The end result should be something like this

\dave\dave.look1.3.var
\dave\dave.look1.3.jpg
\dave\dave.look2.1.var
\dave\dave.look3.1.var

\bert\bert.look1.4.var

\fred\fred.look1.3.var
\fred\fred.look3.5.var
\fred\fred.look3.5.jpg

where the digit is the version number but I only want to keep the latest .var (and it's associated .txt, .jpg or .png (may be others) with the highest number and delete the others in powershell.

So far I have the following which fails on the remove-item because the passed name no longer contains the version number?

# Set the path to the main directory containing the subdirectories
$mainDirectoryPath = "E:\Install\test\_New"

# Get a list of all subdirectories in the main directory
$subDirectories = Get-ChildItem -Path $mainDirectoryPath -Directory -Recurse
Write-Host "Found $($files.Count) files in $subDirectories"

# Loop through each subdirectory
foreach ($subDirectory in $subDirectories) {
    # Get a list of all files in the subdirectory
    $files = Get-ChildItem -Path $subDirectory.FullName -File

    # Group the files by the name before the version number
    $groupedFiles = $files | Group-Object { $_.Name.Split('.')[0,1] -join '.' }

    # For each group of files, keep only the file with the highest version number
    foreach ($group in $groupedFiles) {
        Write-Host "Processing $($group.Count) files for $($group.Name)"
        $group | Sort-Object { [int]$_.Name.Split('.')[2] } -Descending | Select-Object -Skip 1 | Remove-Item -Force -whatif
    }

@theo -
No that doesn't give me what i'm looking for:

Running against the following files -

AcidBubbles.Animations_Presets.1.jpg
AcidBubbles.BlendShapes.1.var
AcidBubbles.BlendShapes.2.var
AcidBubbles.ChloeTouch.1.jpg
AcidBubbles.ColliderEditor.36.var
AcidBubbles.ColliderEditor.37.var
AcidBubbles.ColliderEditor.38.var
AcidBubbles.ColliderTuner.13.jpg
Acidbubbles.Cornwall.2.jpg
AcidBubbles.Cornwall.2.var
AcidBubbles.Embody.53.var
AcidBubbles.Embody.57.var
AcidBubbles.Embody.58.var
AcidBubbles.FloatingUIComponents.3.jpg
AcidBubbles.Glance.11.var
AcidBubbles.Glance.16.var
AcidBubbles.Glance.19.var
AcidBubbles.Glance.20.var
AcidBubbles.RubberLeash.2.jpg
AcidBubbles.RubberLeash.4.var
AcidBubbles.Timeline.200.jpg
AcidBubbles.Timeline.210.var
AcidBubbles.Timeline.242.var
AcidBubbles.Timeline.248.var
AcidBubbles.Timeline.249.var
AcidBubbles.Timeline.250.var
AcidBubbles.Timeline.251.var
AcidBubbles.Timeline.252.var
AcidBubbles.Timeline.268.var
AcidBubbles.Timeline.269.var
AcidBubbles.Timeline.270.var
AcidBubbles.Timeline.271.var
AcidBubbles.Timeline.272.var
AcidBubbles.Timeline.273.var
AcidBubbles.Timeline.274.var
AcidBubbles.Timeline.275.var
AcidBubbles.Timeline.280.var
AcidBubbles.Utilities.5.var
AcidBubbles.Utilities.6.var

Gives -

What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Animations_Presets.1.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.BlendShapes.1.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ChloeTouch.1.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.BlendShapes.2.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.RubberLeash.2.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Cornwall.2.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\Acidbubbles.Cornwall.2.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.FloatingUIComponents.3.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.RubberLeash.4.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Utilities.5.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Utilities.6.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.11.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderTuner.13.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.16.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.19.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.20.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderEditor.36.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderEditor.37.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderEditor.38.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Embody.53.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Embody.57.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Embody.58.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.200.jpg".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.210.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.242.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.248.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.249.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.250.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.251.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.252.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.268.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.269.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.270.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.271.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.272.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.273.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.274.var".
What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.275.var".

What I'm after is -


AcidBubbles.Animations_Presets.1.jpg
AcidBubbles.BlendShapes.2.var
AcidBubbles.ChloeTouch.1.jpg
AcidBubbles.ColliderEditor.38.var
AcidBubbles.ColliderTuner.13.jpg
Acidbubbles.Cornwall.2.jpg
AcidBubbles.Cornwall.2.var
AcidBubbles.Embody.58.var
AcidBubbles.FloatingUIComponents.3.jpg
AcidBubbles.Glance.20.var
AcidBubbles.RubberLeash.2.jpg
AcidBubbles.RubberLeash.4.var
AcidBubbles.Timeline.200.jpg
AcidBubbles.Timeline.280.var
AcidBubbles.Utilities.6.var

答案1

得分: 1

您应该能够只使用一个Get-ChildItem调用来完成这个操作:

$mainDirectoryPath = 'E:\Install\test\_New'
# 首先获取所有文件,其基本名称以点和数字结尾
Get-ChildItem -Path $mainDirectoryPath -File -Recurse |
Where-Object { $_.BaseName -match '\.\d+$' } | 
Group-Object { $_.BaseName -replace '\.\d+$'} |   # 首先按照不带版本号的BaseName进行分组
ForEach-Object {
    # 接下来,在每个这样的组内部,再次按文件扩展名进行分组
    $subgroups = $_.Group | Group-Object Extension
    foreach ($group in $subgroups) {
        # 按整数版本值排序,最大的数字排在前面
        # 选择除了第一个之外的所有文件并删除其余的文件
        $group.Group | Sort-Object {[int]($_.BaseName -split '\.')[-1]} -Descending | 
                       Select-Object -Skip 1 |
                       Remove-Item -Force
    }
}

使用您提供的最新文件示例,在运行此脚本之后,您将得到以下文件:

AcidBubbles.Animations_Presets.1.jpg
AcidBubbles.BlendShapes.2.var
AcidBubbles.ChloeTouch.1.jpg
AcidBubbles.ColliderEditor.38.var
AcidBubbles.ColliderTuner.13.jpg
Acidbubbles.Cornwall.2.jpg
AcidBubbles.Cornwall.2.var
AcidBubbles.Embody.58.var
AcidBubbles.FloatingUIComponents.3.jpg
AcidBubbles.Glance.20.var
AcidBubbles.RubberLeash.2.jpg
AcidBubbles.RubberLeash.4.var
AcidBubbles.Timeline.200.jpg
AcidBubbles.Timeline.280.var
AcidBubbles.Utilities.6.var

P.S. 在您期望的输出中,您忘记了文件 AcidBubbles.RubberLeash.2.jpg

英文:

You should be able to do this with just one Get-ChildItem call:

$mainDirectoryPath = 'E:\Install\test\_New'
# first get all files with a basename that ends with a dot followed by a number
Get-ChildItem -Path $mainDirectoryPath -File -Recurse |
Where-Object { $_.BaseName -match '\.\d+$' } | 
Group-Object { $_.BaseName -replace '\.\d+$'} |   # first group on the BaseName without the version number
ForEach-Object {
    # next, within each of these groups, group again on the file extension
    $subgroups = $_.Group | Group-Object Extension
    foreach ($group in $subgroups) {
        # sort on the integer version value, highest number on top
        # select all except the first one and remove the rest
        $group.Group | Sort-Object {[int]($_.BaseName -split '\.')[-1]} -Descending | 
                       Select-Object -Skip 1 |
                       Remove-Item -Force
    }
}

Using your latest example files, after running this you will end up with these files:

AcidBubbles.Animations_Presets.1.jpg
AcidBubbles.BlendShapes.2.var
AcidBubbles.ChloeTouch.1.jpg
AcidBubbles.ColliderEditor.38.var
AcidBubbles.ColliderTuner.13.jpg
Acidbubbles.Cornwall.2.jpg
AcidBubbles.Cornwall.2.var
AcidBubbles.Embody.58.var
AcidBubbles.FloatingUIComponents.3.jpg
AcidBubbles.Glance.20.var
AcidBubbles.RubberLeash.2.jpg
AcidBubbles.RubberLeash.4.var
AcidBubbles.Timeline.200.jpg
AcidBubbles.Timeline.280.var
AcidBubbles.Utilities.6.var

<sup>P.S. In your desired output you forgot file AcidBubbles.RubberLeash.2.jpg</sup>

huangapple
  • 本文由 发表于 2023年3月9日 17:09:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定