6 Replies - 981 Views - Last Post: 23 September 2009 - 01:27 PM

#1 gabehabe   User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1433
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

List all contents of a given directory

Posted 07 August 2008 - 11:35 AM

Description: This is also compatible with C, just replace #include<cstdio> with #include<stdio.h> and away you go! =)
/*
 * A function to list all contents of a given directory
 * author: Danny Battison
 * contact: [email protected]
 */

#include <dirent.h> // directory header
#include <cstdio>

void listdir (const char *path)
{
    // first off, we need to create a pointer to a directory
    DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL!
    pdir = opendir (path); // "." will refer to the current directory
    struct dirent *pent = NULL;
    if (pdir == NULL) // if pdir wasn't initialised correctly
    { // print an error message and exit the program
        printf ("nERROR! pdir could not be initialised correctly");
        return; // exit the function
    } // end if

    while (pent = readdir (pdir)) // while there is still something in the directory to list
    {
        if (pent == NULL) // if pent has not been initialised correctly
        { // print an error message, and exit the program
            printf ("nERROR! pent could not be initialised correctly");
            return; // exit the function
        }
        // otherwise, it was initialised correctly. let's print it on the console:
        printf ("%sn", pent->d_name);
    }

    // finally, let's close the directory
    closedir (pdir);
}

/** EXAMPLE USAGE **/
int main ()
{
    listdir ("C:\");
    return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: List all contents of a given directory

#2 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: List all contents of a given directory

Posted 11 September 2008 - 08:14 PM

Intersting, was looking for a way to create a list of all my music, this might get me on the right path :)
Was This Post Helpful? 0
  • +
  • -

#3 dor1997   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 04-September 08

Re: List all contents of a given directory

Posted 18 September 2008 - 05:43 AM

Pretty cool! Good work!
Was This Post Helpful? 0
  • +
  • -

#4 Plus   User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: List all contents of a given directory

Posted 04 February 2009 - 08:11 AM

good function for starters with Files,
Was This Post Helpful? 0
  • +
  • -

#5 Rj1231   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 19-August 09

Re: List all contents of a given directory

Posted 19 August 2009 - 01:49 PM

I'll try this code. Really need a snipped how to access any drive on the computer.
Was This Post Helpful? 0
  • +
  • -

#6 mosweg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 23-September 09

Re: List all contents of a given directory

Posted 23 September 2009 - 01:23 PM


Was This Post Helpful? 0
  • +
  • -

#7 mosweg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 23-September 09

Re: List all contents of a given directory

Posted 23 September 2009 - 01:27 PM

I seach a lot og internet pages to find this kind of code, every code wasn't working for me. But this one finaly works! I have tryd to put your input in it. But that wasn't working, I had added: #include #include string drive; cout<<"Drive: "; cin >>drive; listdir (drive":\\"); But than this error was there: error C2143: syntax error : missing ')' before 'string' error C2664: 'listdir' : cannot convert parameter 1 from 'std::string' to 'const char *' error C2059: syntax error : ')' Whit this line: listdir (drive":\\"); Does someone have a slution, becouse all the other codes are not wordking for me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1