Listing files and folders of a specific directory in C++

This tutorial will teach us how files and folders are listed in a specific directory.  And we are going to complete this process using C++ Programming Language.

C++ Implementation

#include <sys\stat.h>
#include <iostream>
#include <dirent.h>
#include <conio.h>

using namespace std;

int main ()
{
   struct dirent *d;
   struct stat dst;
    
   DIR *dr;
   string path = ".\\";
   dr = opendir(path.c_str());
    
   if (dr != NULL)
   {
      for (d = readdir(dr); d != NULL; d = readdir(dr))
      {
          string type = d->d_name;
          type = path + type;
          if (stat(type.c_str(), &dst) == 0)
          {
          if (dst.st_mode & S_IFDIR)
          {
              type = "is a FOLDER.";
          }
          else if (dst.st_mode & S_IFREG)
          {
              type = "is a FILE.";
          }
         }
         cout<<d->d_name<<endl<<type<<endl;
    }
    closedir(dr);
   }
   else
   {
        cout<<"ERROR"<<endl;
   }
   getch();
}

The output of the above C++ Program is a list of all files and folders in a specific directory. Here we see the list of files and folders in the Internship Task folder.

So, the Output is :

.
is a FOLDER.
..
is a FOLDER.
CodeSpeedy
is a FOLDER.
Codespeedytask1.cpp
is a FILE.
Codespeedytask1.exe
is a FILE.
Internship
is a FOLDER.
New.cpp
is a FILE.
New.exe
is a FILE.

Explanation of the Code

We need to include sys\stat.h, dirent.h and conio.h header files. Because,

  1. sys\stat.h, returns the data by the stat() function.
  2. dirent.h, the format of directory entries. It contains prototypes of opendir(), closedir(), etc.
  3. conio.h, it contains console input/output functions used primarily by MS-DOS compilers.

Then we use namespace std, because it is the declarative part where the identifiers like functions, classes, variables, etc., are declared.

Then the ‘main()’ function starts the execution.

After that, we need to create a pointer of type DIR for handling the directory. The entries in the directory are the structure of dirent type. So, we created the pointer.

OPERATION ON DIRECTORIES
  • Open the Directory, this operation will be done by using the opendir() method. If the directory is absent, opendir() returns a NULL pointer.
  • Then perform the Operation.
  • Close the Directory, will be done using closedir().

Then, the default path is set to the current directory (“.\\”). Then, the program opens to the directory using opendir(). If it is successful, then it iterates through each entry in the directory. For each entry, it constructs the full path and retrieves the file info using stat(). Then the name and type of each entry are printed to the console. Then we use the readdir() method to return a pointer to a direct structure describing the next directory entry in the directory stream associated with dir. After the iterating all the entries, the directory is closed (‘closedir()’).

If the directory is not opened, then an “ERROR” message is displayed.

Internship Task Folder

In the above picture, we see the files and folders in the ‘Internship Task‘ folder.

You can also check the below link,

Listing all files and sub-directories within a directory in C++

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top