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

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

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

问题

  1. AcidBubbles.Animations_Presets.1.jpg
  2. AcidBubbles.BlendShapes.2.var
  3. AcidBubbles.ChloeTouch.1.jpg
  4. AcidBubbles.ColliderEditor.38.var
  5. AcidBubbles.ColliderTuner.13.jpg
  6. Acidbubbles.Cornwall.2.jpg
  7. AcidBubbles.Cornwall.2.var
  8. AcidBubbles.Embody.58.var
  9. AcidBubbles.FloatingUIComponents.3.jpg
  10. AcidBubbles.Glance.20.var
  11. AcidBubbles.RubberLeash.2.jpg
  12. AcidBubbles.RubberLeash.4.var
  13. AcidBubbles.Timeline.200.jpg
  14. AcidBubbles.Timeline.280.var
  15. AcidBubbles.Utilities.6.var
英文:

i have a lot of files in subfolders with names like

  1. \dave\dave.look1.1.var
  2. \dave\dave.look1.1.jpg
  3. \dave\dave.look1.1.txt
  4. \dave\dave.look1.2.var
  5. \dave\dave.look1.2.jpg
  6. \dave\dave.look1.3.var
  7. \dave\dave.look1.3.jpg
  8. \dave\dave.look2.1.var
  9. \dave\dave.look3.1.var
  10. \bert\bert.look1.1.var
  11. \bert\bert.look1.2.var
  12. \bert\bert.look1.2.png
  13. \bert\bert.look1.4.var
  14. \fred\fred.look1.1.var
  15. \fred\fred.look1.1.jpg
  16. \fred\fred.look1.2.var
  17. \fred\fred.look1.3.var
  18. \fred\fred.look3.4.var
  19. \fred\fred.look3.4.jpg
  20. \fred\fred.look3.5.var
  21. \fred\fred.look3.5.jpg

