1

I have to believe that this question must have been already answered multiple times, but I cannot find it. Using Visual Studio C (not C++ or C#), how do I get a list of directories inside another directory? I've tried searching "get list of directories" and "get list of folders" and "find folders in a directory".

Can someone enlighten me as to 1) actually get a directory list and 2) what I am doing wrong in my choice of search terms that I am unable to find what I think should be a relatively frequent question.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
PaeneInsula
  • 2,010
  • 3
  • 31
  • 57
  • I think [this](http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx) is about as close as you're going to get. – Robert Harvey Jul 24 '12 at 22:22

2 Answers2

0

EX: compile as Dirc.exe, usage: dirc -d [Directory]

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

int main();
void get_args();
void help();

void get_args(int argc, char** argv)
{
    int i;
        for (i = 1; i < argc; i++) {

            if (argv[i][0] == '-') {

                switch (argv[i][1]) {

        case 'd': for (i = 2; i < argc; i++){
              struct dirent *dp;
                      DIR *dfd = opendir(argv[i]);
                        if(dfd != NULL) {
                      while((dp = readdir(dfd)) != NULL)
                  printf("%s\n", dp->d_name);
                      closedir(dfd);
                        }
              }
                break;

        default:    
        fprintf(stderr,
                "\nERROR: Unknown switch %s\n", argv[i]);
                help();
        }

      } else {
                for (i = 1; i < argc; i++){
                printf("%s ", argv[i]);}
            }
    }
}

int main(int argc, char** argv)
{
        get_args(argc, argv);
        if (argc==1){ help();}
}

void help()
{
    printf("\n Usage: dirc.exe [Arguments] [Dir]\n");
    printf(" Example: dirc -y words\n");
    printf("\n Arguments:");
        printf("\n -d |Directory|");
}
Joe DF
  • 5,438
  • 6
  • 41
  • 63
  • This will list all directory entries, not just directory entries that are directories themselves as OP asked. On *nix you could use stat(2) on each directory entry to determine its type (directory or something else). I'm sure there's something similar on Windows (maybe even stat(2) itself). – Giel Jul 24 '12 at 22:38
  • 1
    This doesn't work in VS2010 C (console app) because there is no equivalent dirent.h file in that environment. – PaeneInsula Jul 24 '12 at 22:44
  • @Giel what about including this file http://linux.die.net/include/sys/stat.h ?, found it on: http://linux.die.net/man/2/stat ? is this the one? – Joe DF Jul 24 '12 at 22:44
  • @JoeDF: if is available with your compiler that has a good chance of working. Probably does with MinGW which has a POSIX compatibility library, but with VSC I honestly wouldn't know (last time I used that was 2005ish). – Giel Jul 24 '12 at 22:52
0

The answer from NTDLS should solve your problem:
Listing directory contents using C and Windows

You have to change the function so the files would be skipped but that shouldn't be the problem. If you don't want to list all the folders of the subdirectorys: leave out the recursive call in the if statement.

Community
  • 1
  • 1
qwertz
  • 14,614
  • 10
  • 34
  • 46