...how to get the number of files in a folder

This is a discussion on ...how to get the number of files in a folder within the Windows Programming forums, part of the Platform Specific Boards category; So...how can i get the number of files in a folder? Is it possible to get the names too?...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    12

    ...how to get the number of files in a folder

    So...how can i get the number of files in a folder? Is it possible to get the names too?

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    Try this, I converted this from Randy Birch's VBnet code, just for you

    It's just a little function that takes the source dir (and "*.*" for all files) and returns the number of files present in it. I can't guarantee it'll work (I'm at school atm, so I can't test it), but I'm sure someone will correct me...
    Code:
    int rgbCountFilesAll(char *sPath)
    {
       WIN32_FIND_DATA wfd;
       long hFile;
       BOOL bNext = TRUE;
       long fCount = 0;
          
       hFile = FindFirstFile(sPath, &wfd);
       
       if (hFile == INVALID_HANDLE_VALUE)
          //no match, so bail out  
           return 0;
       end if
           
      //must have at least one, so ...  
       if (hFile)
          while (bNext != FALSE)
          {
             fCount++;
             bNext = FindNextFile(hFile, &wfd);
          }
       end if
          
      //Close the search handle  
       FindClose(hFile);
          
      //return the number of files found  
       return fCount;
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    I'll try this, thank you very much for your help!!! (i would say much but i'm french and i haven't the vocable )

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    the following code works .... it is part of one of my programs...
    it loads a list box with with the file names in the give path


    Code:
    //** Loads a list box with the files in sPath
    //** filter files with the extension sFilter i.e. "mdb" "txt"
    //** returns the number of files loaded... -1 for invalid dir
    int CMyDialog::LoadFileList(CString sPath, CString sFilter, CListBox *pListBox)
    {
    	CFileFind ffind;
    	BOOL bWorking;
    	CString sFileName;
    	int iFileCount = 0;
    
    	//** Set up for wild characters
    	if( sFilter.Find(".*") >= 0)
    		sFilter.TrimRight("*");
    
    	if( sFilter.Find("*.") == 0)
    		sFilter.TrimLeft("*");
    
    	sPath.TrimRight("\\");
    	sPath += _T("\\*.*");
    
    	//** Verify that it is a valid path
    	bWorking = ffind.FindFile(sPath);
    
    	if( bWorking == 0)
    		return -1;
    
    	//** Verify that the list box is not NULL
    	if( pListBox == NULL )
    		return -1;
    
    	//** Reset the content of the list box
    	pListBox->ResetContent();
    
    	while( bWorking != 0)
    	{
    	        bWorking = ffind.FindNextFile();
    
    	        if( bWorking != 0)
    	       {
    		sFileName = ffind.GetFileName();
                                   //** If the file name holds the filter as a substring
                                   //** but do not load the files "." and ".."
                                  if( sFileName.Find( sFilter ) >= 0 && sFileName.Find(".") > 0)
    		{
    		         if( pListBox->FindStringExact(-1, sFileName) < 0 )
    			pListBox->AddString( sFileName );
    				iFileCount++;
    			}
    		}
    	}
    	
    	return iFileCount;
    }
    zMan

  6. #6
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Smile I thank you too!

    I was also wondering this... I didnt know it would equal false!

    Thanks, SPH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-09-2008, 02:34 AM
  2. Number of files in a folder
    By gadu in forum C Programming
    Replies: 1
    Last Post: 10-09-2008, 07:16 PM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. Code to display files and sizes of any folder
    By CrackDown in forum Linux Programming
    Replies: 0
    Last Post: 04-22-2003, 12:23 PM
  5. writing files to a specified folder
    By jverkoey in forum Windows Programming
    Replies: 5
    Last Post: 03-29-2003, 11:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21