Click here to Skip to main content
15,902,634 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
1.03/5 (33 votes)
22 Dec 20031 min read 151.5K   23   36
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..

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
Questionhow to get file names from folder specified Pin
Anonymous10-Apr-05 23:49
Anonymous10-Apr-05 23:49 
GeneralRe-research Pin
David Crow24-Feb-04 7:04
David Crow24-Feb-04 7:04 
GeneralFile n Folder Pin
Roger6524-Dec-03 7:17
Roger6524-Dec-03 7:17 
I liked your idea and have added file size and a few other bells n whistles. Like the other people that have responded, you did make a few mistakes in your logic and use of the language: One thing, the function Folder and the structure pointer have the same name which confused my compile. I made the stuct pointer global to fix that and changed the name of the function. The file size variable I added it not supported my all OS or compilers but __int64 works for me. If you would like, I'll e-mail you the finished project.

Roger L. McElfresh rlmcelfresh@charter.net
Generalfree store allocation Pin
Anonymous22-Dec-03 22:19
Anonymous22-Dec-03 22:19 
GeneralBetter user CFileFind class Pin
bhushan_at22-Dec-03 17:05
bhushan_at22-Dec-03 17:05 
GeneralRe: Better user CFileFind class Pin
kalicharan22-Dec-03 19:21
kalicharan22-Dec-03 19:21 
GeneralRe: Better user CFileFind class Pin
kalicharan22-Dec-03 21:16
kalicharan22-Dec-03 21:16 
GeneralRe: Better user CFileFind class Pin
bhushan_at23-Dec-03 18:19
bhushan_at23-Dec-03 18:19 
GeneralRe: Better user CFileFind class Pin
kiranpk110-Jan-04 0:42
kiranpk110-Jan-04 0:42 
GeneralRe: Better user CFileFind class Pin
Praveen Chandran2-Apr-07 20:29
professionalPraveen Chandran2-Apr-07 20:29 
GeneralIt's better use CFileFind Class. Pin
joekaren100322-Dec-03 15:31
joekaren100322-Dec-03 15:31 
GeneralDelete this crap Pin
armentage19-Dec-03 8:16
armentage19-Dec-03 8:16 
GeneralRe: Delete this crap Pin
Uwe Keim22-Dec-03 20:25
sitebuilderUwe Keim22-Dec-03 20:25 
GeneralRe: Delete this crap Pin
brian scott29-Dec-03 6:49
brian scott29-Dec-03 6:49 
GeneralRe: Delete this crap Pin
ThatsAlok6-Dec-04 2:38
ThatsAlok6-Dec-04 2:38 
GeneralThis code has many problems Pin
Steve Messer17-Dec-03 12:48
Steve Messer17-Dec-03 12:48 
GeneralRe: This code has many problems Pin
kalicharan17-Dec-03 19:17
kalicharan17-Dec-03 19:17 
GeneralRe: This code has many problems Pin
kalicharan17-Dec-03 19:26
kalicharan17-Dec-03 19:26 
GeneralRe: This code has many problems Pin
Steve Messer17-Dec-03 19:46
Steve Messer17-Dec-03 19:46 
GeneralRe: This code has many problems Pin
kalicharan17-Dec-03 20:15
kalicharan17-Dec-03 20:15 
GeneralRe: This code has many problems Pin
Steve Messer17-Dec-03 21:07
Steve Messer17-Dec-03 21:07 
GeneralRe: This code has many problems Pin
danlobo10-Sep-04 4:56
danlobo10-Sep-04 4:56 
GeneralSome comments Pin
Remon19-Dec-03 10:30
Remon19-Dec-03 10:30 
GeneralSomething less buggy Pin
Remon19-Dec-03 10:36
Remon19-Dec-03 10:36 
GeneralRe: Something less buggy Pin
Anonymous20-Dec-03 3:16
Anonymous20-Dec-03 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.