PowerShell脚本优化:保留最新版本.var及关联文件
PowerShell文件版本清理脚本修正
需要实现:遍历子文件夹,仅保留每个文件组内版本号最高的.var文件及其关联的.jpg/.txt/.png等格式文件,删除其余旧版本文件。现有脚本删除逻辑错误,无法得到预期结果,相关信息如下:
原始文件示例
\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
当前使用的PowerShell脚本
# 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 } }
错误执行结果
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".
期望留存文件
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
问题分析与修正脚本
原脚本问题
- 分组逻辑僵化:固定取文件名前两段作为分组依据,无法兼容带下划线的组名(如
Animations_Presets) - 文件处理逻辑错误:将同一版本下的不同后缀文件单独排序后删除,导致本该保留的关联文件被误删
- 版本号提取逻辑不通用:固定取第三段作为版本号,未考虑文件名结构的一致性(版本号始终是倒数第二段)
修正后的脚本
# 设置包含子文件夹的主目录路径 $mainDirectoryPath = "E:\Install\test\_New" # 获取主目录下的所有子文件夹 $subDirectories = Get-ChildItem -Path $mainDirectoryPath -Directory -Recurse foreach ($subDirectory in $subDirectories) { # 获取当前子文件夹下的所有文件 $files = Get-ChildItem -Path $subDirectory.FullName -File Write-Host "正在处理文件夹: $($subDirectory.FullName),共 $($files.Count) 个文件" # 按文件组前缀分组(例如dave.look1、AcidBubbles.Animations_Presets) $groupedByPrefix = $files | Group-Object { $parts = $_.Name.Split('.') # 提取前缀:去掉最后两段(版本号+后缀) $parts[0..($parts.Length - 3)] -join '.' } foreach ($prefixGroup in $groupedByPrefix) { Write-Host "处理文件组: $($prefixGroup.Name)" # 按版本号分组,将同版本的所有文件归为一组 $versionGroups = $prefixGroup.Group | Group-Object { $parts = $_.Name.Split('.') [int]$parts[$parts.Length - 2] # 版本号为倒数第二段 } -AsHashTable -AsString # 找到当前组的最高版本号 $highestVersion = ($versionGroups.Keys | [int[]] | Measure-Object -Maximum).Maximum # 删除所有非最高版本的文件 foreach ($version in $versionGroups.Keys) { if ([int]$version -ne $highestVersion) { Write-Host "删除版本 $version 的文件:" $versionGroups[$version] | Remove-Item -Force -WhatIf } } } }
修正说明
- 灵活分组:通过动态截取文件名前缀(去掉最后两段),兼容任意格式的组名
- 版本组管理:将同一版本下的所有关联文件归为一组,确保最高版本的所有文件都被保留
- 精准版本判断:通过
Measure-Object直接获取最高版本号,避免排序后误删同版本文件 - 可验证执行:保留
-WhatIf参数,可先预览删除操作,确认无误后再移除该参数执行实际删除
内容的提问来源于stack exchange,提问作者Puck
相关产品推荐
相关产品推荐

