Introduction
This is my first article, actually for the project I am currently doing I have a need to find out the number of files & folders existing in a main folder,the same way the windows exploer gives in the status bar, for which I have browsed net, several sites, but I have't got the details, not even some idea & then I have written the following function to get the required result & I have decided to post it in this site so that if any other guy needs the same. & This idea is given by MR.Sai sir, who is presentely working in Infosys, Banglore,India.
Coming to the code, we have to use API provided WIN32_FIND_DATA, The WIN32_FIND_DATA structure describes a file found by the FindFirstFile, FindFirstFileEx, or FindNextFile function.
The FindFirstFile function gives the first file in the directory by searching a directory for a file whose name matches the specified file name. FindFirstFile examines subdirectory names as well as file names.
& all other functions (FindNextFile, etc.,)works similar
You have to pass the folder for which you want to findout the size, then it returns the count for number of folders & files existing in that folder. here is the the function written in vc++
typedef struct _MyFolder {
int Folders;
int Files;
}MyFolder;
MyFolder* Folder(CString strFolderPathLocal)
{
char strFolderName[_MAX_PATH] = {0};
strcpy(strFolderName,strFolderPathLocal);
char *pdest;
int ch = '\\';
int result,length;
int Folders =0;
int Files =0;
pdest = strchr(strFolderName,ch);
result = pdest - strFolderName + 1;
length = strlen(strFolderName);
BOOL Drive = FALSE;
if(strFolderName[strlen(strFolderName)] == '\\')
strcat(strFolderName,"*.*");
else if(pdest != NULL && result == 3 && length == 3)
{
strcat(strFolderName,"\*.*");
Drive = TRUE;
}
else
strcat(strFolderName,"\\*.*");
MyFolder *Folder = new MyFolder;
Folder->Folders =0;
Folder->Files =0;
WIN32_FIND_DATA FindFileData;
HANDLE hFile = FindFirstFile(strFolderName,&FindFileData);
while(hFile)
{
if((stricmp(FindFileData.cFileName,".")!=0)
&& (stricmp (FindFileData.cFileName,"..")!=0)
&&(stricmp(FindFileData.cFileName,"") !=0))
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
Folder->Folders += 1;
MyFolder *Folder2 = new MyFolder;
CString subFolderPath;
subFolderPath.Empty();
subFolderPath += strFolderPathLocal;
subFolderPath += "\\";
subFolderPath += FindFileData.cFileName;
//it is a subfolder so again, recursively call the
// subFolder also & add no of files in that subfolder
Folder2 = Folder(subFolderPath);
Folder->Folders += Folder2->Folders;
Folder->Files += Folder2->Files;
delete Folder2;
}
else if
(!((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)
||(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) )
{
Folder->Files += 1;
}
}
if(!FindNextFile(hFile,&FindFileData))
{
//If nothing is found , make hFile NULL so that
//we get out of the while loop
FindClose(hFile);
hFile = NULL;
}
}
return Folder;
//The memory allocated for Folder is to be cleared extenally.
}
}
& thats all..