65.9K
CodeProject is changing. Read more.
Home

Code for finding out the total number of files & folders existing in a folder through VC++

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.03/5 (30 votes)

Dec 17, 2003

1 min read

viewsIcon

152302

The small function block will return a structure consisting of number of folders & files existing in the given folder.

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++

 
    //the structure I am using to describe a folder
    
    typedef struct _MyFolder {
                          int Folders;
                          int Files;
                             }MyFolder;
    
    //the above structure is returned by the followig function 

    //function "Folder" starts **************      
   
    MyFolder* Folder(CString strFolderPathLocal)
    {
    char strFolderName[_MAX_PATH] = {0};
    strcpy(strFolderName,strFolderPathLocal);
    // I want to use each and every character of the CString  thats why I
    // used the character arry
    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; 
    //Manipulate the FolderName for watching all the items in the Folder
    if(strFolderName[strlen(strFolderName)] == '\\')
        strcat(strFolderName,"*.*");
    else if(pdest != NULL && result == 3 && length == 3) 
                   //if any drive is specified as c:\
    {
        strcat(strFolderName,"\*.*");
        Drive = TRUE;
    }
    else
        strcat(strFolderName,"\\*.*");
    
    //defining the folder 
    MyFolder *Folder = new MyFolder;
    Folder->Folders =0;
    Folder->Files =0;
    WIN32_FIND_DATA FindFileData;

    //Attack the Target Folder to get the first file handle.
    //get the first file handle
  
    HANDLE hFile = FindFirstFile(strFolderName,&FindFileData);

    //Now run in a loop of all the files and folders under this folder
    while(hFile)
    {
    //Ignore system directories "." and ".." the good old MS-DOS days

    if((stricmp(FindFileData.cFileName,".")!=0) 
                    && (stricmp (FindFileData.cFileName,"..")!=0)
                    &&(stricmp(FindFileData.cFileName,"") !=0))
    {
        //Check if the current object is a Folder or a File
        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..