Click here to Skip to main content
15,901,505 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

 
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 
GeneralRe: Something less buggy Pin
kalicharan20-Dec-03 3:21
kalicharan20-Dec-03 3:21 
GeneralRe: This code has many problems Pin
Yangghi Min22-Dec-03 15:43
Yangghi Min22-Dec-03 15:43 
GeneralRe: This code has many problems Pin
Yangghi Min22-Dec-03 15:53
Yangghi Min22-Dec-03 15:53 
GeneralRe: This code has many problems Pin
kalicharan22-Dec-03 18:47
kalicharan22-Dec-03 18:47 
GeneralWow, er thanks. Pin
Bill Gates Antimatter Particle17-Dec-03 10:43
Bill Gates Antimatter Particle17-Dec-03 10:43 
I dont normally criticise articles, but you should realise that this is elementary code and of little value to anyone but a complete novice. Im all for encouraging submissions, but I think there should be a peer review of an article before it is accepted onto the main article list. Maybe a pre-acceptance area sort of thing. I suspect that there isnt enough manpower to do this effectively though.

"Life begins at 140"
GeneralRe: Wow, er thanks. Pin
Anonymous17-Dec-03 19:07
Anonymous17-Dec-03 19:07 
GeneralRe: Wow, er thanks. Pin
kalicharan17-Dec-03 20:27
kalicharan17-Dec-03 20:27 
GeneralRe: Wow, er thanks. Pin
Uwe Keim17-Dec-03 22:04
sitebuilderUwe Keim17-Dec-03 22:04 
GeneralI See a Loop ... Pin
Chris Meech17-Dec-03 7:15
Chris Meech17-Dec-03 7:15 
GeneralRe: I See a Loop ... Pin
kalicharan17-Dec-03 19:08
kalicharan17-Dec-03 19:08 
GeneralRe: I See a Loop ... Pin
kalicharan17-Dec-03 19:22
kalicharan17-Dec-03 19:22 

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.