Author Topic: Turbo C Program to enumerate all sub directories in a directory  (Read 1419 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Turbo C Program to enumerate all sub directories in a directory
« on: February 08, 2012, 02:02:39 pm »
Here is the simple Turbo C Program to enumerate all sub directories in a directory (folder).

Code:
//Source Code Listing


#include <dirent.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

int Display_All_Directories(char *searchstring)
{
   int count = 0;
   DIR *directory_list;
   struct dirent *dentry;
   char buf[128];
   directory_list = opendir (searchstring);
   if (directory_list != NULL) {
      while ((dentry = readdir (directory_list)) != NULL) {
	 strcpy(buf,searchstring);
	 strcat(buf, dentry->d_name);
	 if(opendir(buf) != NULL)
	 {
		 printf ("Dir: %s\n", dentry->d_name);
		 count++;
	 }
      }
      closedir (directory_list);
   } else {
      printf("could not open directory");
   }
   return count;
}

void main()
{
   char searchstring[128] = "c:\\";
   int count = 0;

   count = Display_All_Directories(searchstring);
   printf("\n%d directories found\n\n", count);
   getch();
}

/***
The output would the list of all sub directories under C:\ directory. 
At the end it will display how many sub directories are there in the current directory.
***/


 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1