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

发布于 2024-09-07 02:36:15 字数 307 浏览 6 评论 0 原文

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

Get-ChildItem c:\mypath -Recurse

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

dir /b /ad /s

I'm using PowerShell 2.0 and I want to pipe out all the subdirectories of a certain path. The following command outputs all files and directories, but I can't figure out how to filter out the files.

Get-ChildItem c:\mypath -Recurse

I've tried using $_.Attributes to get the attributes but then I don't know how to construct a literal instance of System.IO.FileAttributes to compare it to. In cmd.exe it would be

dir /b /ad /s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

始于初秋 2024-09-14 02:36:15

对于 PowerShell 3.0 及更高版本:

Get-ChildItem -Directory

您还可以使用别名 dirlsgci


对于PowerShell 版本低于 3.0:

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

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

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

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

For PowerShell 3.0 and greater:

Get-ChildItem -Directory

You can also use the aliases dir, ls, and gci


For PowerShell versions less than 3.0:

The FileInfo object returned by Get-ChildItem has a "base" property, PSIsContainer. You want to select only those items.

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

If you want the raw string names of the directories, you can do

Get-ChildItem -Recurse | ?{ $_.PSIsContainer } | Select-Object FullName
夕色琉璃 2024-09-14 02:36:15

在PowerShell 3.0中,更简单:

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

In PowerShell 3.0, it is simpler:

Get-ChildItem -Directory #List only directories
Get-ChildItem -File #List only files
全部不再 2024-09-14 02:36:15

使用

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:从 6.0 版开始,PowerShell 现在是跨平台的。跨平台版本最初称为 PowerShell Core,但自 PowerShell 7.0+ 以来,“Core”一词已被删除。

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

Use

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

If you prefer aliases, use

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

or

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

To recurse subdirectories as well, add -r option.

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

Tested on PowerShell 4.0, PowerShell 5.0 (Windows 10), PowerShell Core 6.0 (Windows 10, Mac, and Linux), and PowerShell 7.0 (Windows 10, Mac, and Linux).

Note: On PowerShell Core, symlinks are not followed when you specify the -r switch. To follow symlinks, specify the -FollowSymlink switch with -r.

Note 2: PowerShell is now cross-platform, since version 6.0. The cross-platform version was originally called PowerShell Core, but the the word "Core" has been dropped since PowerShell 7.0+.

Get-ChildItem documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem

千里故人稀 2024-09-14 02:36:15

更简洁的方法:

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

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

A cleaner approach:

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

I wonder if PowerShell 3.0 has a switch that only returns directories; it seems like a logical thing to add.

秋意浓 2024-09-14 02:36:15

使用:

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

Use:

dir -r | where { $_ -is [System.IO.DirectoryInfo] }
贵在坚持 2024-09-14 02:36:15

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

From PowerShell v2 and newer (k represents the folder you are beginning your search at):

Get-ChildItem $Path -attributes D -Recurse

If you just want folder names only, and nothing else, use this:

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

If you are looking for a specific folder, you could use the following. In this case, I am looking for a folder called myFolder:

Get-ChildItem $Path -attributes D -Recurse -include "myFolder"
心如狂蝶 2024-09-14 02:36:15

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

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

Less text is required with this approach:

ls -r | ? {$_.mode -match "d"}
猫七 2024-09-14 02:36:15

接受的答案提到

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

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

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

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

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

The accepted answer mentions

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

to get a "raw string".
But in fact objects of type Selected.System.IO.DirectoryInfo will be returned. For raw strings the following can be used:

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

The difference matters if the value is concatenated to a string:

  • with Select-Object suprisingly foo\@{FullName=bar}
  • with the ForEach-operator the expected: foo\bar
昔梦 2024-09-14 02:36:15

使用:

dir -Directory -Recurse | Select FullName

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

Use:

dir -Directory -Recurse | Select FullName

This will give you an output of the root structure with the folder name for directories only.

橘亓 2024-09-14 02:36:15

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

