【问题标题】:Powershell Find all empty folders and subfolders in a given Folder namePowershell 查找给定文件夹名称中的所有空文件夹和子文件夹
【发布时间】:2021-08-09 07:48:10
【问题描述】:

我正在尝试获得一个 a) 如果文件夹名为“Archiv”,则列出所有空文件夹和子文件夹 b) 我想删除所有这些空文件夹。我当前的方法不检查子文件夹。

如果结果能以 .csv 格式导出也很好 =)

$TopDir = 'C:\Users\User\Test'

$DirToFind = 'Archiv'>$EmptyDirList = @(
    Get-ChildItem -LiteralPath $TopDir -Directory -Recurse |
        Where-Object {
            #[System.IO.Directory]::GetFileSystemEntries($_.FullName).Count -eq 0
            $_.GetFileSystemInfos().Count -eq 0 -and
            $_.Name -match $DirToFind
            }
        ).FullName

$EmptyDirList

任何想法如何调整代码?提前致谢

【问题讨论】:

    标签: powershell directory get-childitem


    【解决方案1】:

    您需要颠倒 Get-ChildItem 列出项目的顺序,以便您可以先使用嵌套最深的空文件夹来删除。

    $LogFile = 'C:\Users\User\RemovedEmptyFolders.log'
    $TopDir  = 'C:\Users\User\Test'
    
    # first get a list of all folders below the $TopDir directory that are named 'Archiv' (FullNames only)
    $archiveDirs = (Get-ChildItem -LiteralPath $TopDir -Filter 'Archiv' -Recurse -Directory -Force).FullName | 
                    # sort on the FullName.Length property in Descending order to get 'deepest-nesting-first' 
                    Sort-Object -Property Length -Descending 
    
    # next, remove all empty subfolders in each of the $archiveDirs
    $removed = foreach ($dir in $archiveDirs) {
        (Get-ChildItem -LiteralPath $dir -Directory -Force) |
        # sort on the FullName.Length property in Descending order to get 'deepest-nesting-first' 
        Sort-Object @{Expression = {$_.FullName.Length}} -Descending | 
        ForEach-Object {
           # if this folder is empty, remove it and output its FullName for the log
           if (@($_.GetFileSystemInfos()).Count -eq 0) { 
               $_.FullName
               Remove-Item -LiteralPath $_.FullName -Force
           }
        }
        # next remove the 'Archiv' folder that is now possibly empty too
        if (@(Get-ChildItem -LiteralPath $dir -Force).Count -eq 0) {
            # output this folders fullname and delete
            $dir
            Remove-Item -LiteralPath $dir -Force
        }
    }
    
    $removed | Set-Content -Path $LogFile -PassThru  # write your log file. -PassThru also writes the output on screen
    

    【讨论】:

    • 谢谢。似乎还有一个问题:PowerShell error: get-ChildItem: "a part of the path "path-name" could not be found 因此方法 get-ChildItem 不起作用..
    • @Peppino 试试Get-ChildItem -LiteralPath
    • @Peppino 抱歉,刚刚注意到过滤器应该是Archiv,而不是Archive
    • 我改变了这两种情况,但我仍然得到同样的错误
    • @Peppino 我编辑了代码。如果您仍然收到该错误,则可能是某些路径超过了 260 个字符的路径限制。确保您的窗口 accepts long paths
    【解决方案2】:

    不确定是否需要 CSV,我认为一个简单的文本文件就足够了,因为它只是一个列表。 无论如何,这里(虽然不是最优雅的)一个解决方案也将删除“嵌套的空目录”。这意味着如果一个目录只包含空的directorIS,它也会被删除

    $TopDir = "C:\Test" #Top level directory to scan
    $EmptyDirListReport = "C:\EmptyDirList.txt" #Text file location to store a file with the list of deleted directorues
    if (Test-Path -Path $EmptyDirListReport -PathType Leaf)
    {
        Remove-Item -Path $EmptyDirListReport -Force
    }
    $EmptyDirList = ""
    Do
    {
        $EmptyDirList = Get-ChildItem -Path $TopDir -Recurse | Where-Object -FilterScript { $_.PSIsContainer } | Where-Object -FilterScript { ((Get-ChildItem -Path $_.FullName).Count -eq 0) } | Select-Object -ExpandProperty FullName
        if ($EmptyDirList)
        {
            $EmptyDirList | Out-File -FilePath $EmptyDirListReport -Append
            $EmptyDirList | Remove-Item -Force
        }
    } while ($EmptyDirList)
    

    【讨论】:

    • 嵌套的空目录将被删除似乎很完美,但文件夹名称“Archiv”有过滤器?
    【解决方案3】:

    这应该可以解决问题,也应该适用于嵌套。

    $result=(Get-ChildItem -Filter "Archiv" -Recurse -Directory  $topdir | Sort-Object @{Expression = {$_.FullName.Length}} -Descending | ForEach-Object {
         if ((Get-ChildItem -Attributes d,h,a $_.fullname).count -eq 0){
             $_
             rmdir $_.FullName
          } 
    })
    $result | select Fullname |ConvertTo-Csv |Out-File $Logfile
    

    【讨论】:

      【解决方案4】:

      你可以用单线做到这一点:

      > Get-ChildItem -Recurse dir -filter Archiv | 
          Where-Object {($_ | Get-ChildItem).count -eq 0} | 
            Remove-Item
      

      尽管出于某种原因,如果您嵌套了像 Archiv/Archiv 这样的存档文件,则需要多次运行该行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        • 2020-05-16
        • 1970-01-01
        • 1970-01-01
        • 2018-04-02
        • 1970-01-01
        相关资源
        最近更新 更多