The end result should be something like this

  1. \dave\dave.look1.3.var
  2. \dave\dave.look1.3.jpg
  3. \dave\dave.look2.1.var
  4. \dave\dave.look3.1.var
  5. \bert\bert.look1.4.var
  6. \fred\fred.look1.3.var
  7. \fred\fred.look3.5.var
  8. \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?

  1. # Set the path to the main directory containing the subdirectories
  2. $mainDirectoryPath = "E:\Install\test\_New"
  3. # Get a list of all subdirectories in the main directory
  4. $subDirectories = Get-ChildItem -Path $mainDirectoryPath -Directory -Recurse
  5. Write-Host "Found $($files.Count) files in $subDirectories"
  6. # Loop through each subdirectory
  7. foreach ($subDirectory in $subDirectories) {
  8. # Get a list of all files in the subdirectory
  9. $files = Get-ChildItem -Path $subDirectory.FullName -File
  10. # Group the files by the name before the version number
  11. $groupedFiles = $files | Group-Object { $_.Name.Split('.')[0,1] -join '.' }
  12. # For each group of files, keep only the file with the highest version number
  13. foreach ($group in $groupedFiles) {
  14. Write-Host "Processing $($group.Count) files for $($group.Name)"
  15. $group | Sort-Object { [int]$_.Name.Split('.')[2] } -Descending | Select-Object -Skip 1 | Remove-Item -Force -whatif
  16. }

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

Running against the following files -

  1. AcidBubbles.Animations_Presets.1.jpg
  2. AcidBubbles.BlendShapes.1.var
  3. AcidBubbles.BlendShapes.2.var
  4. AcidBubbles.ChloeTouch.1.jpg
  5. AcidBubbles.ColliderEditor.36.var
  6. AcidBubbles.ColliderEditor.37.var
  7. AcidBubbles.ColliderEditor.38.var
  8. AcidBubbles.ColliderTuner.13.jpg
  9. Acidbubbles.Cornwall.2.jpg
  10. AcidBubbles.Cornwall.2.var
  11. AcidBubbles.Embody.53.var
  12. AcidBubbles.Embody.57.var
  13. AcidBubbles.Embody.58.var
  14. AcidBubbles.FloatingUIComponents.3.jpg
  15. AcidBubbles.Glance.11.var
  16. AcidBubbles.Glance.16.var
  17. AcidBubbles.Glance.19.var
  18. AcidBubbles.Glance.20.var
  19. AcidBubbles.RubberLeash.2.jpg
  20. AcidBubbles.RubberLeash.4.var
  21. AcidBubbles.Timeline.200.jpg
  22. AcidBubbles.Timeline.210.var
  23. AcidBubbles.Timeline.242.var
  24. AcidBubbles.Timeline.248.var
  25. AcidBubbles.Timeline.249.var
  26. AcidBubbles.Timeline.250.var
  27. AcidBubbles.Timeline.251.var
  28. AcidBubbles.Timeline.252.var
  29. AcidBubbles.Timeline.268.var
  30. AcidBubbles.Timeline.269.var
  31. AcidBubbles.Timeline.270.var
  32. AcidBubbles.Timeline.271.var
  33. AcidBubbles.Timeline.272.var
  34. AcidBubbles.Timeline.273.var
  35. AcidBubbles.Timeline.274.var
  36. AcidBubbles.Timeline.275.var
  37. AcidBubbles.Timeline.280.var
  38. AcidBubbles.Utilities.5.var
  39. AcidBubbles.Utilities.6.var

Gives -

  1. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Animations_Presets.1.jpg".
  2. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.BlendShapes.1.var".
  3. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ChloeTouch.1.jpg".
  4. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.BlendShapes.2.var".
  5. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.RubberLeash.2.jpg".
  6. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Cornwall.2.var".
  7. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\Acidbubbles.Cornwall.2.jpg".
  8. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.FloatingUIComponents.3.jpg".
  9. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.RubberLeash.4.var".
  10. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Utilities.5.var".
  11. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Utilities.6.var".
  12. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.11.var".
  13. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderTuner.13.jpg".
  14. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.16.var".
  15. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.19.var".
  16. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Glance.20.var".
  17. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderEditor.36.var".
  18. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderEditor.37.var".
  19. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.ColliderEditor.38.var".
  20. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Embody.53.var".
  21. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Embody.57.var".
  22. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Embody.58.var".
  23. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.200.jpg".
  24. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.210.var".
  25. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.242.var".
  26. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.248.var".
  27. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.249.var".
  28. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.250.var".
  29. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.251.var".
  30. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.252.var".
  31. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.268.var".
  32. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.269.var".
  33. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.270.var".
  34. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.271.var".
  35. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.272.var".
  36. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.273.var".
  37. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.274.var".
  38. What if: Performing the operation "Remove File" on target "E:\_test\AcidBubbles\AcidBubbles.Timeline.275.var".

What I'm after is -

  1. AcidBubbles.Animations_Presets.1.jpg
  2. AcidBubbles.BlendShapes.2.var
  3. AcidBubbles.ChloeTouch.1.jpg
  4. AcidBubbles.ColliderEditor.38.var
  5. AcidBubbles.ColliderTuner.13.jpg
  6. Acidbubbles.Cornwall.2.jpg
  7. AcidBubbles.Cornwall.2.var
  8. AcidBubbles.Embody.58.var
  9. AcidBubbles.FloatingUIComponents.3.jpg
  10. AcidBubbles.Glance.20.var
  11. AcidBubbles.RubberLeash.2.jpg
  12. AcidBubbles.RubberLeash.4.var
  13. AcidBubbles.Timeline.200.jpg
  14. AcidBubbles.Timeline.280.var
  15. AcidBubbles.Utilities.6.var

答案1

得分: 1

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

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

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

  1. AcidBubbles.Animations_Presets.1.jpg
  2. AcidBubbles.BlendShapes.2.var
  3. AcidBubbles.ChloeTouch.1.jpg
  4. AcidBubbles.ColliderEditor.38.var
  5. AcidBubbles.ColliderTuner.13.jpg
  6. Acidbubbles.Cornwall.2.jpg
  7. AcidBubbles.Cornwall.2.var
  8. AcidBubbles.Embody.58.var
  9. AcidBubbles.FloatingUIComponents.3.jpg
  10. AcidBubbles.Glance.20.var
  11. AcidBubbles.RubberLeash.2.jpg
  12. AcidBubbles.RubberLeash.4.var
  13. AcidBubbles.Timeline.200.jpg
  14. AcidBubbles.Timeline.280.var
  15. AcidBubbles.Utilities.6.var

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

英文:

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

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

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

  1. AcidBubbles.Animations_Presets.1.jpg
  2. AcidBubbles.BlendShapes.2.var
  3. AcidBubbles.ChloeTouch.1.jpg
  4. AcidBubbles.ColliderEditor.38.var
  5. AcidBubbles.ColliderTuner.13.jpg
  6. Acidbubbles.Cornwall.2.jpg
  7. AcidBubbles.Cornwall.2.var
  8. AcidBubbles.Embody.58.var
  9. AcidBubbles.FloatingUIComponents.3.jpg
  10. AcidBubbles.Glance.20.var
  11. AcidBubbles.RubberLeash.2.jpg
  12. AcidBubbles.RubberLeash.4.var
  13. AcidBubbles.Timeline.200.jpg
  14. AcidBubbles.Timeline.280.var
  15. 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:

确定