ChatGPT解决这个技术问题 Extra ChatGPT

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

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

Get-ChildItem c:\mypath -Recurse

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

dir /b /ad /s

K
Kshitij Agarwal

对于 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

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

在 PowerShell 3.0 中,它更简单:

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

dir 是 Get-ChildItem 的别名
@crashmstr 你确定吗?我检查了我的 PS4.0。对我来说,dirGet-ChildItem 的别名,-Directory-File 选项按描述工作。我使用命令 echo $PSVersionTablehelp dirdir -Directorydir -File 来提出这条评论。
这应该是答案
lsdirGet-ChildItem 的别名选择你的毒药
S
Sachin Joseph

利用

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 中的有效参数
P
Peter Mortensen

更清洁的方法:

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

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


仅供参考 powershell 3.0 添加了 -Directory-File 标志
P
Peter Mortensen

利用:

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

P
Peter Mortensen

从 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 中似乎没有 Attributes 参数,它给出了错误“找不到与参数名称'Attributes'匹配的参数”。在 PS3 中运行正常。
O
OneOfThePetes

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

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

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

接受的答案提到

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

得到一个“原始字符串”。但实际上会返回 Selected.System.IO.DirectoryInfo 类型的对象。对于原始字符串,可以使用以下内容:

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

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

与 Select-Object 令人惊讶的是 foo\@{FullName=bar}

使用 ForEach-operator 的预期: foo\bar


Select-Object 实际上会返回 PSCustomObject 类型的对象。虽然您可以像使用 %(即 ForEach-Object)一样获取原始字符串,但您也可以使用 Select-Object -ExpandProperty FullName
P
Peter Mortensen

利用:

dir -Directory -Recurse | Select FullName

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


s
sonjz

您需要首先使用 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 ;
}

U
Underverse

利用:

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 中的有效参数
Z
Zorayr

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

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

希望这可以帮助!


P
Peter Mortensen

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

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

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


u
user5947282

这个问题得到了很好的回答,但我想我会添加一些额外的东西,因为我一直在看这个。

Get-ChildItem 恰好产生 两种 类型的对象,而大多数命令只产生一种。

返回 FileInfo 和 DirectoryInfo。

您可以通过查看此命令可用的“成员”来查看这一点,如下所示:

Get-ChildItem | Get-Member

类型名称:System.IO.DirectoryInfo

类型名称:System.IO.FileInfo

您将看到可用于每种类型的各种方法和属性。请注意,存在差异。例如,FileInfo 对象具有长度属性,但 DirectoryInfo 对象没有。

无论如何,从技术上讲,我们可以通过隔离 DirectoryInfo 对象只返回目录

Get-ChildItem | Where-Object {$_.GetType().Name -eq "DirectoryInfo"}

显然,正如最佳答案所述,最直接的解决方案是简单地使用 Get-ChildItem -Directory 但我们现在知道将来如何使用多种对象类型:)


这可能不是最简单的方法,但它是一个有用的见解,谢谢!
P
Peter Mortensen

使用这个:

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

N
Nicolas Melay

要专门回答原始问题(使用 IO.FileAttributes):

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

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


关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