The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: fearless on October 31, 2007, 09:43:58 AM

Title: How to get total number of files on a partition
Post by: fearless on October 31, 2007, 09:43:58 AM
Hi All,

I've been coding an application that utilizes FindFirst / FindNext calls to recursively traverse the entire c: drive - lots of useful information relating to it was found on these forums. Cheers for that. :D  :U

My question is:

Is there a way of determining the total number of files on a disk/partition by utilizing some low level access, like using the DeviceIO function to read this information from somewhere?. I've spent a good amount of time on google, looking for "Win32 total file count", "total files on disk", "total files on drive" "code number of files on partition" etc etc - lots of variations. The only code/examples I've found relate to doing a recursive walk of the files/folders to total up that information. Surely there is a way to determine beforehand this information? I've briefly looked at NTFS and FAT structures to see if the information can be calculated from these. I just cant seem to find anything at the moment. Hopefully someone can point me in the right direction.

I want to use the total files count to present a progress bar. My program will be using other disks later - its only traversing the c: drive at the moment, but it would be handy to correctly show the progress of the recursive walk. Currently i have had to settle with the Marquee effect of the progress bar to indicate that something is happening, but obviously this does not indicate how long left to wait.
Title: Re: How to get total number of files on a partition
Post by: MichaelW on October 31, 2007, 09:57:19 AM
At least for Windows 2000, and with my configuration, it looks to me like Windows is doing a recursive search to count the files and directories, because for a directory of moderate size the counts start small and gradually increase to their final values.
Title: Re: How to get total number of files on a partition
Post by: sinsi on October 31, 2007, 09:58:59 AM
For the progress thing, use GetDiskFreeSpaceEx to find the total used bytes, then as you recurse keep a running total of file bytes.
Title: Re: How to get total number of files on a partition
Post by: fearless on October 31, 2007, 10:19:08 AM
Cheers for that sinsi

The WIN32_FIND_DATA structure returns the file size and i can add this to my count. And i can use GetDiskFreeSpaceEx on each disk and total up for the whole systems combined maximum for the progress bar. Nice! :D

I was thinking i might have had to create another thread that looked/scanned ahead and updated the total file count as i went. Obviously i would be putting more workload on the CPU by doing so and slowing down the entire process, this way i can get the info at the start of the program and then still do the recursive scan and update the progress bar each time i count a file.

I presume ill need a QWORD to store the maximum of all free space, or will i need a bigger defined variable? Also with the nFileSizeHigh and nFileSizeLow fields of WIN32_FIND_DATA structure, how do i add these properly together to my QWORD variable. I have a feeling that it isnt as simple as:

add myqword, nFileSizeHigh
add myqword, nFileSizeLow

Title: Re: How to get total number of files on a partition
Post by: sinsi on October 31, 2007, 11:39:20 AM
Try something like

user_free     dd 0,0
total_bytes   dd 0,0
total_free    dd 0,0
running_total dd 0,0

drive_string  db 'c:\',0

    ...
    invoke GetDiskFreeSpace,offset drive_string,offset user_free,offset total_bytes,offset total_free
    ...
    mov eax,wfd.nFilesizeLow
    mov edx,wfd.nFilesizeHigh
    add running_total,eax
    adc running_total+4,edx
    ...


Something to beware of - Computing the size of a directory is more than just adding file sizes (http://blogs.msdn.com/oldnewthing/archive/2004/12/28/336219.aspx)