PowerShell 类似于“dir /a:d” (Win) 或“ls -d */” (重击)

发布于 2024-08-03 02:42:42 字数 350 浏览 6 评论 0原文

我只想使用 PowerShell 列出当前工作目录下的所有目录。这在 Bash shell

ls -d */ 

或 Windows 中的 cmd.exe

dir /a:d

中很容易实现:使用 PowerShell,但是我似乎无法使用单个命令来完成此操作。相反,我发现唯一有效的是:

ls | ? {$_Mode -like "d*"}

这似乎太冗长和复杂,而且我怀疑我不需要一个单独的Where子句。不过,Get-ChildItem 的帮助并没有明确说明如何过滤模式。谁能启发我吗?

I simply want to list all of the directories under my current working directory, using PowerShell. This is easy from a Bash shell:

ls -d */ 

or cmd.exe in Windows:

dir /a:d

Using PowerShell however I cannot seem to be able to do it with a single command. Instead the only the I've found that works is:

ls | ? {$_Mode -like "d*"}

That seems way too wordy and involved, and I suspect that I don't need a separate Where clause there. The help for Get-ChildItem doesn't make it clear how to filter on Mode though. Can anyone enlighten me?

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

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

发布评论

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

评论(7

夏至、离别 2024-08-10 02:42:42

这也有效:

ls | ?{$_.PsIsContainer}

毫无疑问,它比 bash 或 cmd.exe 更冗长一些。如果您想减少冗长,您当然可以在您的配置文件中添加一个函数和一个别名。我会看看是否能找到一种使用 -filter 的方法。

经过进一步调查,我认为除了创建自己的函数和别名之外,没有更简洁的方法可以做到这一点。您可以将其放入您的个人资料中:

function Get-ChildContainer
{
    param(
            $root = "."
          )
    Get-ChildItem -path $root | Where-Object{$_.PsIsContainer}
}

New-Alias -Name gcc -value Get-ChildContainer -force

然后将 ls 文件夹中的目录:

gcc C:\

此解决方案会受到一些限制,因为它无法处理任何花哨的内容,例如 -Include、-Exclude、-Filter、-Recurse 等,但您可以轻松地将其添加到函数中。

实际上,这是一个相当幼稚的解决方案,但希望如果您决定追求它,它将引导您走向正确的方向。老实说,我不会打扰你。在我个人看来,powershell 总体上更大的灵活性足以克服这种情况下的额外冗长。

This works too:

ls | ?{$_.PsIsContainer}

There is no doubt that it is a little more wordy than bash or cmd.exe. You could certainly put a function and an alias in your profile if you wanted to reduce the verbosity. I'll see if I can find a way to use -filter too.

On further investigation, I don't believe there is a more terse way to do this short of creating your own function and alias. You could put this in your profile:

function Get-ChildContainer
{
    param(
            $root = "."
          )
    Get-ChildItem -path $root | Where-Object{$_.PsIsContainer}
}

New-Alias -Name gcc -value Get-ChildContainer -force

Then to ls the directories in a folder:

gcc C:\

This solution would be a little limited since it would not handle any fanciness like -Include, -Exclude, -Filter, -Recurse, etc. but you could easily add that to the function.

Actually, this is a rather naive solution, but hopefully it will head you in the right direction if you decide to pursue it. To be honest with you though I wouldn't bother. The extra verbosity in this one case is more than overcome by the overall greater flexibility of powershell in general in my personal opinion.

撧情箌佬 2024-08-10 02:42:42

尝试:

ls | ? {$_.PsIsContainer}

Try:

ls | ? {$_.PsIsContainer}
撞了怀 2024-08-10 02:42:42
dir -Exclude *.*

我发现这比 Plus 更容易记住

dir | ? {$_.PsIsContainer}

,输入速度更快,因为您可以使用 -ex 而不是 -exclude 或使用 tab 来展开它。

dir -Exclude *.*

I find this easier to remember than

dir | ? {$_.PsIsContainer}

Plus, it is faster to type, as you can do -ex instead of -exclude or use tab to expand it.

硪扪都還晓 2024-08-10 02:42:42

您现在可以使用 Get-ChildItem -Directory 或简称 ls -dir。根据 微软的文档

You can now use Get-ChildItem -Directory or ls -dir for short. This has existed at least since PowerShell 3.0 according to Microsoft's documentation.

我还不会笑 2024-08-10 02:42:42

您可以查看 PowerShell 团队博客上的旧帖子: http: //blogs.msdn.com/powershell/archive/2009/03/13/dir-ad.aspx

You can check old post on PowerShell team blog: http://blogs.msdn.com/powershell/archive/2009/03/13/dir-a-d.aspx

匿名的好友 2024-08-10 02:42:42

我来到这个帖子是因为我想弄清楚“FOR /D”是如何工作的。实际上如何使用带有 /D 选项的批处理命令转义(%)。

我满怀希望地阅读了上述内容,说实话,它们都比 FOR 命令选项复杂得多——当然,如果它有效的话。


使用 for 的其他形式

如果启用命令扩展(即默认),则支持以下附加形式的 for:

仅限目录

如果 set 包含通配符(* 和 ?),则针对与 set 匹配的每个目录(而不是指定目录中的一组文件)执行指定的命令。语法为:

对于 /D {%% | %}(设置)do 命令中的变量 [CommandLineOptions]

I came to this thread because I'm trying to figure out how "FOR /D" works. Well actually how to use the batch-command escape(%) with the /D option.

I read the above items with hope, to be honest they're all a lot more complex than the FOR command option -- If it worked of course.


Using additional forms of for

If command extensions are enabled (that is, the default), the following additional forms of for are supported:

Directories only

If set contains wildcards (* and ?), the specified command executes for each directory (instead of a set of files in a specified directory) that matches set. The syntax is:

for /D {%% | %}variable in (set) do command [CommandLineOptions]

蓝天白云 2024-08-10 02:42:42

好吧,很快我们就必须创建一个类来简化事情并输入类似的命令
dir -options {122b312aa3132-1313-131112f8111111} 这样我们就可以执行与

dir/ad/od相同的操作

Well, soon we will have to a make a class to simplify things and be typing commands like
dir -options {122b312aa3132-1313-131112f8111111} just so we can do the same as

dir/ad/od

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文