在vbscript中获取扩展文件属性(如加密,脱机......)

时间:2016-07-08 07:29:02

标签: vbscript filesystems

在C ++中,有一些能够读取文件文件属性的meathod 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

1 个答案:

答案 0 :(得分:1)

使用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