0

Is there a way to list all folders, sub folders and files in a given path ? for example like: (c:\myfolder), and print full path for each file and folder contained on it ?

c:\myfolder\folder1\
c:\myfolder\folder2\
c:\myfolder\folder2\f1
c:\myfolder\folder2\f2\g1
c:\myfolder\test.txt
c:\myfolder\t.txt

I found this example but designed only for linux:

int is_directory_we_want_to_list(const char *parent, char *name) {
    struct stat st_buf;
    if (!strcmp(".", name) || !strcmp("..", name))
        return 0;

    char *path = alloca(strlen(name) + strlen(parent) + 2);
    sprintf(path, "%s/%s", parent, name);
    stat(path, &st_buf);
    return S_ISDIR(st_buf.st_mode);
}

int list(const char *name) {
    DIR *dir = opendir(name);
    struct dirent *ent;

    while (ent = readdir(dir)) {
        char *entry_name = ent->d_name;
        printf("%s\n", entry_name);

        if (is_directory_we_want_to_list(name, entry_name)) {
            // You can consider using alloca instead.
            char *next = malloc(strlen(name) + strlen(entry_name) + 2);
            sprintf(next, "%s/%s", name, entry_name);
            list(next);
            free(next);
        }
    }

    closedir(dir);
}

Source: How to recursively list directories in C on LINUX

Community
  • 1
  • 1

2 Answers2

0

Use _popen with dir /S /B /OG.
(You can reduce the code)

code sample.
(The display position of the folder may be different from the expected. DIY :-)

#include <Windows.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]){
    if(argc != 2){
        fprintf(stderr, "Usage : %s path\n", argv[0]);
        return -1;
    } else {
        char buff[1024];//or MAX_PATH + α
        FILE *fp;

        sprintf(buff, "dir %s /S /B /OG", argv[1]);
        if(NULL == (fp = _popen(buff, "r"))){
            perror("can't open pipe");
            return -2;
        }
        while(fgets(buff, sizeof buff, fp)){
            char *p = strchr(buff, '\n');
            *p = 0;
            if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(buff)){
                *p = '\\';
                p[1] = '\0';
            }
            puts(buff);
        }
        _pclose(fp);
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

As suggested by @purplepsycho, the simplest way is to use FindFirstFile, FindNextFile and FileClose.

But there are some differences anyway from the Unix version: you must search for a name like folder\* instead of browsing the directory, FindFirstFile gives first name, and you get the other ones with FindNextFile.

Here is a full code example:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#ifdef UNICODE
#define fmt "%S"
#else
#define fmt "%s"
#endif

void process_file(LPCTSTR filename) {
    // TODO: implement actual file processing
    printf(fmt, filename);
    fputs("\n", stdout);
}

void process_folder(LPCTSTR foldername) {
    WIN32_FIND_DATA findFileData;
    HANDLE handle;
    LPTSTR newfolder = malloc(sizeof(TCHAR) *(_tcslen(foldername) + 3));  // add some room for the additional \*
    _tcscpy(newfolder, foldername);
    _tcscat(newfolder, _T("\\*"));
    handle = FindFirstFile(newfolder, &findFileData);
    if (handle != INVALID_HANDLE_VALUE) {
        while(1) {
            // skip . and .. to avoid infinite recursion
            if ((_tccmp(findFileData.cFileName, _T(".")) != 0) && (_tccmp(findFileData.cFileName, _T("..")) != 0)) {
                // compute name as folder\filename
                LPTSTR newname = malloc(sizeof(TCHAR) * (_tcslen(foldername) + _tcslen(findFileData.cFileName) + 2));
                _tcscpy(newname, foldername);
                _tcscat(newname, _T("\\"));
                _tcscat(newname, findFileData.cFileName);
                if ((findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
                    process_folder(newname); // recurse if is is a directory
                }
                else {
                    process_file(newname);  // process other files
                }
                free(newname);  // consistenly free any malloc
            }
            if (FindNextFile(handle, &findFileData) == FALSE) break; // exit the loop when folder is exhausted
        }
        FindClose(handle);
    }
    free(newfolder); 
}

int _tmain(int argc, TCHAR *argv[]) {
    if (argc != 2) {
        fputs("Usage: ", stderr);
        fprintf(stderr, fmt, argv[0]);
        fputs(" top_folder\n", stderr);
        return 1;
    }
    process_folder(argv[1]);
    return 0;
}

Above code still lacks some error condition processing, but is an example of recursive use of Find{First|Next}File on Windows.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • This code wirtten in C++, i want a C example as montionned please :) anyway thank you for your contribution. – Coder Asker Oct 03 '16 at 14:16
  • @CoderAsker: Oups, my bad :-( Hopefully it was easy to convert it to C... – Serge Ballesta Oct 03 '16 at 14:30
  • Many errors detected in your code, and not good converted ... not working solution, sorry :) – Coder Asker Oct 03 '16 at 14:40
  • @CoderAsker: It compiles and runs without errors (only warnings because of the `tcscpy` and `tcscat` functions) under an old VC2008, either in unicode or ansi mode. What errors did you find? – Serge Ballesta Oct 03 '16 at 14:48
  • I get errors like: `error LNK2001: unresolved external symbol __tccmp` – Coder Asker Oct 03 '16 at 14:52
  • Did you include tchar.h? According to this page in [MSDN](https://msdn.microsoft.com/en-us/library/eywx8zcx.aspx), they are macros that should be mapped to the UNICODE or ANSI version depending on _UNICODE being defined or not. – Serge Ballesta Oct 03 '16 at 15:18
  • @CoderAsker: this example heavily depends on the Win32 API and can only run on Windows... – Serge Ballesta Oct 03 '16 at 15:23
  • This example works perfectly, i forgot to include tchar.h ... thank you so much for your time. – Coder Asker Oct 03 '16 at 15:25