【问题标题】:How to list all files in a folder (do cmd's `dir /A`) in PowerShell?如何在PowerShell中列出文件夹中的所有文件(执行cmd的`dir / A`)?
【发布时间】:2021-06-25 18:40:25
【问题描述】:

在cmd中你可以运行C:\path> dir /A,它会列出一个文件夹中的所有文件(例如包括隐藏的.git/目录)。

您可以在 cmd 和 PowerShell 中运行 dir

您可以在 cmd 中运行 dir /A,但不能在 PowerShell 中运行 dir /A。它会抱怨:powershell error pic

同样,在 PowerShell 中,您可以像在 Linux bash 中一样运行 PS C:\path> ls,但不能像在所有 Linux 中那样运行 ls -a,或者像在 Ubuntu 中那样运行 la

这是为什么?我的意思是为什么微软会如此部分地实施事情?那么如何在 PowerShell 中列出文件夹中的所有文件,例如 cmd 中的 dir /A 或 bash 中的 ls -a

我遇到这种情况是因为我仍在 Windows 7 中运行这些,还是在 Windows 10 上相同?

【问题讨论】:

  • 试试dir -force。它只是Get-ChildItem 的别名,并不意味着参数兼容。
  • @zett42 是的,我刚刚发现 PowerShell 中的 dirls 实际上是来自 help dirhelp lsGet-ChildItem。无论如何,您的解决方案有效,我也认为ls -force 的工作原理相同。只是我只需要记住至少 3 种不同的方式在 cmd、PowerShell 和 Linux 中做“同样的事情”。
  • From : stackoverflow.com/questions/1479663/… cmd /r dir /a 也可以工作,可能还有许多其他解决方案或黑客。但我不认为它们是很好的解决方案......
  • 至于这个It's just that I just have to remember at least 3 different ways to do "same thing" in cmd, PowerShell, and Linux,为什么?只需避免使用 cmd.exe,始终留在 PS 中,在 Windows/Linux 上,坚持使用 PS 命令。您始终可以根据需要从 PS 中选择其他可执行文件。在此处查看详细信息:• PowerShell: Running Executables : https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

标签: powershell cmd windows-10 windows-7


【解决方案1】:

简短的回答是使用Get-ChildItem -Force 会给你DIR /A 的结果。虽然不应该将它放入脚本中,但如果您急于保存击键,则它是 gci -fo

但是,您可能需要更多详细信息。您可以使用 Get-ChildItem 和 Get-ItemProperty 返回的 System.Io.FileInfo 或 System.Io.DirectoryInfo 对象中的 Attributes 成员来获取它。

foreach ($f in [Enum]::GetValues([System.Io.FileAttributes])) { $f }            # attribute ames
foreach ($f in [Enum]::GetValues([System.Io.FileAttributes])) { $f.value__ }    # attribute values

$TheFile = 'C:\src\t.txt'
Get-ChildItem -Path $TheFile | Format-List -Property * -Force   # see all members

使用带有 Io.FileAttributes 枚举的二进制和运算符测试每个枚举属性。

if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::ReadOnly) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Hidden) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::System) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Directory) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Archive) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Device) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Normal) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Temporary) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::SparseFile) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::ReparsePoint) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Compressed) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Offline) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::NotContentIndexed) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::Encrypted) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::IntegrityStream) {'yes'}else{'no'}
if ((Get-ChildItem -Path $TheFile).Attributes -band [Io.FileAttributes]::NoScrubData) {'yes'}else{'no'}

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    • 2011-10-27
    • 1970-01-01
    • 2011-07-18
    • 2020-07-02
    相关资源
    最近更新 更多