Author Topic: Turbo C - Enumerating the list of directories in a folder  (Read 961 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Turbo C - Enumerating the list of directories in a folder
« on: December 01, 2011, 07:13:16 pm »
Here is the turbo c code to enumerate the list of directories in a folder.

It accepts the input from the user. You can execute this program by

DirList.exe c:\


Code:
//Source Code Listing


//DirList.C File

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

int main (int argc, char *argv[])
{
   int count = 0;
   DIR *directoryPtr;
   struct dirent *dirEntPtr;     

   if(argc < 2)
   {
      printf("Usage: DirList.exe <folder>")
      return 1;
   }
   directoryPtr = opendir (argv[1]);

   if (directoryPtr == NULL)
   {
      printf("Error in opening the directory %s", argv[1]);
      return 1;
   }
   printf("List of directories from : %s\n", argv[1]);
   while (dirEntPtr = readdir (directoryPtr))
   {
      printf("%s\n", dirEntPtr->d_name);
      count++;
   }

   printf("Total directories found: %d", count);
   closedir (directoryPtr);

   return 0;
}

 


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