About get the information of a folder
Hello, everyone!
This is Echo. I want get the size, file numbers of a folder. i made
following code. It works for most folders. But for larger folder, there is
problem.
The problem is: if I choose a larger folder, such as C or other drives, the
result is different to the results obtained by windows OS. For example,the C
dirve on my PC, if i select all the folder and file in C and right-click
"properties", windows OS gives the result is 133,055 files, 18.0G. But, my
code gives result is 148,368 files and 27G.
I cannot find any error in my code. Can you can help me? Thank you in
advance!!!
unsigned long SizeofFolder(CString path, DWORD& gb, DWORD& mb, DWORD& kb)
{
DWORD num=0;
gb=mb=kb=0;
CStringArray dirs;
CString searchname;
CFileFind find;
dirs.Add(path);
BOOL bRet;
while(dirs.GetSize()>0)
{
searchname = dirs[0] +"\\*.*";
dirs.RemoveAt(0);
bRet = find.FindFile (searchname,0);
if(!bRet)continue;
do{
bRet = find.FindNextFile ();
if(find.IsDots ())
continue;
if(find.IsDirectory ())//This is a folder
{
dirs.Add (find.GetFilePath());
continue;
}
else//This is a file
{
num++;
kb=kb+find.GetLength();//bytes
if(kb>=1048576)//1M byte
{
DWORD i=kb/1048576;
mb=mb+i;
kb=kb-i*1048576;
}
if(mb>=1024)//1G byte
{
DWORD i=mb/1024;
gb=gb+i;
mb=mb-i*1024;
}
}
}while(bRet);
}
find.Close();
return 1;
}