【问题标题】:Powershell command to delete subfolders without deleting the rootPowershell命令删除子文件夹而不删除根目录
【发布时间】:2017-06-25 23:07:03
【问题描述】:

我在创建允许我删除多个子文件夹而不删除屋顶文件夹的 PS 命令时遇到问题。

IE:

C:\Test 有很多子文件夹:

  • C:\Test\Item1
  • C:\Test\Item2
  • C:\Test\Item3

并且文件夹 Item1、Item2 和 Item3 有许多子文件夹和文件。

我想创建一个 PS,允许我删除 Item1、Item2 和 Item3 中的所有空子文件夹,而不删除 Item1、Item2 和 Item3 文件夹。可能任何 Item 文件夹是空的,但我不想删除它们,只是每个文件夹的空内容。

这只是一个例子,我在 Test 中有大约 300 个 Item 文件夹。

我通常会使用这个:

$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)为空,则会删除它们。

提前致谢。

【问题讨论】:

    标签: powershell root directory


    【解决方案1】:

    所以您要删除空子文件夹中的所有项目或一般项目中的所有项目?

    这将删除目录“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 }
    }
    

    我没有太多时间,只是快速而肮脏的代码,希望我能有所帮助。

    编辑:这是我想出的,它的性能不如我想的那样,但它可以完成工作并且相当快,你自己试试它对我有用 - 甚至扔了几个 cmets并输出以澄清发生了什么。

    $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        
        }
    }
    

    【讨论】:

    • 感谢您的回复,这对我很有帮助,因为这几乎是我想要的。问题是当一个文件夹(例如 Item1)有其他空的子文件夹时,PS 不会删除它们。如果有办法实现这一点,那就太棒了。非常感谢!!!
    • 我可以使用的另一件事是删除 Item1.. ItemZ 文件夹中的每个文件和文件夹,如果它们超过 X 时间。这能让事情变得更容易吗?我不想要的是删除 ItemX 文件夹。
    • 这里的小更新:我已经设法删除了所有文件,但是当我尝试只删除超过 X 天的文件时,它不起作用:$Path = "C:\Test\"$itemFolders= Get-ChildItem -Path $Path $limit = (Get-Date).AddDays(-30)$itemFolders| Foreach-Object {Get-ChildItem -Path $_.FullName |Where-Object {((Get-ChildItem $_.FullName).Count -ge 0) -or ($_.CreationTime -lt $limit)} | Foreach-object {Remove-item -Recurse -Force -path $_.FullName} }
    • 第一个不起作用,它输出一堆错误并且没有任何内容被删除:“Remove-Item : A positional parameter cannot be found that accept argument 'C:\Code\repos\zzz\代表\小部件\suites.json'。”
    • 效果很好!我遇到的唯一问题是文件名中的通配符。我在不同的脚本上查看了-LiteralPath,它半有效。认为我可以为它们添加错误捕获器并仅在这些文件上使用 -LiteralPath? Remove-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: SK9Z$[EDJW{{K@TZ(L43ML7.png
    猜你喜欢
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    相关资源
    最近更新 更多