This forum is in archive mode. You will not be able to post new content.

Author Topic: How to get a list of files in a directory  (Read 2042 times)

0 Members and 2 Guests are viewing this topic.

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
How to get a list of files in a directory
« on: July 04, 2012, 04:20:32 AM »
In PHP, the following code:
Code: [Select]
scandir("/path/to/directory");will get a list of all the files in a directory and store it in an array. With that array I can make a loop to read or edit all the files in the directory. How can I do this in C++?
« Last Edit: July 04, 2012, 04:21:08 AM by m0l0ko »

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: How to get a list of files in a directory
« Reply #1 on: July 04, 2012, 10:59:06 AM »
Code: (C) [Select]
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int main()
{
   DIR *dir = opendir (".");
   struct dirent *file;
   if (dir) {
      while ((file = readdir (dir)) != NULL) {
          printf ("%s\n", file->d_name);
      }
      closedir (dir);
   }
   else printf ("Cannot open dir\n");
   return 0;
}
« Last Edit: July 04, 2012, 10:59:43 AM by ca0s »

Offline m0l0ko

  • Peasant
  • *
  • Posts: 129
  • Cookies: -4
    • View Profile
Re: How to get a list of files in a directory
« Reply #2 on: July 04, 2012, 05:11:06 PM »
Thanks. I'm new to C/C++, does this code load the contents of the directory into an array?

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: How to get a list of files in a directory
« Reply #3 on: July 04, 2012, 06:20:40 PM »
Thanks. I'm new to C/C++, does this code load the contents of the directory into an array?
As far as I know, it doesn't. With opendir() you get a  folder "descriptor". Then, with readdir() you get info of the files in that folder, one file for each call to readdir().
Code: [Select]
$ man opendir
$ man readdir

Offline Homi

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: How to get a list of files in a directory
« Reply #4 on: July 22, 2012, 07:56:48 AM »
Code: (c++) [Select]
#include <windows.h>
#include <string>
#include <iostream>
#include <list>
using namespace std;

void GetFileListing(list<string>& listing, string directory, string fileFilter, bool recursively=true)
{
    if (recursively)
        GetFileListing(listing, directory, fileFilter, false);
   
    directory += "\\";
   
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
   
    string filter = directory + (recursively ? "*" : fileFilter);
   
    hFind = FindFirstFile(filter.c_str(), &FindFileData);
   
    if (hFind == INVALID_HANDLE_VALUE)
    {
        DWORD dwError = GetLastError();
        if (dwError!=ERROR_FILE_NOT_FOUND)
        {
            cout << "Invalid file handle for filter "<<filter<<". Error is " << GetLastError() << endl;
        }
    }
    else
    {
        if (!recursively)
        {
            listing.push_back(directory + string(FindFileData.cFileName));
        }
       
        while (FindNextFile(hFind, &FindFileData) != 0)
        {
            if (!recursively)
            {
                listing.push_back(directory + string(FindFileData.cFileName));
            }
            else
            {
                if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)>0 && FindFileData.cFileName[0]!='.')
                {
                    GetFileListing(listing, directory + string(FindFileData.cFileName), fileFilter);
                }
            }
        }
       
        DWORD dwError = GetLastError();
        FindClose(hFind);
        if (dwError != ERROR_NO_MORE_FILES)
        {
            cout << "FindNextFile error. Error is "<< dwError << endl;
        }
    }
}

int main(int argc, char* argv[])
{
    list<string> listing;
    GetFileListing(listing, "G:", "*.*");
    for(list<string>::iterator it = listing.begin(); it!=listing.end();++it)
    {
        cout << *it << endl;
    }
    getchar();
    return 0;
}
Enjoy
« Last Edit: July 22, 2012, 08:15:21 AM by Kulverstukas »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.