在vbscript中获取扩展文件属性(如加密,离线...)
问题描述:
在C++中有meathod GetFileAttributes,它能够读取文件的文件属性。有一些随着时间的推移而增加。如果我使用VBscript来访问属性,我只能得到低16位。在vbscript中获取扩展文件属性(如加密,离线...)
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