• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 786
  • Last Modified:

Size of folder

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
MainMa
Asked:
MainMa
  • 5
  • 5
  • 4
  • +1
1 Solution
 
void_mainCommented:
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
 
akalmaniCommented:
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
 
akalmaniCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nonubikCommented:
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
 
MainMaAuthor Commented:
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
 
nonubikCommented:
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
 
akalmaniCommented:
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
 
MainMaAuthor Commented:
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
 
nonubikCommented:
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
 
akalmaniCommented:
//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
 
MainMaAuthor Commented:
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
 
nonubikCommented:
;) you can share points......
0
 
MainMaAuthor Commented:
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
 
nonubikCommented:
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
 
MainMaAuthor Commented:
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.

  • 5
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now