Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

Please tell me how to get the files size in specific directory. I read GetDiskFreeSpace() it will return available free space in given drive like C:\. I want to calculate how much size the files occupied in c:\MyFolder. I have number of files in MyFolder, I want to calculate the total no of files size. Is there any function to get the size of a directory. Please tell me.

Thank u all.
Posted
Updated 15-Oct-12 19:59pm
v2

XML
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main (void)
{
  DIR *dp;
  int i;
  struct dirent *ep;
  dp = opendir ("./");

  if (dp != NULL)
  {
    while (ep = readdir (dp))
      i++;

    (void) closedir (dp);
  }
  else
    perror ("Couldn't open the directory");

  printf("There's %d files in the current directory.\n", i);

  return 0;
}
 
Share this answer
 
Comments
vallikelam 16-Oct-12 2:01am    
Hello Mukesh kumar thanks for your response. I want total size of files in a directory.
Firstly, you need to get the total no. of files. Then get size of each file.

To get total no. of files, you can use FindFirstFile/FindNextFile (FindNextFile needs to be called in a loop, until all files have been processed).

See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx[^] for a sample using FindFirstFile/FindNextFile.

Then use GetFileSizeEx to get size of each file.

If you are using MFC, you can use CFileFind class instead of FindFirstFile and FindNextFile.
 
Share this answer
 
http://www.codeproject.com/Articles/19142/XFolderSize-A-class-to-determine-folder-size.
If you can use Win32, this might be helpful to get the file size.
 
Share this answer
 
Comments
vallikelam 17-Oct-12 8:31am    
Hi I downloaded this file and try to build the application it is showing below error
error C2065: 'GetFileSizeEx' : undeclared identifier
My system is running on windows 7, using VS 6 and also i included the Kernel32.lib in project settings still I am getting the error. Please solve the problem.
Santhosh G_ 17-Oct-12 9:09am    
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364957(v=vs.85).aspx
Please check Community Additions section, Details of using this API with VS 6.0 is explained. You have to use LoadLibrary to access this library,
vallikelam 18-Oct-12 2:59am    
Thanks a lot its working fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900