【问题标题】:There are some (hopefully faster) alternatives for dir on windows?Windows 上的 dir 有一些(希望更快)替代品吗?
【发布时间】:2023-01-25 03:06:06
【问题描述】:

本质上,标题

我认为通常 Windows 上的 dir 命令过于冗长和缓慢。

(而且我开始认为也许自己写一个替代方案是一种选择)

【问题讨论】:

  • 你指的是来自 powershell 的 dir (Get-ChildItem) 还是 dir 命令?
  • 您当然可以自己编写,但不能保证它会更快。
  • 太冗长和缓慢?模式dir /B(裸机)怎么样?

标签: windows powershell terminal


【解决方案1】:

这取决于你需要什么。你只想要文件名吗?你想要目录和文件吗?是否需要递归?如果您只是查看基本的 dir,那么在 PowerShell 的上下文中,您可以使用 DirectoryInfo 类的 GetFiles() 方法更快地获得结果。我使用 Get-ChildItem 以及 GetDirectories()GetFiles() 在我的系统上拉了 C:WindowsSystem32 1000 次,.Net 方法要快得多(大约快 7 倍)。

1..1000|%{Measure-Command -Expression {Get-ChildItem C:WindowsSystem32}} |Measure-Object -Average -Property TotalMilliseconds |% Average
142.9717982
1..1000|%{Measure-Command -Expression {([System.IO.DirectoryInfo]'C:WindowsSystem32').GetDirectories();([System.IO.DirectoryInfo]'C:WindowsSystem32').GetFiles()}} |Measure-Object -Average -Property TotalMilliseconds |% Average
19.9039127

同样,这只是让您获得单个路径的目录名和文件名,但如果仅此而已,这是一个不错的选择。

【讨论】:

    猜你喜欢
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2022-10-07
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多