Thread: Counting Files

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Question Counting Files

    I need to write a C program for a friend that counts the number of dat files in a certain directory, however, I don't know how to count files in C. Can anyone help me out?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Windows SDK File System: Q: How to count files within a directory and subdirectories?

    Too convenient! (It's the concept that counts, not that it's C++ or not) Bad mistake of assuming Windows...I keep editing my post...but you should have posted compiler/OS in the first place.
    Last edited by Tonto; 09-29-2005 at 07:24 PM.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    http://www.eskimo.com/~scs/C-faq/q19.20.html

    There's no truly portable way. The logic is simple, read all the files in the directory, check if they end in .dat, increment a counter.

    For reading files in a directory, there's opendir/readdir in POSIX environments.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    ls *.dat | wc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869

    Talking

    Salem,

    That's not a C program

    I think you meant
    Code:
    #include <stdlib.h>
    int main(void)
    {
        return system("ls *.dat | wc");
    }

  6. #6
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    fstream ft;
    ft.open("store.txt",ios::out);
    WIN32_FIND_DATA FindFileData; 
    HANDLE hFind = INVALID_HANDLE_VALUE; 
    char DirSpec[MAX_PATH]; // directory specification 
    
    
    cout<<"Please enter your path"<<endl;
    cout<<"\nEg. C:\\Program Files\\Common Files\\System  "<<endl;
    cout<<">>";
    
    cin.get(DirSpec, MAX_PATH);
    cout<<"\n";
    strncat(DirSpec, "\\*", 3);
    hFind = FindFirstFile(DirSpec, &FindFileData);
    int cond=0;
    	if(hFind == INVALID_HANDLE_VALUE)
    	{
    	cout<<"Error: invalid path\n";
         cond=1;
    	}
    
    if (cond!=1)
    {
    
    
    	while(FindNextFile(hFind, &FindFileData) != 0)
    	{
        char array[81];
        
    	ft<<FindFileData.cFileName<<"\n";
    	cout<<FindFileData.cFileName<<"\n";
    
    
    	
    	}
    
    FindClose(hFind);
    }
    ft.close();
    /*------------------------------------------------------*/
    cout<<"\n\nNow enter the extension of the files you wish to count"<<endl;
    cout<<"Example (mp3 or dat...)"<<endl;
    cout<<">>";
    
    char extension[81];
    cin>>extension;
    
    int s_e = strlen(extension);
    
    fstream file_pointer;
    file_pointer.open("store.txt",ios::in);
    int file_count=-1; //to account for eof anomoly
    do
    {
        char stuff[100];
        file_pointer>>stuff;
        
        
        int counter=0;
        int check=0;
        int s = strlen(stuff);
        for (int i=0; i<s; i++)
        {
            if(stuff[i]==extension[0])
            {
                for (int b=0; b<s_e; b++)
                {
                    if(stuff[i+b]==extension[counter])
                    {
                        check++;
                        counter++;
                    }    
                } 
                if(check==s_e)
                {
                    file_count++;
                }
                counter=0;
                check=0; 
            }          
        }
        
        
    }while(file_pointer.peek()!=EOF);
    file_pointer.close();               
    
    cout<<"\nThere were "<<file_count<<" files found";    
      
    cin.get();
    cin.get();
    
    
    }

    My sample input...
    Code:
    Please enter your path
    
    Eg. C:\Program Files\Common Files\System
    >>C:\program files\common files\system
    
    ..
    ado
    directdb.dll
    msadc
    Ole DB
    wab32.dll
    wab32res.dll
    
    
    Now enter the extension of the files you wish to count
    Example (mp3 or dat...)
    >>.dll
    
    There were 3 files found
    This code was written using a snippet from the FAQ. It's probably a lot more cumbersome than it should be but it appears to work. Basically i'm doing what cwr suggested.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    treenef: your program will miss the first file because you toss its filename into the bit bucket. FindFirstFile() returns the first filename in the WIN32_FIND_DATA and you need to add it to the list before calling FindNextFile() the first time. use a do-while loop makes this easier to code.
    Code:
     do {
        ft<<FindFileData.cFileName<<"\n";
        cout<<FindFileData.cFileName<<"\n";
     } while( FindNextFile(hFind, &FindFileData) != 0);

  8. #8
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    anicent: You're right. I didn't know of this since I just cut and pasted the code from a snippet i found in the FAQ.

    Also I've noticed there is a problem with EOF as mentioned in the FAQ which causing other file counting problems. Hopefully, here is a corrected version...

    Code:
    #include <iostream>
    #include <windows.h>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      fstream ft;
      ft.open("store.txt",ios::out);
      WIN32_FIND_DATA FindFileData; 
      HANDLE hFind = INVALID_HANDLE_VALUE; 
      char DirSpec[MAX_PATH]; // directory specification 
    
    
      cout<<"Please enter your path"<<endl;
      cout<<"\nEg. C:\\Program Files\\Common Files\\System  "<<endl;
      cout<<">>";
    
      cin.get(DirSpec, MAX_PATH);
      cout<<"\n";
      strncat(DirSpec, "\\*", 3);
      hFind = FindFirstFile(DirSpec, &FindFileData);
      
      int cond=0;
    	
        if(hFind == INVALID_HANDLE_VALUE)
    	{
    	   cout<<"Error: invalid path\n";
           cond=1;
    	}
    
        if (cond!=1)
        {
    
    
    	 do {
        ft<<FindFileData.cFileName<<"\n";
        cout<<FindFileData.cFileName<<"\n";
     } while( FindNextFile(hFind, &FindFileData) != 0);
    
          FindClose(hFind);
        }
        ft.close();
        
    /*------------------------------------------------------*/
    cout<<"\n\nNow enter the extension of the files you wish to count"<<endl;
    cout<<"Example (mp3 or dat...)"<<endl;
    cout<<">>";
    
    char extension[81];
    cin>>extension;
    
    int s_e = strlen(extension);
    
    char stuff[81];
    
    int file_count=0; 
    ifstream file_pointer("store.txt");
    while(file_pointer>>stuff)  
    {
    
        
        int counter=0;
        int check=0;
        int s = strlen(stuff);
        for (int i=0; i<s; i++)
        {
            if(stuff[i]==extension[0])
            {
                for (int b=0; b<s_e; b++)
                {
                    if(stuff[i+b]==extension[counter])
                    {
                        check++;
                        counter++;
                    }    
                } 
                if(check==s_e)
                {
                    file_count++;
                }
                counter=0;
                check=0; 
            }          
        }
        
        
    }
    file_pointer.close();               
    
    cout<<"\nThere were "<<file_count<<" files found";    
      
    cin.get();
    cin.get();
    
    
    }
    Last edited by treenef; 09-30-2005 at 03:29 PM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    You could actually do the program without using the file. Get the extension from the keyboard first then search the directory for it. That eliminates the need for the file.

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    Smile Thanks

    I appreciate all the help. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM