首页 文章

如何使用Get-ChildItem仅获取目录?

提问于
浏览
190

我正在使用PowerShell 2.0,我想要管理某个路径的所有子目录 . 以下命令输出所有文件和目录,但我无法弄清楚如何过滤掉文件 .

Get-ChildItem c:\mypath -Recurse

我已经尝试使用 $_.Attributes 获取属性,但后来我不知道如何构建 System.IO.FileAttributes 的文字实例来比较它 . 在 cmd.exe 它会

dir /b /ad /s

15 回答

  • 0

    使用这一个:

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

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

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

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

  • 1

    For PowerShell versions less than 3.0:

    Get-ChildItem 返回的 FileInfo 对象具有"base"属性 PSIsContainer . 您只想选择那些项目 .

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

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

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

    For PowerShell 3.0 and greater:

    dir -Directory
    
  • 44

    在PowerShell 3.0中,它更简单:

    Get-ChildItem -Directory #List only directories
    Get-ChildItem -File #List only files
    
  • 2
    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)上进行了测试 .

  • 160

    更清洁的方法:

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

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

  • 0

    使用:

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

    从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"
    
  • 2

    此方法需要较少的文本:

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

    使用:

    dir -Directory -Recurse | Select FullName
    

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

  • 19

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

    $Directory = "./"
    Get-ChildItem $Directory -Recurse | % {
        if ($_.Attributes -eq "Directory") {
            Write-Host $_.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

  • 6

    接受的答案提到了

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

    得到"raw string" . 但实际上将返回 Selected.System.IO.DirectoryInfo 类型的对象 . 对于原始字符串,可以使用以下内容:

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

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

    • Select-Object 令人惊讶 foo\@{FullName=bar}

    • ForEach -operator预期: foo\bar

  • 3

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

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

    我在我的脚本中使用它,它运行良好 .

  • 1

    您将要使用 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 ;
    }
    

相关问题