Solved

Size of folder

Posted on 2004-03-28
15
738 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
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!

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 97
ASP.net build a IF/Then Walkthrough Guide 1 283
Compile GLUT with Visual Studio 2015 1 209
VS2015 Redefinition errors 4 91
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 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