VBScript - 获取NTFS压缩文件的实际大小

时间:2014-04-07 14:02:10

标签: vbscript ntfs filesystemobject

我注意到"尺寸" NTFS压缩文件的属性实际上返回其未压缩的大小,我似乎无法找到一种方法来获取磁盘上的实际大小。

我需要此值才能知道包含压缩数据的特定文件夹的实际权重。

有没有办法在VBS中执行此操作?

谢谢!

2 个答案:

答案 0 :(得分:1)

以下是如何执行相同的程序,而无需单独搜索文件,使用“UserAccounts.CommonDialog”,这在所有操作系统中都不活动,而且,我清理了代码,因此它只显示大小磁盘而不是任何其他冗余信息。

享受。

Dim sFile : sFile = "C:\testenv\compressed\test.txt"
Dim oShell, oFSO, oEnv, oNet
Set oShell = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oEnv = oShell.Environment("Process")
Set oNet = WScript.CreateObject("WScript.Network")
Dim sTempFile, aText, i, aInfo
sTempFile = oFSO.GetAbsolutePathName(oFSO.GetTempName)
oShell.Run "%comspec% /c compact " & Chr(34) & sFile & Chr(34) & " > " & Chr(34) & sTempFile & Chr(34), 0, True
aText = Split(oFSO.OpenTextFile(sTempFile,1).ReadAll,vbCrLf)
If oFSO.FileExists(sTempFile) Then oFSO.DeleteFile sTempFile, True 
For i = 0 To UBound(aText)
    If InStr(aText(i),oFSO.GetBaseName(sFile)) Then
        aInfo = Split(Replace(aText(i),"=",":"), ":")
        If IsNumeric(Trim(aInfo(1))) Then
            WScript.Echo sFile & " : Size on Disk = " & FormatNumber(Trim(aInfo(1)), 0) & " bytes"
        End If
    End If 
Next

答案 1 :(得分:0)

您可以使用COMPACT.EXE dos命令获取磁盘上的文件大小和大小。

Option Explicit
 Dim oShell, oFSO, oEnv, oNet
 Set oShell = CreateObject("Wscript.Shell")
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 Set oEnv = oShell.Environment("Process")
 Set oNet = WScript.CreateObject("WScript.Network")
 Dim sFile, sTempFile, aText, i, aInfo
 sFile = fBrowseForFile
 sTempFile = oFSO.GetAbsolutePathName(oFSO.GetTempName)
 oShell.Run "%comspec% /c compact " & Chr(34) & sFile & Chr(34) & " > " & Chr(34) & sTempFile & Chr(34), 0, True
 aText = Split(oFSO.OpenTextFile(sTempFile,1).ReadAll,VbCrLf)
 If oFSO.FileExists(sTempFile) Then oFSO.DeleteFile sTempFile, True 
 For i = 0 To UBound(aText)
 If InStr(aText(i),oFSO.GetBaseName(sFile)) Then
 aInfo = Split(Replace(aText(i),"=",":"), ":")
 WScript.Echo sFile & " : Size = " & Trim(aInfo(0))
 WScript.Echo sFile & " : Size on Disk = " & Trim(aInfo(1))
 End If 
 Next
 Function fBrowseForFile()
 Dim sBrowsePath, sBrowseFilter, oBrowseDialog
 sBrowsePath = "C:\"
 sBrowseFilter = "All Files|*.*"
 Set oBrowseDialog = CreateObject("UserAccounts.CommonDialog")
 oBrowseDialog.Filter = sBrowseFilter
 oBrowseDialog.InitialDir = sBrowsePath
 oBrowseDialog.Flags = &H80000 + &H4 + &H8
 oBrowseDialog.ShowOpen
 fBrowseForFile = oBrowseDialog.FileName
 End Function

我找到了这个解决方案here