Hi guys,

Im doing this project in VC++ 2005.

I cannot seem to get the size for my directory. No error is returned when I use GetLastError();
Both Low and High are coming out as 0.
I do get a handle value for MF ( dunno how to verify if it is a handle for the same directory, but im guessing it should be).

I read in MSDN, FILE_FLAG_BACKUP_SEMANTICS flag should be set for getting a handle for a directory.

still i dont know why im always getting 0 as the size.

HANDLE MF = CreateFile(_T("D:\\mp3Test"), GENERIC_READ, FILE_SHARE_READ, NULL,
                          OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, NULL);
   DWORD High=0, Low=0;
   if ( MF != INVALID_HANDLE_VALUE )
       Low = GetFileSize(MF, &High);

   DWORD lastErr = GetLastError();

  CloseHandle(MF);

any help would be greatly appreciated.

thanks

Worked for me. I don't know if the size is correct or not but I got 4096 for the directory size. All I did was change the path to one that's on my computer. I'm running Windows 7, VC++ 2008 Express if that matters.

Edited 6 Years Ago by Ancient Dragon: n/a

Hi,

The return value is zero, because the size of the folder itself is zero. If you need to find the size of all the contents within a directory, that's a different problem to solve.

I realise the documentation for GetFileSize() could have been a little more helpful stating how it behaves when the file handle passed to it is a directory.

You'll need to "manually" find the size of each files within the folder, and add them up. If there are sub folders, you'll have to recurse. Not a terribly difficult task, but you might as well use something like XFolderSize - A class to determine folder size. Take a good look into the code there, that might help you figure things if you want to write your own routine.

I do wish MS had written a GetFolderSize() call as well. Bunch of lazy folks, ain't they? :)

The same thing can be achieved by calling FindFirstFile() and FindNextFile(). Here is a function that will do everything except sum up the file sizes. And that feature could be easily added.

On second thought, that may not be so easy to change for new programmers. So here is a version that will return the size of all the files in the folder and its sub-folders.

thanks for the inputs guys..
Yea I guess there is no function to get the folder size directly, but im surprised. Im sure there would be some underlying data structure in the kernel which stores size of the directory.

Going through all the files defeats my purpose. I wanted to create a progress bar in an MFC app for which I needed the total size of the folder and then as and when I recurse through each subfolder or file, I would update the progress bar accordingly.

Initially I thought I would get a count of all items in a folder by some function, and then depending on the number of items(subfolders or files) completed, I would update the progress bar. But turned out there is no function to count items in a folder too, other than recursing through all using FindFirst....etc

So do you people have any other idea to get either a count or total size of the folder without recursing through it.

thanks

thanks for the inputs guys..
Yea I guess there is no function to get the folder size directly, but im surprised. Im sure there would be some underlying data structure in the kernel which stores size of the directory.

Well, be surprised, but that's how it is. There's no such underlying datastructure within the Kernel which keeps the folder sizes stored. Been there, seen that. Trust me. :)

Going through all the files defeats my purpose. I wanted to create a progress bar in an MFC app for which I needed the total size of the folder and then as and when I recurse through each subfolder or file, I would update the progress bar accordingly.

Initially I thought I would get a count of all items in a folder by some function, and then depending on the number of items(subfolders or files) completed, I would update the progress bar. But turned out there is no function to count items in a folder too, other than recursing through all using FindFirst....etc

So do you people have any other idea to get either a count or total size of the folder without recursing through it.

thanks

Well, what is the "purpose" anyways and why is defeated with our answers here? I linked you to XFolderSize, and another poster has given you with a niche little piece of code which will suit the purpose well. If you need to update the UI, then do the entire calculation from within a background worker thread and update the progressbar in the UI accordingly.

Post a query here if you get stuck somewhere.

Getting the number of files, or folder size, by either of the two previously posted methods goes pretty quickly (depending on file system speed of course). If what you have in mind doing takes a considerable amount of time then you will probably not see only very little pause while counting the files. Of course that will depend on how many folders you want to process, such as the entire C: drive.

well i guess my fears were unfounded, it took like 5 seconds to find the size of the folder i wanted , around 17 GB....in fact when I ran it again as i was writing this, it took less than a second....what do you think was the reason? cache?

thanks for the ideas.

well i guess my fears were unfounded, it took like 5 seconds to find the size of the folder i wanted , around 17 GB....in fact when I ran it again as i was writing this, it took less than a second....what do you think was the reason? cache?

thanks for the ideas.

There's no "cache" involved.

It would take hardly a second because the entire operation is just mathematics (addition of sizes of files). In fact it must complete in virtually no time at all. The one second taken is probably to update the UI (if you're showing the progress).

This article has been dead for over six months. Start a new discussion instead.