Solved

Size of folder

Posted on 2004-03-28
15
736 Views
Last Modified: 2007-12-19
How is it possible to get the size of the folder? I try to get it like a file's size:
----------------------------------------------------------------------
HANDLE hDirShared;
hDirShared = CreateFile("G:\\Shared",
            GENERIC_READ,
            FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_DIRECTORY,
            NULL);
int iDirSize = GetFileSize(hDirShared, NULL);
----------------------------------------------------------------------
but the returned value is (-1).
What must I do?
0
Comment
Question by:MainMa
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 4

Expert Comment

by:void_main
ID: 10701970
A folder does not have a size. It is an entry in the root directory.
(try to get the size of a fileNAME  it is an entry, too)

what you can do is accessing the MFT (I think you have Win XP) but I don't bet a size is stored there
or you count all entries in mft; look for its size and divide the two values (this give the average size of an entry)


hope I helped you
void_main
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10702740
Your CreateFile will fail before GetFileSize(). Did you check the handle of the file before calling GetFileSize() ?

Refer to MSDN for CreateFile() it says to open and directory you need to specify FILE_FLAG_BACKUP_SEMANTICS instead of FILE_ATTRIBUTE_DIRECTORY. With this you can open a directory I am sure.

But GetFileSize will always return 0 you need to recursively list and calculate the size yourself.

There is a script DLL SCRRUN.dll which implements this functionality maybe you can use that !!

0
 
LVL 3

Expert Comment

by:akalmani
ID: 10702766
Also there is no API to get the size of a directory. You need to write a recursive function which will traverse through all the directories and files and count the size in bytes.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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:nonubik
ID: 10704415
DWORD GetDirSize(LPCTSTR szDir)
{
  DWORD dwSize = 0;
  FileFind      Finder;
  CString      strPath(szDir);
  strPath += "\\*.*";
  BOOL      bFind = Finder.FindFile(strPath);
  while(bFind)
  {
    bFind = Finder.FindNextFile();
   
    // skip . and .. files; otherwise, we'd
    // recur infinitely!
    if (Finder.IsDots())
         continue;

    // if it's a directory, recursively search it
    if (Finder.IsDirectory())
      dwSize += GetDirSize(Fider.GetFilePath());
    else
      dwSize += Finder.GetLength();
  }

  return dwSize;
}
0
 

Author Comment

by:MainMa
ID: 10705721
To nonubik:

That's the list of errors (Visual C++ 6.0).
--------------------------------------------------------------------------------------------------------------
g:\development\scripts\remover\main.cpp(9) : error C2065: 'FileFind' : undeclared identifier
g:\development\scripts\remover\main.cpp(9) : error C2146: syntax error : missing ';' before identifier 'Finder'
g:\development\scripts\remover\main.cpp(9) : error C2065: 'Finder' : undeclared identifier
g:\development\scripts\remover\main.cpp(10) : error C2065: 'CString' : undeclared identifier
g:\development\scripts\remover\main.cpp(10) : error C2146: syntax error : missing ';' before identifier 'strPath'
g:\development\scripts\remover\main.cpp(10) : error C2065: 'strPath' : undeclared identifier
g:\development\scripts\remover\main.cpp(11) : error C2297: '+=' : illegal, right operand has type 'char [5]'
g:\development\scripts\remover\main.cpp(12) : error C2228: left of '.FindFile' must have class/struct/union type
g:\development\scripts\remover\main.cpp(16) : error C2228: left of '.FindNextFileA' must have class/struct/union type
g:\development\scripts\remover\main.cpp(20) : error C2228: left of '.IsDots' must have class/struct/union type
g:\development\scripts\remover\main.cpp(24) : error C2228: left of '.IsDirectory' must have class/struct/union type
g:\development\scripts\remover\main.cpp(25) : error C2065: 'Fider' : undeclared identifier
g:\development\scripts\remover\main.cpp(25) : error C2228: left of '.GetFilePath' must have class/struct/union type
g:\development\scripts\remover\main.cpp(27) : error C2228: left of '.GetLength' must have class/struct/union type
--------------------------------------------------------------------------------------------------------------
It seems that I must include a file... If I include <afx.h>, instead of having 14 errors, I have 25, and 'FileFind' is again an undeclared identifier.

