Solved

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

Posted on 2004-08-31
10
570 Views
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
0
Comment
Question by:muntasirrahman
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:brianb99999
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.

Brian.
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11952705
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
 
LVL 16

Expert Comment

by:JohnBPrice
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.



0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 16

Expert Comment

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

Author Comment

by:muntasirrahman
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…?
0
 

Author Comment

by:muntasirrahman
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
0
 
LVL 16

Expert Comment

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

Expert Comment

by:DarkoLord
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
0
 
LVL 16

Assisted Solution

by:JohnBPrice
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.


0
 
LVL 5

Accepted Solution

by:
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).

Brian.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 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