在 vbscript 中获取扩展文件属性(如加密、离线......)

Get extended file attributes in vbscript (like encrypted, offline ...)

在 C++ 中有方法 GetFileAttributes 可以读取文件的文件属性。有一些随着时间的推移而增加。如果我使用 VBscript 访问属性,我只能得到低 16 位。

CreateObject("Scripting.FileSystemObject").GetFile("C:\hiberfil.sys").Attributes

dos 命令行工具 attrb.exe 也能够读取这些属性,所以我使用 dos 工具编写了一个包装器。

真的没有其他方法可以访问那些扩展文件属性吗?

这是我的包装代码

Option Explicit

dim filepath : filePath = "C:\hiberfil.sys"
MsgBox CreateObject("Scripting.FileSystemObject").GetFile(filePath).Attributes & VBCRLF & GetFileAttributes(filePath)


function GetFileAttributes(byref path)
  dim gfa : set gfa = new CGetFileAttributes
  GetFileAttributes = gfa.Attrib(path)
end function

class CGetFileAttributes

  function Attrib(byref path)

    Attrib = 0

    dim cmd : cmd = "%comspec% /c attrib.exe """ & path & """ > """ & tmpFile_ & """"
    call wsh_.Run(cmd, 0, true)
    dim strAttr : strAttr = fso_.OpenTextFile(tmpFile_, 1, false, 0).ReadLine
    strAttr = Left(strAttr, 12)

    if 0 <> instr(strAttr, "R") then Attrib = Attrib or 1  ' FILE_ATTRIBUTE_READONLY
    if 0 <> instr(strAttr, "H") then Attrib = Attrib or 2 ' FILE_ATTRIBUTE_HIDDEN
    if 0 <> instr(strAttr, "S") then Attrib = Attrib or 4  ' FILE_ATTRIBUTE_SYSTEM
    if 0 <> instr(strAttr, "A") then Attrib = Attrib or 32 ' FILE_ATTRIBUTE_ARCHIVE
    if 0 <> instr(strAttr, "N") then Attrib = Attrib or 128 ' FILE_ATTRIBUTE_NORMAL
    if 0 <> instr(strAttr, "T") then Attrib = Attrib or 256   ' FILE_ATTRIBUTE_TEMPORARY
    if 0 <> instr(strAttr, "P") then Attrib = Attrib or 512 ' FILE_ATTRIBUTE_SPARSE_FILE
    if 0 <> instr(strAttr, "L") then Attrib = Attrib or 1024 ' FILE_ATTRIBUTE_REPARSE_POINT
    if 0 <> instr(strAttr, "C") then Attrib = Attrib or 2048 ' FILE_ATTRIBUTE_COMPRESSED
    if 0 <> instr(strAttr, "O") then Attrib = Attrib or 4096  ' FILE_ATTRIBUTE_OFFLINE
    if 0 <> instr(strAttr, "I") then Attrib = Attrib or 8192 ' FILE_ATTRIBUTE_NOT_CONTENT_INDEXED
    if 0 <> instr(strAttr, "E") then Attrib = Attrib or 16384 ' FILE_ATTRIBUTE_ENCRYPTED

  end function

  Private Sub Class_Initialize()
    set wsh_ = CreateObject("WScript.Shell")
    set fso_ = CreateObject("Scripting.FileSystemObject")
    tmpFile_ = wsh_.ExpandEnvironmentStrings("%TEMP%") & "\attrib_" & replace(replace(replace(replace(CStr(Now), ".", "_"), ":", "_"), " ", "_"), "/", "_") & ".tmp"
  End Sub

  private sub DeleteTmpFile_()
    on error resume next
    fso_.DeleteFile(tmpFile_)
    on error goto 0
  end sub

  Private Sub Class_Terminate()
    DeleteTmpFile_()
  End Sub

  private tmpFile_
  private wsh_
  private fso_

end class

使用 WMI。来自 CIM_Datafile 的帮助 - https://msdn.microsoft.com/en-us/library/aa387236(v=vs.85).aspx

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where FileName Like '%~%'")
For Each objFile in colFiles
   Wscript.Echo objFile.Name
Next

或在命令行

wmic datafile where name^="c:\windows\explorer.exe" get version