How to retrieve “Size On Disk” value of a file or  folder

Posted on 2004-08-31
Last Modified: 2013-12-25
I am using vb6.0 and Microsoft Scriptiong RunTime to retrieve File or folder Information as Follows:

Dim Fso As New Scripting.FileSystemObject
Dim Folder As Scripting.Folder
Dim Size As Long

Set Folder = Fso.GetFolder("C:\myfolder")

Size = Folder.Size

But it does not meet my requirements, I need the property “Size On Disk” instead of size , that could be retrieve by right clicking the object.

Does it possible in VB 6.0
Plz Help me on that…
Thanks in Advance
Question by:muntasirrahman
  • 4
  • 2
  • 2
  • +1

Expert Comment

ID: 11947934
This all depends on your operating system, size of disk and whether FAT32 or NTFS.
On my Windows 2000 NTFS system with a greater than 2gb hard drive has a 4kb cluster size
any file under 4096bytes will have a size on disk of 4096
ie a file on my disk is 32256 bytes its size on disk is therfore 32768 bytes (1024 * 32)

WINDOWS 2000 - other operating systems will likely be different.
Size of disk                    Cluster Size
512MB  or less               512 bytes
513MB  to 1024MB(1GB)  1024 bytes (1KB)
1025MB to 2048MB(2GB) 2048 bytes (2KB)
2049MB and larger         4096 bytes (4KB)

From this you would be able to create a calculation (ie if you know you have NTFS >2gb harddrive and Win2000 then a file size returned as 22400 bytes is 22528 bytes size on disk.

You should be able to find some sights on the net which lists each operating systems cluster sizes.

LVL 22

Expert Comment

ID: 11952705
You can get cluster size with GetDiskFreeSpace() API (

Then to calculate size on disk you can use:
size on disk = ((size \ cluster size) * cluster size) + cluster size

LVL 16

Expert Comment

ID: 11953486
No, you can't figure out "Size on Disk", as reported by right clicking the object solely from size & cluster sizes.  The file might be compressed, where the size on disk is nearly unrelated to actual size, compressed text files, for example, will have much smaller size on disk than file size.

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

LVL 16

Expert Comment

ID: 11953863
You can use the WinAPI function GetCompressedFileSize to return the actual number of bytes used to store a file.

Author Comment

ID: 11973953
HI  JohnBPrice
It does not solve my problem. It shows Following result:
I have a file following attribute (shown at right click)
Size               77 bytes (77 bytes)
Size on disk    4.00 KB (4,096 bytes)

I looking for(Size on disk    4.00 KB) but it shows 77
What’s the wrong with me…?

Author Comment

ID: 11973969
Hi Darko,
I used the following code
And shows following result

Path: C:\
 Sectors per Cluster: 0
 Bytes per sector: 512
 Number Of Free Clusters: 105859
 Total Number Of Clusters: 1146395
 Total number of bytes in path: 0
 Free bytes: 0

Then which one is cluster size?

 Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long

Private Sub Form_Load()

    Dim Sectors As Long, Bytes As Long, FreeC As Long, TotalC As Long, Total As Long, Freeb As Long
    'Retrieve information about the C:\
    GetDiskFreeSpace "C:\", Sectors, Bytes, FreeC, TotalC
    'Set graphic mode to persistent
    Me.AutoRedraw = True
    'Print the information to the form
    Me.Print " Path: C:\"
    Me.Print " Sectors per Cluster:" + Str$(Sector)
    Me.Print " Bytes per sector:" + Str$(Bytes)
    Me.Print " Number Of Free Clusters:" + Str$(FreeC)
    Me.Print " Total Number Of Clusters:" + Str$(TotalC)
    Total = rTotalc& * rSector& * rBytes&
    Me.Print " Total number of bytes in path:" + Str$(Total)
    Freeb = rFreec& * rSector& * rBytes&
    Me.Print " Free bytes:" + Str$(Freeb)
End Sub
LVL 16

Expert Comment

ID: 11974308
You still have to round up to the next cluster after you get the compressed size.
LVL 22

Expert Comment

ID: 11974322
Me.Print " Sectors per Cluster:" + Str$(Sector) <------- Change "Sector" to "Sectors"

then you multiply (bytes per sector) * (sectors per cluster) to get cluster size
LVL 16

Assisted Solution

JohnBPrice earned 20 total points
ID: 11974552
Note also that GetCompressedSize will return file size (not necessarily size on disk) for uncompressed files too, so you can always use GetCompressedSize without checking if the file is compressed, but you always have to bump up to the nearest cluster if it's not a multiple of cluster size.

For example I have a file whose size is 104,960, and GetCompressedSize returns 104,960 if the file is not compressed (not a multiple of 4096, my (and your) cluster size.)  Its size on disk is actually 106,496 bytes (the next cluster boundry),

Compressed, however, GetCompressedSize returns 81,920 which is a cluster boundry and is its size on disk.

I suspect any compressed file over a cluster will have a compressed size on a cluster boundry, whereas any file less than a cluster will not compress (no benefit to it) and GetCompressedSize will return the original file size, not size on disk, so you still have bump it to the next highest cluster size.


Accepted Solution

brianb99999 earned 30 total points
ID: 11986209
Hi muntasirrahman,

As I said in the first post, you need to find the size and the cluster size:
From this you would be able to create a calculation (ie if you know you have NTFS >2gb harddrive and Win2000 then a file size returned as 22400 bytes is 22528 bytes size on disk).

Something like:
size/clustersize = amount.
round amount up to nearest whole value.
amount*clustersize = size on disk.
eg a file on my disk is 32256 bytes its size on disk is therfore 32768 bytes (1024 * 32)
32256(file size)/4096 (cluster size)=7.875.
Rounded to 8 (always round up).
8*4096=32768 (size on disk).


Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 54
Updates not working for MS Windows 7 12 166
Add a task in Outlook from access 11 39
VB6 ListBox Question 4 44
Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question