我在子文件夹中有很多文件,它们的名称如下
\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
最终的结果应该是这样的
\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
其中数字是版本号,但我只想保留最新的.var(它与.txt,.jpg或.png(可能是其他人)相关联),并在powershell中删除其他人。
到目前为止,我有以下失败的remove-item,因为传递的名称不再包含版本号?
# 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 -不,这并没有给予我我想要的:
运行以下文件-
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
给予-
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
1条答案
按热度按时间7eumitmz1#
您应该可以通过一个Get-ChildItem调用来完成此操作:
使用您最新的示例文件,运行此命令后,您将得到以下文件:
P.S.在您想要的输出中,您忘记了文件
AcidBubbles.RubberLeash.2.jpg