代码之家  ›  专栏  ›  技术社区  ›  Peter Hull

如何使用get ChildItem仅获取目录?

  •  200
  • Peter Hull  · 技术社区  · 16 年前

    Get-ChildItem c:\mypath -Recurse
    

    我试过用 $_.Attributes 但是我不知道如何构造 System.IO.FileAttributes cmd.exe

    dir /b /ad /s
    
    15 回复  |  直到 13 年前
        1
  •  317
  •   Oskar Austegard zaph    5 年前

    FileInfo 返回的对象 Get-ChildItem PSIsContainer . 您只想选择那些项目。

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

    如果需要目录的原始字符串名称,可以

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

    对于PowerShell 3.0及更高版本:

    Get-ChildItem -Directory
    

    dir , ls gci

        2
  •  209
  •   Marc    8 年前

    在PowerShell 3.0中,它更简单:

    Get-ChildItem -Directory #List only directories
    Get-ChildItem -File #List only files
    
        3
  •  54
  •   Sachin Joseph    6 年前

    使用

    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 PowerShell 7.0

    注意 :在PowerShell Core上,指定 开关。要跟随符号链接,请指定 -FollowSymlink .

    附注2

    获取子项文档: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem

        4
  •  22
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    更干净的方法:

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

        5
  •  12
  •   Peter Mortensen Pieter Jan Bonestroo    8 年前

    dir -r | where { $_ -is [System.IO.DirectoryInfo] }
    
        6
  •  7
  •   Peter Mortensen Pieter Jan Bonestroo    10 年前

    从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"
    
        7
  •  6
  •   OutOfThisPlanet EBGreen    7 年前

    这种方法需要较少的文本:

    ls -r | ? {$_.mode -match "d"}
    
        8
  •  4
  •   Cwt    12 年前

    公认的答案提到

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

    获得“原始字符串”。 但事实上 Selected.System.IO.DirectoryInfo

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

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

    • 具有 Select-Object foo\@{FullName=bar}
    • ForEach -应为操作员: foo\bar
        9
  •  4
  •   Peter Mortensen Pieter Jan Bonestroo    10 年前

    dir -Directory -Recurse | Select FullName
    

        10
  •  3
  •   sonjz    10 年前

    获取子项 以递归方式首先获取所有文件夹和文件。然后通过管道将输出数据传输到 Where对象

    # 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
  •  3
  •   Underverse    10 年前

    用途:

    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
        12
  •  2
  •   Zorayr    12 年前

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

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

    希望这有帮助!

        13
  •  1
  •   Peter Mortensen Pieter Jan Bonestroo    10 年前

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

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

    我在剧本里用过,效果不错。

        14
  •  0
  •   Peter Mortensen Pieter Jan Bonestroo    11 年前

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

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

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

    Where-Object { $_ -is [System.IO.DirectoryInfo] }

    推荐文章