Question

I have noticed that the "size" property of an NTFS compressed file actually returns its uncompressed size and I can't seem to find a way to get the actual size on disk.

I would need this value in order to know the real weight of specific folders that contain compressed data.

Is there a way to do this in VBS ?

Thanks !

Was it helpful?

Solution

Here's how you execute the same program without having to search for a file individually, using a "UserAccounts.CommonDialog" which isn't active in all OS's anymore, also, I cleaned up the code so it only displays the size on disk and not any other redundant info.

Enjoy.

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

OTHER TIPS

You can use the COMPACT.EXE dos command to get the file size and size on disk.

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

I found this solution here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top