C++ / C Program to show files and Folders recursively

C++ / C program to show files and folders of any given folder recursively ie this will fetch all the folder inside another folder and then files and show all of them one by one on the screen.

This C++ program is a part of the same compression utility, another programs of the same compression utility is as follows

The source code is

/*	Program to show files and folders of any given folder recursively
	made by		: rakesh kumar
*/
#include<iostream>
#include<fstream>
#include<dirent.h>
#include<error.h>
#include<string.h>
using namespace std;
int isFile(const char* name)
{
    DIR* directory = opendir(name);
    if(directory != NULL) {
     closedir(directory);
     return 0;
    }
    if(errno == ENOTDIR){
     return 1;
    }
    return -1;
}
void show_dir(char source[])
{
DIR *dir;
char name[80];
char d[80];
char ch;
strcpy(name,source);
struct dirent *ent;
if ((dir = opendir (source)) != NULL) {
/* print all the files and directories within directory */
while((ent = readdir (dir)) != NULL) {
strcat(name,ent->d_name);
if(isFile(name)==0) {
cout<d_name<<"\t\t\t\t\t

\n";
if(!((strcmp(ent->d_name,".")==0) || (strcmp(ent->d_name,"..")==0)))
{
show_dir(name);
}
}
else {
printf ("%s\n", ent->d_name);}
strcpy(name,source);
}
closedir (dir);
} else {
/* could not open directory */
cout<<"\n Could not open Directory";
return;
}
return;
}

int main(){
char source[]="C:\\wamp64\\www\\cms";
show_dir(source);
return 0;
}

Here is this program we have supplied a folder that is available on our hard drive. The output of the above program is as follows.
This C++ program to show files and folders was developed in windows 7 using Dev C++ IDE. if you have any better solution to the above problem then please let us know.

C++/ C program to show files and folder



%d bloggers like this: