Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • Last Modified:

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

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
0
muntasirrahman
Asked:
muntasirrahman
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
brianb99999Commented:
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.

Brian.
0
 
DarkoLordCommented:
You can get cluster size with GetDiskFreeSpace() API (http://www.mentalis.org/apilist/GetDiskFreeSpace.shtml)

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

Darko
0
 
JohnBPriceCommented:
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.



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JohnBPriceCommented:
You can use the WinAPI function GetCompressedFileSize to return the actual number of bytes used to store a file.
0
 
muntasirrahmanAuthor Commented:
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…?
0
 
muntasirrahmanAuthor Commented:
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
0
 
JohnBPriceCommented:
You still have to round up to the next cluster after you get the compressed size.
0
 
DarkoLordCommented:
Me.Print " Sectors per Cluster:" + Str$(Sector) <------- Change "Sector" to "Sectors"

then you multiply (bytes per sector) * (sectors per cluster) to get cluster size
0
 
JohnBPriceCommented:
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.


0
 
brianb99999Commented:
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).

Brian.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now