我如何只有目录使用 Get-ChildItem?

我用 PowerShell 2.0 我想传送特定路径的所有子目录。 以下命令显示所有文件和目录,但我无法理解如何过滤文件。


Get-ChildItem c:\mypath -Recurse


我试着用
$_.Attributes

要接收属性,但然后我不知道如何构建文字副本
System.IO.FileAttributes

, 比较它。 在
cmd.exe

这将是


dir /b /ad /s
已邀请:

风见雨下

赞同来自:

对于版本 PowerShell 少于 3.0

:

一个东西
FileInfo

, 返回
Get-ChildItem

, 它有一个财产 "base",
PSIsContainer

. 您希望只选择这些元素。


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


如果您需要原始目录线条名称,则可以执行此操作


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


为了 PowerShell 3.0 和更高

:


dir -Directory

奔跑吧少年

赞同来自:

在 PowerShell 3.0 一切都很容易:


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

风见雨下

赞同来自:

利用


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/ 和
https://github.com/powershell/powershell
/Windows 10, Mac 和 Linux/.

小明明

赞同来自:

更清洁的方法:


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


我想知道是否有 PowerShell 3.0 仅返回目录的交换机; 添加似乎是合乎合理的。
</name_of_directory>

知食

赞同来自:

利用:


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

小明明

赞同来自:

以。。。开始 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"

奔跑吧少年

赞同来自:

通过这种方法,需要更少的文本:


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

八刀丁二

赞同来自:

利用:


dir -Directory -Recurse | Select FullName


这将为您的根结构提供仅适用于目录的文件夹的名称。

快网

赞同来自:

已接受的答复提到


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


获得 "raw string".
但实际上,类型的对象将被返回。
Selected.System.IO.DirectoryInfo

. 对于原始行,您可以使用以下内容:


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


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


Select-Object

奇怪
foo\@{FullName=bar}



ForEach

- 预期预期:
foo\bar

江南孤鹜

赞同来自:

利用:


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

君笑尘

赞同来自:

你想用

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

詹大官人

赞同来自:

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


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


希望它会有所帮助!

君笑尘

赞同来自:

我的决定是基于文章 TechNet "
https://technet.microsoft.com/ ... .aspx
".


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


我在我的场景中使用它,它很好。

詹大官人

赞同来自:

用这个:


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

知食

赞同来自:

专门回答原始问题 /使用 IO.FileAttributes/:


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


但是,我更喜欢马雷克的决定 /
Where-Object { $_ -is [System.IO.DirectoryInfo] }

/.

要回复问题请先登录注册