【问题标题】:How do I get only directories using Get-ChildItem?如何使用 Get-ChildItem 仅获取目录?
【发布时间】:2011-03-06 08:16:52
【问题描述】:

我正在使用 PowerShell 2.0,我想通过管道输出某个路径的所有子目录。以下命令输出所有文件和目录,但我不知道如何过滤掉这些文件。

Get-ChildItem c:\mypath -Recurse

我尝试使用$_.Attributes 来获取属性,但是我不知道如何构造一个System.IO.FileAttributes 的文字实例来进行比较。在cmd.exe 中是

dir /b /ad /s

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    对于 PowerShell 3.0 及更高版本:

    Get-ChildItem -Directory
    

    您还可以使用别名dirlsgci


    对于低于 3.0 的 PowerShell 版本:

    Get-ChildItem 返回的FileInfo 对象有一个“基础”属性PSIsContainer。您只想选择那些项目。

    Get-ChildItem -Recurse | ?{ $_.PSIsContainer }
    

    如果你想要目录的原始字符串名称,你可以这样做

    Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
    

    【讨论】:

    • Wish 别名为“IsFolder”。
    • xcud:不是每个由 PSDrive 表示的层次结构都是基于文件夹的。
    • “容器”和“文件夹”之间的语义鸿沟不是你可以开着卡车穿过的。
    • @xcud:请参阅 iraSenthil 的回答。 -Directory 和 -File 也适用于 Get-ChildItem。无需直接使用 PSIContainer 属性。
    • (Get-ChildItem | ?{ $_.PSIsContainer } | 选择名称).Name
    【解决方案2】:

    在 PowerShell 3.0 中,它更简单:

    Get-ChildItem -Directory #List only directories
    Get-ChildItem -File #List only files
    

    【讨论】:

    • dir 是 Get-ChildItem 的别名
    • @crashmstr 你确定吗?我检查了我的 PS4.0。对我来说,dir 别名为 Get-ChildItem-Directory-File 选项按描述工作。我使用命令echo $PSVersionTablehelp dirdir -Directorydir -File 来提出这个评论。
    • 这应该是答案
    • lsdirGet-ChildItem 的别名,选择你的毒药
    【解决方案3】:

    使用

    Get-ChildItem -dir #lists only directories
    Get-ChildItem -file #lists only files
    

    如果您喜欢别名,请使用

    ls -dir #lists only directories
    ls -file #lists only files
    

    dir -dir #lists only directories
    dir -file #lists only files
    

    要递归子目录,添加-r 选项。

    ls -dir -r #lists only directories recursively
    ls -file -r #lists only files recursively 
    

    在 PowerShell 4.0、PowerShell 5.0(Windows 10)、PowerShell Core 6.0(Windows 10、Mac 和 Linux)和PowerShell 7.0(Windows 10、Mac 和 Linux)上测试。

    注意:在 PowerShell Core 上,指定 -r 开关时不遵循符号链接。要遵循符号链接,请使用 -r 指定 -FollowSymlink 开关。

    注意 2:PowerShell 现在是跨平台的,从 6.0 版开始。跨平台版本最初称为 PowerShell Core,但从 PowerShell 7.0+ 开始,“Core”一词已被删除。

    Get-ChildItem 文档:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem

    【讨论】:

    • 这在 OP 的特定需要的 Powershell 2.0 中不起作用。 -[/Dir/Directory] ​​不是 Powershell 2.0 中的有效参数
    【解决方案4】:

    更简洁的方法:

    Get-ChildItem "<name_of_directory>" | where {$_.Attributes -match'Directory'}
    

    我想知道 PowerShell 3.0 是否有一个只返回目录的开关;添加似乎是合乎逻辑的事情。

    【讨论】:

    • 仅供参考 powershell 3.0 添加了 -Directory-File 标志
    【解决方案5】:

    用途:

    dir -r | where { $_ -is [System.IO.DirectoryInfo] }
    

    【讨论】:

      【解决方案6】:

      从 PowerShell v2 及更高版本(k 代表您开始搜索的文件夹):

      Get-ChildItem $Path -attributes D -Recurse
      

      如果您只想要文件夹名称,而不需要其他任何东西,请使用:

      Get-ChildItem $Path -Name -attributes D -Recurse
      

      如果您正在寻找一个特定的文件夹,您可以使用以下内容。在这种情况下,我正在寻找一个名为 myFolder 的文件夹:

      Get-ChildItem $Path -attributes D -Recurse -include "myFolder"
      

      【讨论】:

      • 使用 PS 3.0 或 4.0 ?
      • 属性参数似乎不在 PS2 中,它给出错误“找不到与参数名称'属性'匹配的参数”。在 PS3 中运行正常。
      【解决方案7】:

      这种方法需要更少的文字:

      ls -r | ? {$_.mode -match "d"}
      

      【讨论】:

      • 这是我会使用的。
      • 更短:ls -r | ? { $_.mode -match "d" }
      • 这没有找到压缩文件夹
      • 因为压缩文件夹是一个zip文件
      • 递归所有内容并不总是有用的。您可能有一棵非常深且茂密的树,您只对恰好向下两层的目录感兴趣;向下搜索三十层是浪费时间。
      【解决方案8】:

      接受的答案提到

      Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
      

      获取“原始字符串”。 但实际上Selected.System.IO.DirectoryInfo 类型的对象将被返回。对于原始字符串,可以使用以下内容:

      Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | % { $_.FullName }
      

      如果将值连接到字符串,差异很重要:

      • Select-Object 令人惊讶的是foo\@{FullName=bar}
      • 使用ForEach-操作符预期:foo\bar

      【讨论】:

      • Select-Object 实际上会返回 PSCustomObject 类型的对象。虽然您可以像使用 % (即 ForEach-Object)一样获取原始字符串,但您也可以使用 Select-Object -ExpandProperty FullName
      【解决方案9】:

      用途:

      dir -Directory -Recurse | Select FullName
      

      这将为您提供根结构的输出,其中仅包含目录的文件夹名称。

      【讨论】:

        【解决方案10】:

        您需要先使用 Get-ChildItem 以递归方式获取所有文件夹和文件。然后将该输出通过管道传输到仅获取文件的 Where-Object 子句中。

        # one of several ways to identify a file is using GetType() which
        # will return "FileInfo" or "DirectoryInfo"
        $files = Get-ChildItem E:\ -Recurse | Where-Object {$_.GetType().Name -eq "FileInfo"} ;
        
        foreach ($file in $files) {
          echo $file.FullName ;
        }
        

        【讨论】:

          【解决方案11】:

          用途:

          Get-ChildItem \\myserver\myshare\myshare\ -Directory | Select-Object -Property name |  convertto-csv -NoTypeInformation  | Out-File c:\temp\mydirectorylist.csv
          

          下面是哪个

          • 获取目标位置的目录列表: Get-ChildItem \\myserver\myshare\myshare\ -Directory
          • 仅提取目录名称: Select-Object -Property name
          • 将输出转换为 CSV 格式: convertto-csv -NoTypeInformation
          • 将结果保存到文件中: Out-File c:\temp\mydirectorylist.csv

          【讨论】:

          • 如果你能解释每个步骤,让更多人看到发生了什么,那就太好了。
          • 这在 OP 的特定需要的 Powershell 2.0 中不起作用。 -[/Dir/Directory] ​​不是 Powershell 2.0 中的有效参数
          【解决方案12】:

          使用下面的脚本可以实现更易读和更简单的方法:

          $Directory = "./"
          Get-ChildItem $Directory -Recurse | % {
              if ($_.Attributes -eq "Directory") {
                  Write-Host $_.FullName
              }
          }
          

          希望这会有所帮助!

          【讨论】:

            【解决方案13】:

            我的解决方案基于 TechNet 文章 Fun Things You Can Do With the Get-ChildItem Cmdlet

            Get-ChildItem C:\foo | Where-Object {$_.mode -match "d"}
            

            我在脚本中使用了它,效果很好。

            【讨论】:

              【解决方案14】:

              使用这个:

              Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"
              

              【讨论】:

                【解决方案15】:

                具体回答原始问题(使用 IO.FileAttributes):

                Get-ChildItem c:\mypath -Recurse | Where-Object {$_.Attributes -and [IO.FileAttributes]::Directory}

                不过,我确实更喜欢 Marek 的解决方案 (Where-Object { $_ -is [System.IO.DirectoryInfo] })。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-05-12
                  • 2014-01-08
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-01-06
                  相关资源
                  最近更新 更多