You'll want to use Get-ChildItem to recursively get all folders and files first. And then pipe that output into a Where-Object clause which only take the files.

# 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 ;
}
囍孤女 2024-09-14 02:36:15

使用:执行

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
  • 将结果保存到文件中:
    输出文件 c:\temp\mydirectorylist.csv

Use:

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

Which does the following

  • Get a list of directories in the target location:
    Get-ChildItem \\myserver\myshare\myshare\ -Directory
  • Extract only the name of the directories:
    Select-Object -Property name
  • Convert the output to CSV format:
    convertto-csv -NoTypeInformation
  • Save the result to a file:
    Out-File c:\temp\mydirectorylist.csv
ゃ人海孤独症 2024-09-14 02:36:15

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

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

希望这会有所帮助!

A bit more readable and simple approach could be achieved with the script below:

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

Hope this helps!

情何以堪。 2024-09-14 02:36:15

我的解决方案基于 TechNet 文章您可以使用 Get 做的有趣事情-ChildItem Cmdlet

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

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

My solution is based on the TechNet article Fun Things You Can Do With the Get-ChildItem Cmdlet.

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

I used it in my script, and it works well.

早茶月光 2024-09-14 02:36:15

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

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

返回FileInfoDirectoryInfo

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

Get-ChildItem | Get-Member
  • TypeName: System.IO.DirectoryInfo
  • TypeName: System.IO.FileInfo

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

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

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

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

This question is well and truly answered but thought I'd add something extra as I've just been looking at this.

Get-ChildItem happens to produce two types of objects whereas most commands produce just one.

FileInfo and DirectoryInfo are returned.

You can see this by viewing the 'members' available to this command like so:

Get-ChildItem | Get-Member
  • TypeName: System.IO.DirectoryInfo
  • TypeName: System.IO.FileInfo

You'll see the various methods and properties available to each type. Note that there are differences. For example that the FileInfo object has a length property but the DirectoryInfo object doesn't.

Anyway, technically, we can return just the directories by isolating the DirectoryInfo object

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

Obviously as the top answer states the most straightforward solution is to simply use Get-ChildItem -Directory but we now know how to work with multple object types in future :)

丿*梦醉红颜 2024-09-14 02:36:15

使用这个:

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

Use this one:

Get-ChildItem -Path \\server\share\folder\ -Recurse -Force | where {$_.Attributes -like '*Directory*'} | Export-Csv -Path C:\Temp\Export.csv -Encoding "Unicode" -Delimiter ";"
疑心病 2024-09-14 02:36:15

您可以尝试 PsIsContainer 对象

Get-ChildItem -path C:\mypath -Recurse | where {$_.PsIsContainer -eq $true} 

You can try the PsIsContainer Object

Get-ChildItem -path C:\mypath -Recurse | where {$_.PsIsContainer -eq $true} 
冷心人i 2024-09-14 02:36:15

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

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

不过,我确实更喜欢 Marek 的解决方案:

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

To answer the original question specifically (using IO.FileAttributes):

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

I do prefer Marek's solution though:

Where-Object { $_ -is [System.IO.DirectoryInfo] }
梦里梦着梦中梦 2024-09-14 02:36:15

尝试以下方法
dir -目录 -名称

TRY THE FOLLOWING
dir -Directory -Name

吃不饱 2024-09-14 02:36:15

这是我使用的方法,它为您提供了目录和文件名:

Get-ChildItem "c:\mypath" -Recurse |  ForEach-Object {
    $directory = Split-Path $_.Fullname -parent
    Write-Host "directory = $directory"
    $file = Split-Path $_.Fullname -leaf
    Write-Host "file = $file"
}

Here is what I use which gives you both the directory and the filename:

Get-ChildItem "c:\mypath" -Recurse |  ForEach-Object {
    $directory = Split-Path $_.Fullname -parent
    Write-Host "directory = $directory"
    $file = Split-Path $_.Fullname -leaf
    Write-Host "file = $file"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文