windows - 在 Powershell 中删除具有序列化名称的文件夹
问题描述
我的问题可能\可能并不复杂,但我一直在摸索并寻找一种方法来做到这一点,但到目前为止还没有想出太多。
我有一个像这样的文件夹结构:
C:\
└───ParentFolder
├───ChildFolder1
│ ├───SubFolderA_1
│ ├───SubFolderA_2
│ ├───SubFolderA_3
│ ├───SubFolderA_4
│ ├───SubFolderB_1
│ ├───SubFolderB_2
│ ├───SubFolderB_3
│ └───SubFolderB_4
└───ChildFolder2
├───SubFolderA_1
├───SubFolderA_2
├───SubFolderA_3
├───SubFolderA_4
├───SubFolderB_1
├───SubFolderB_2
├───SubFolderB_3
└───SubFolderB_4
我正在寻找的是一个 PowerShell 脚本,它将利用“子文件夹”名称的序列化特性来删除旧版本,只留下最新的子文件夹。
使用上面的示例,这意味着脚本将删除 SubFolderA_1 到 SubFolderA_3 和 SubFolderB_1 到 SubFolderB_3,在 ChildFolders 中只留下 SubFolderA_4 和 SubfolderB_4。
有人知道这样做的方法吗?我在考虑对象排序 + 递归函数 + 模式匹配,但我似乎没有得到任何结果。顺便说一句,我是个PS菜鸟。
您的帮助将不胜感激。
解决方案
这是一种方法。[笑] 它的核心是Group-Object
cmdlet。经常被忽视的一件事是使用计算属性的能力与Select-Object
cmdlet 一样。
# fake reading in a list of directories
# in real life, use Get-ChildItem -Directory
$DirList = @(
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderA_1'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderA_2'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderA_3'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderA_4'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderB_1'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderB_2'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderB_3'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder1\SubFolderB_4'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderA_1'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderA_2'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderA_3'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderA_4'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderB_11'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderB_22'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderB_3'
[System.IO.DirectoryInfo]'C:\ParentFolder\ChildFolder2\SubFolderB_44'
)
$GroupedDirList = $DirList |
# changed from sorting by the FullName to sorting by the trailing number
# thanks to LotPings for pointing out the glitch with multi-digit numbers
Sort-Object {[int]$_.FullName.Split('_')[1]} |
Group-Object {$_.FullName.Split('_')[0]}
foreach ($GDL_Item in $GroupedDirList)
{
$GDL_Item.Group |
Select-Object -SkipLast 1 |
ForEach-Object {
# remove the quotes, the Write-Host, and the "$()" when you do this for real
# can't use the "-WhatIf" parameter here since the dirs don't actually exist on my system
Write-Host "Remove-Item -LiteralPath $($_.FullName) -Recurse -WhatIf"
}
'=' * 20
}
输出 ...
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder1\SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder1\SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder1\SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder1\SubFolderB_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder1\SubFolderB_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder1\SubFolderB_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder2\SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder2\SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder2\SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder2\SubFolderB_3 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder2\SubFolderB_11 -Recurse -WhatIf
Remove-Item -LiteralPath C:\ParentFolder\ChildFolder2\SubFolderB_22 -Recurse -WhatIf
====================
推荐阅读
- javascript - 如何在服务工作者中刷新 Firebase 消息传递令牌
- python - 如果行匹配,PySpark 设置列值等于另一个数据帧值
- c++ - 使用 using 为成员模板 typedef 起别名会使编译器认为它不是类模板
- javascript - 使用 JSON.Stringify() 设置带撇号的隐藏字段的 VAL 以在 Django 中使用
- cloudflare-apps - data-cfapps-unplaceable 是什么意思?
- vba - 如果字符串具有不等式运算符,如何在 VBA 中进行字符串比较?
- python - 如何在一行数据框中解压缩列表中的多个字典对象?
- r - 在R中的向量中显示对象的顺序
- android - 为什么可点击视图被选中后会出现波纹?
- bluetooth - 远距离蓝牙发射器微芯片