Solved

Size of folder

Posted on 2004-03-28
15
728 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now