【问题标题】:What are the possible 'Mode' values returned by PowerShell's Get-ChildItem cmdlet?PowerShell 的 Get-ChildItem cmdlet 返回的可能“模式”值是什么?
【发布时间】:2011-02-08 23:41:55
【问题描述】:

当我在目录(或任何返回文件系统项的 cmdlet)上运行 PowerShell 的 Get-ChildItem 时,它会显示一个名为 Mode 的列,如下所示:

    Directory: C:\MyDirectory


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          2/8/2011  10:55 AM            Directory1
d----          2/8/2011  10:54 AM            Directory2
d----          2/8/2011  10:54 AM            Directory3
-ar--          2/8/2011  10:54 AM        454 File1.txt
-ar--          2/8/2011  10:54 AM       4342 File2.txt

我搜索并搜索了 Google 和我本地的 PowerShell 书籍,但找不到任何关于 Mode 列含义的文档。

Mode 列的可能值是什么?每个值的含义是什么?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    请注意,您看到的模式只是隐藏在Attributes 属性中的位域enum 的字符串表示形式。您可以通过简单地并排显示每个字母来弄清楚各个字母的含义:

    PS> gci|select mode,attributes -u
    
    Mode                Attributes
    ----                ----------
    d-----               Directory
    d-r---     ReadOnly, Directory
    d----l Directory, ReparsePoint
    -a----                 Archive
    

    无论如何,完整列表是:

    d - Directory
    a - Archive
    r - Read-only
    h - Hidden
    s - System
    l - Reparse point, symlink, etc.
    

    【讨论】:

    • Get-ChildItem 在我的用户目录中显示了很多我在文件资源管理器中看不到的项目,尽管打开了“隐藏项目”。似乎它们与我可以看到的隐藏项目(如 AppData)之间的区别在于它们具有该系统属性。有什么办法可以在文件资源管理器中看到这些?
    • 可执行文件的“x”在哪里?
    • @S.N.:这个答案早于 Linux 上的 PowerShell,Windows 没有可执行文件的文件系统属性。
    • 请注意,在问题中,Mode 是一个 5 个字符的字符串。这对应于我在带有 PowerShell 4.0 的 Server 2012 R2 上看到的内容。在这个答案中,模式为 6 个字符的字符串,我可以在 Windows 10 和 PowerShell 5.1 中看到。可能添加了“l”属性。不要像我一样检查Mode = "d-----"。这不会识别旧版本上的目录。
    • 这不是完整列表。真正的要长得多:[Enum]::GetValues("System.IO.FileAttributes")
    【解决方案2】:

    恕我直言,最能说明问题的是代码本身:

    if (instance == null)
    {
        return string.Empty;
    }
    FileSystemInfo baseObject = (FileSystemInfo) instance.BaseObject;
    if (baseObject == null)
    {
        return string.Empty;
    }
    string str = "";
    if ((baseObject.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
    {
        str = str + "d";
    }
    else
    {
        str = str + "-";
    }
    if ((baseObject.Attributes & FileAttributes.Archive) == FileAttributes.Archive)
    {
        str = str + "a";
    }
    else
    {
        str = str + "-";
    }
    if ((baseObject.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    {
        str = str + "r";
    }
    else
    {
        str = str + "-";
    }
    if ((baseObject.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
    {
        str = str + "h";
    }
    else
    {
        str = str + "-";
    }
    if ((baseObject.Attributes & FileAttributes.System) == FileAttributes.System)
    {
        return (str + "s");
    }
    return (str + "-");
    

    【讨论】:

    • @splattered:我猜是来自 PowerShell 程序集的反射器。
    • 附带说明:属性的完整列表比模式列中显示的五个标志要大得多,它们是在对象上填写的属性。试试这个:[Enum]::GetValues("System.IO.FileAttributes") ...
    【解决方案3】:

    这些是所有文件属性名,含义可以在here:

    PS C:\> [enum]::GetNames("system.io.fileattributes")
    ReadOnly
    Hidden
    System
    Directory
    Archive
    Device
    Normal
    Temporary
    SparseFile
    ReparsePoint
    Compressed
    Offline
    NotContentIndexed
    Encrypted
    

    【讨论】:

    • 虽然我很欣赏代码和链接,但这并不能解释“模式”列中看到的缩写,这是原始问题中的主题。因此,如果您有另一个资源显示“哪个缩写对应哪个属性”,那将是一个非常有用的编辑
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多