What must I do?

MainMa
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10711501
My appologies, it was CFileFind instead of FileFind. And you should make a MFC project. Or include :
#include <afxwin.h>        // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxcmn.h>            // MFC support for Windows 95 Common Controls

If you don't want to use MFC, just tell and I'll fetch you another sample.
Enjoy
0
 
LVL 3

Expert Comment

by:akalmani
ID: 10711619
You need to change your projects settings such that it uses MFC. In the project settings General you must say "Use MFC in a Shared DLL"

There is a slight typo in the above code written by nonubik. Its CFileFind and not FileFind. One more typo in line
>>> dwSize += GetDirSize(Fider.GetFilePath());
Its Finder not Fider.

The logic presented by nonubik should work.

0
 

Author Comment

by:MainMa
ID: 10715036
To nonubik:
I will use this code in the program, written yet as Windows 32 Application. That's why I would like to have another simple which don't use MFC. In addition, I am not familiar with MFC, so if I would like to modify or to understand the code precisely, it seems that it will be easier for me to have a code without MFC.

To akalmani:
Thanks.
PS It's strange: I looked at errors that my compiler outputted, but I didn't see this one...
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10721800
MainMa,

You can use FindFirstFile(..), FindNextFile(..) and FindClose(..). See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/retrieving_and_changing_file_attributes.asp for more info. And to see if is a directory, use GetFileAttributes(..) and check against FILE_ATTRIBUTE_DIRECTORY.  IsDots() can be replaced with a strcmp(<filename>, "..") and "."

Enjoy
0
 
LVL 3

Accepted Solution

by:
akalmani earned 20 total points
ID: 10722020
//This code was found in MS newsgroups. I have modified it as per your needs. Now it uses
//Win32 API's to search and calculate the file size. The size return is in bytes. You need to divide by 1024 to get in KB or further divide it by 1024 to get it in MB.

//In main() you can call this function something like this...
long lSize = GetSize("C:\windows\");
//size in KB is lSize/1024;
//size in MB is lSize/(1024*1024);

long GetSize(LPCTSTR szDir)
{
   long lSize = 0;
   WIN32_FIND_DATA FileData;
   HANDLE hSearch = NULL;
   _TCHAR szPath[MAX_PATH] = _T("");     //MAX_PATH can be defined as 255

  _tcscpy(szPath, szDir);
  _tcscat(szPath, _T("*"));

   hSearch = FindFirstFile(szPath, &FileData);
   if(INVALID_HANDLE_VALUE != hSearch)
   {
        while(FindNextFile(hSearch, &FileData))
        {
               //Search for . and .. special files
      if((_tcsicmp(FileData.cFileName, _T(".")) == 0) ||
         (_tcsicmp(FileData.cFileName, _T("..")) == 0))
      {
          continue;
      }

                 //Check if it is directory
      if(FILE_ATTRIBUTE_DIRECTORY == FileData.dwFileAttributes)
      {
                 _tcscpy(szPath, szDir);
           _tcscat(szPath, FileData.cFileName);
           _tcscat(szPath, _T("\\"));

          lSize += GetSize(szPath);//Recursive call
      }
      else
      {
          lSize = lSize + FileData.nFileSizeLow;
      }
         }//End of while
     }//End of if(hSearch == INVALID_HANDLE_VALUE)
      
    //Close the search handle.
    FindClose(hSearch);

   return lSize;
}
0
 

Author Comment

by:MainMa
ID: 10724802
To akalmani:
Thank you, it works like I wanted.

Sorry to other people, especially to nonubik, who answered and who will not receive any points. I can accept only one answer...
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10724813
;) you can share points......
0
 

Author Comment

by:MainMa
ID: 10725871
I'm sorry! I didn't know that I can share points! How can I do it? If I accept one answer, there's no longer any button "Accept" for other answers...
0
 
LVL 16

Expert Comment

by:nonubik
ID: 10726840
It's ok, there were only 20 of them :)
You should carefuly read help on this site http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/help.jsp#hi69
But no less then 20 points can anyone get.... so I was joking ;)
Cheers
0
 

Author Comment

by:MainMa
ID: 10729709
Thanks for your help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

789 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