Finding out how many files are in a directory

This is a discussion on Finding out how many files are in a directory within the C++ Programming forums, part of the General Programming Boards category; Hi people, I'm very new to C++ (like 2 days) and i can't seem to find out how to check ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    5

    Finding out how many files are in a directory

    Hi people,

    I'm very new to C++ (like 2 days) and i can't seem to find out how to check how many files exist in a directory.

    Say i have 20 text files in c:\temp i would like to be able to write a program that lets me know that i have 20 text files in that folder. I searched this forum but i can't find any examples. Please can anyone help.

    Cheers Mike...

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    /* edited for the purpose of this post */
    
    #include <stdio.h>
    #include <io.h>
    #include <time.h>
    
    int main( void )
    {
        struct _finddata_t c_file;
        long hFile;
    
        /* Find first file in current directory */
        if( (hFile = _findfirst( "*.*", &c_file )) == -1L )
           printf( "No *.* files in current directory!\n" );
       else
       {
                printf( "Listing of files\n\n" );
                printf( "\nRDO HID SYS ARC  FILE         DATE %25c SIZE\n", ' ' );
                printf( "--- --- --- ---  ----         ---- %25c ----\n", ' ' );
                printf( ( c_file.attrib & _A_RDONLY ) ? " Y  " : " N  " );
                printf( ( c_file.attrib & _A_SYSTEM ) ? " Y  " : " N  " );
                printf( ( c_file.attrib & _A_HIDDEN ) ? " Y  " : " N  " );
                printf( ( c_file.attrib & _A_ARCH )   ? " Y  " : " N  " );
                printf( " %-12s %.24s  %9ld\n",
                   c_file.name, ctime( &( c_file.time_write ) ), c_file.size );
    
                /* Find the rest of the files */
                while( _findnext( hFile, &c_file ) == 0 )
                {
                    printf( ( c_file.attrib & _A_RDONLY ) ? " Y  " : " N  " );
                    printf( ( c_file.attrib & _A_SYSTEM ) ? " Y  " : " N  " );
                    printf( ( c_file.attrib & _A_HIDDEN ) ? " Y  " : " N  " );
                    printf( ( c_file.attrib & _A_ARCH )   ? " Y  " : " N  " );
                    printf( " %-12s %.24s  %9ld\n",
                       c_file.name, ctime( &( c_file.time_write ) ), c_file.size );
                }
    
           _findclose( hFile );
       }
       return 0;
    }
    first off, im lazy so im not gonig to turn this into C++, ill leave it in C, so you can learn something.
    Second if you are new to C++, you really should AVOID this. This uses topics you are most likely unfamiliar with, and will have enormous problems trying to implement this, but have fun anyway.

    -LC
    Last edited by Lynux-Penguin; 08-27-2003 at 05:23 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    one way to do it is to use a system call to help you write the directory of a drive out to a file

    system("dir c: > file.txt");

    then you would read in the file

    std::ifstream;

    then you would go through the file, using some token as something to trip a counter...

    of course you'd have to read more about this, and at two days your going to need somebody to write it for you if you need it fast... but look up:

    getline() (iostream)
    system calls (cstdlib)
    ifstream (fstream)
    strings (string)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read here ( C examples, but the concept is the same for C++) : http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    But as already implied, if you're new to C++, try to learn the language before learning OS specific calls.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    Thanks for all your replies guy i'll check them out when i've finished work...

    When i say i'm new to C++ i am but i'm not new to programming as i've done a fair bit with perl, VB, php so i think i've got the basics of language down well almost

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to extract a list of files in a specific directory?
    By Perspektyva in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2008, 02:02 PM
  2. Finding files question
    By geek@02 in forum Windows Programming
    Replies: 12
    Last Post: 09-02-2008, 04:31 AM
  3. Copying all files in a directory
    By rak1986 in forum C Programming
    Replies: 2
    Last Post: 08-25-2008, 02:02 AM
  4. deleting all files in a directory using c..
    By ShadeS_07 in forum C Programming
    Replies: 6
    Last Post: 07-30-2008, 09:21 AM
  5. R/W on files in a directory by i-number
    By Lateralus in forum Linux Programming
    Replies: 1
    Last Post: 07-26-2005, 11:58 AM

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