Directory traversal issue
I'm trying to write a function that calculates the size of a given directory tree. It's your run-of-the-mill directory traversal that simply adds the sizes of each file. It should be simple enough, but for some reason the function doesn't return the correct size for all folders. I'm not sure if it doesn't report large file sizes correctly or large folder sizes, or maybe it skips some folders - I don't know. Some folders it reports fine, and others not. Any ideas?
Code:
long CDirectoryInfo::GetDirectorySize(CString path)
{
WIN32_FIND_DATA data;
HANDLE hFind;
CString szCurrentPath = path + "\\*.*";
hFind = FindFirstFile((LPCTSTR)szCurrentPath, &data);
if(hFind == INVALID_HANDLE_VALUE)
{
return -1;
}
do
{
if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if((strcmp(data.cFileName, ".") != 0) && (strcmp(data.cFileName, "..") != 0))
{
szCurrentPath = path + "\\" + data.cFileName;
GetDirectorySize(szCurrentPath);
}
}
else
{
m_lDirectorySize += data.nFileSizeHigh * (MAXDWORD + 1) + data.nFileSizeLow;
}
}
while(FindNextFile(hFind, &data));
FindClose(hFind);
return m_lDirectorySize;
}