Powershell命令删除子文件夹而不删除根目录
Powershell command to delete subfolders without deleting the root
我在创建 PS 命令时遇到问题,该命令允许我在不删除 roof 文件夹的情况下删除多个子文件夹。
I.E:
C:\Test 有很多子文件夹:
- C:\Test\Item1
- C:\Test\Item2
- C:\Test\Item3
并且文件夹Item1、Item2、Item3有很多子文件夹和文件。
我想创建一个 PS 允许我删除 Item1、Item2 和 Item3 中的所有空子文件夹而不删除 Item1、Item2 和 Item3 文件夹。有可能任何一个Item文件夹都是空的,但我不想删除它们,只是每个文件夹的内容都是空的。
这只是一个示例,我在测试中有大约 300 个项目文件夹。
我通常会用这个:
$path="C:\TEST"
do {
$dir = gci $path -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
$dir | Foreach-Object { Remove-Item $_ }
} while ($dir.count -gt 0)
但这会删除文件夹根文件夹(Item1、Item2 或 Item3),如果它们是空的。
提前致谢。
所以您要删除空子文件夹中的所有项目或常规中的所有项目?
这将删除目录中的所有文件夹或常规项目"C:\abc\"
$path = "C:\abc\"
Get-ChildItem -Path $path -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName }
这将删除所有没有任何项目的文件夹。
$path = "C:\abc\"
Get-ChildItem -Path $path -Recurse | Where-Object {(Get-ChildItem $_.FullName).Count -eq 0} |Foreach-object {Remove-item -Recurse -path $_.FullName }
´
这将查看内部 "C:\abc\" 获取所有子项并删除子项中的所有空目录在您的示例中这将是 Item1,Item2,...
$Path = "C:\abc\"
$itemFolders= Get-ChildItem -Path $Path
$itemFolders| Foreach-Object {
Get-ChildItem -Path $_.FullName |
Where-Object {(Get-ChildItem $_.FullName).Count -eq 0} |
Foreach-object {Remove-item -Recurse -path $_.FullName }
}
由于我没有太多时间,只是快速而肮脏的代码,希望我能有所帮助。
编辑:这是我想出的,它的性能不如我想要的,但它完成了工作并且相当快,亲自尝试一下它对我有用 - 甚至投入了一些评论和输出以澄清发生了什么。
$Path="C:\abc\"
$itemFolders = Get-ChildItem $Path
#Get All Folders inside
$AllFolders = $itemFolders | Get-ChildItem -Recurse | Where-Object {$_.PSIsContainer} | Select -Property FullName
#First delete all files older than 30 days
$itemFolders | Get-ChildItem -Recurse -File | ForEach-Object{
$limit = (Get-Date).AddDays(-30)
if($_.LastWriteTime -lt $limit)
{
"{0} hasn't been modified in the last 30 days deleting it" -f $_.FullName
Remove-Item -Path $_.FullName -Force -ErrorAction SilentlyContinue
}
}
#Check if there are files inside that are not containers
$AllFolders | ForEach-Object{
$files = Get-ChildItem -File -Recurse -Path $_.FullName
$directories = Get-ChildItem -Directory -Recurse -Path $_.FullName
#If There are any files inside the folder dont delete it.
if($files.Count -gt 0)
{
"Found {0} files inside {1} do not delete this" -f $files.Count, $_.FullName
}
#If There are no files and no directories inside delete it.
elseif($files.Count -eq 0 -and $directories.Count -eq 0)
{
"Empty Folder {0} deleting it" -f $_.FullName
Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
#If there are no files and empty directories inside delete it.
elseif($files.Count -eq 0 -and $directories.Count -gt 0)
{
"No Files but directories found in {0} since its recursive delete it" -f $_.FullName
Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
}
我在创建 PS 命令时遇到问题,该命令允许我在不删除 roof 文件夹的情况下删除多个子文件夹。
I.E:
C:\Test 有很多子文件夹:
- C:\Test\Item1
- C:\Test\Item2
- C:\Test\Item3
并且文件夹Item1、Item2、Item3有很多子文件夹和文件。
我想创建一个 PS 允许我删除 Item1、Item2 和 Item3 中的所有空子文件夹而不删除 Item1、Item2 和 Item3 文件夹。有可能任何一个Item文件夹都是空的,但我不想删除它们,只是每个文件夹的内容都是空的。
这只是一个示例,我在测试中有大约 300 个项目文件夹。
我通常会用这个:
$path="C:\TEST"
do {
$dir = gci $path -directory -recurse | Where { (gci $_.fullName).count -eq 0 } | select -expandproperty FullName
$dir | Foreach-Object { Remove-Item $_ }
} while ($dir.count -gt 0)
但这会删除文件夹根文件夹(Item1、Item2 或 Item3),如果它们是空的。
提前致谢。
所以您要删除空子文件夹中的所有项目或常规中的所有项目?
这将删除目录中的所有文件夹或常规项目"C:\abc\"
$path = "C:\abc\"
Get-ChildItem -Path $path -Recurse| Foreach-object {Remove-item -Recurse -path $_.FullName }
这将删除所有没有任何项目的文件夹。
$path = "C:\abc\"
Get-ChildItem -Path $path -Recurse | Where-Object {(Get-ChildItem $_.FullName).Count -eq 0} |Foreach-object {Remove-item -Recurse -path $_.FullName }
´ 这将查看内部 "C:\abc\" 获取所有子项并删除子项中的所有空目录在您的示例中这将是 Item1,Item2,...
$Path = "C:\abc\"
$itemFolders= Get-ChildItem -Path $Path
$itemFolders| Foreach-Object {
Get-ChildItem -Path $_.FullName |
Where-Object {(Get-ChildItem $_.FullName).Count -eq 0} |
Foreach-object {Remove-item -Recurse -path $_.FullName }
}
由于我没有太多时间,只是快速而肮脏的代码,希望我能有所帮助。
编辑:这是我想出的,它的性能不如我想要的,但它完成了工作并且相当快,亲自尝试一下它对我有用 - 甚至投入了一些评论和输出以澄清发生了什么。
$Path="C:\abc\"
$itemFolders = Get-ChildItem $Path
#Get All Folders inside
$AllFolders = $itemFolders | Get-ChildItem -Recurse | Where-Object {$_.PSIsContainer} | Select -Property FullName
#First delete all files older than 30 days
$itemFolders | Get-ChildItem -Recurse -File | ForEach-Object{
$limit = (Get-Date).AddDays(-30)
if($_.LastWriteTime -lt $limit)
{
"{0} hasn't been modified in the last 30 days deleting it" -f $_.FullName
Remove-Item -Path $_.FullName -Force -ErrorAction SilentlyContinue
}
}
#Check if there are files inside that are not containers
$AllFolders | ForEach-Object{
$files = Get-ChildItem -File -Recurse -Path $_.FullName
$directories = Get-ChildItem -Directory -Recurse -Path $_.FullName
#If There are any files inside the folder dont delete it.
if($files.Count -gt 0)
{
"Found {0} files inside {1} do not delete this" -f $files.Count, $_.FullName
}
#If There are no files and no directories inside delete it.
elseif($files.Count -eq 0 -and $directories.Count -eq 0)
{
"Empty Folder {0} deleting it" -f $_.FullName
Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
#If there are no files and empty directories inside delete it.
elseif($files.Count -eq 0 -and $directories.Count -gt 0)
{
"No Files but directories found in {0} since its recursive delete it" -f $_.FullName
Remove-Item -Path $_.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
}