You need to get a list of files in directories and subdirectories in this form:
A\B\1 A\B\2 A\B\C\1 A\B\C\2 A\B\C\3 A\B\C\D\1 A\B\C\D\2 A\E\1 A\E\2 ... It is clear that you need to use recursion. Here's what I got:
#include <windows.h> #include <stdio.h> #include <stdbool.h> #include <wchar.h> #include <stdlib.h> struct Tree { bool is_folder; wchar_t data[256]; unsigned children_num; struct Tree* parent; struct Tree* children[1024]; }; void create_tree(struct Tree* node); int main() { struct Tree Root = { .is_folder = true, .data = L"C:\\Users\\user\\Music", .parent = NULL, .children_num = 0 }; create_tree(&Root); return 0; } void create_tree(struct Tree* Root) { WIN32_FIND_DATAW find_file_data; HANDLE file_handel; /* Π€Π°ΠΉΠ»Ρ ΠΈ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ ΠΈΡΡΡΡΡ ΠΏΠΎ ΠΌΠ°ΡΠΊΠ΅. Π’.Π΅. ΠΌΡ Π½Π°Ρ
ΠΎΠ΄ΠΈΠΌ Π²ΡΠ΅ ΡΠ°ΠΉΠ»Ρ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠ΅ ΠΏΠΎΠ΄ ΠΌΠ°ΡΠΊΡ "C:\\Users\\user\\Music\\*". */ wchar_t patch[258]; wcscpy(patch, Root->data); wcscat(patch, L"\\*"); file_handel = FindFirstFileW(patch, &find_file_data); if (file_handel != INVALID_HANDLE_VALUE) { do { /* "." - ΡΠ΅ΠΊΡΡΠ°Ρ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΡ, ".." - Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΡ ΡΡΠΎΠ²Π½Π΅ΠΌ Π²ΡΡΠ΅. */ if (!wcscmp(find_file_data.cFileName, L".") || !wcscmp(find_file_data.cFileName, L"..")) { continue; } struct Tree* node = malloc(sizeof(*node)); node->children_num = 0; /* ΠΠ°ΠΏΠΈΡΡΠ²Π°Π΅ΠΌ ΠΈΠΌΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°. */ wcscpy(node->data, find_file_data.cFileName); /* ΠΡ Π½Π°ΡΠ»ΠΈ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΡ. */ if ((find_file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { node->is_folder = true; /* ΠΠΎ-ΠΈΠ΄Π΅Π΅, ΡΠ°ΠΊ ΠΊΠ°ΠΊ Π² ΡΡΠΎΠΉ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΡΠ²ΠΎΠΈ ΡΠ°ΠΉΠ»Ρ ΠΈ ΠΏΠΎΠ΄ΠΏΠ°ΠΏΠΊΠΈ, Π½ΡΠΆΠ½ΠΎ ΠΎΠ±ΡΠ°Π±ΠΎΡΠ°ΡΡ ΠΈ ΠΈΡ
. */ create_tree(node); } else { node->is_folder = false; } wprintf(L"%s\\%s%s", Root->data, node->data, node->is_folder ? L"\\\n" : L"\n"); /* ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π΅Π»Π΅ΠΌΠ΅Π½Ρ Π² Π΄Π΅ΡΠ΅Π²ΠΎ. */ Root->children[Root->children_num] = node; Root->children_num++; } while (FindNextFileW(file_handel, &find_file_data)); FindClose(file_handel); } } Here is the result of the program:
C:\Users\user\Music\1.txt C:\Users\user\Music\25-17\ C:\Users\user\Music\2Pac\ C:\Users\user\Music\5'nizza\ C:\Users\user\Music\50 Cent\ C:\Users\user\Music\ACDC\ C:\Users\user\Music\Avicii\ C:\Users\user\Music\Beatles\ C:\Users\user\Music\CENTER\ C:\Users\user\Music\Chuck Berry\ C:\Users\user\Music\Classic\ C:\Users\user\Music\Covers\ C:\Users\user\Music\Cranberries\ C:\Users\user\Music\Crash Test Dummies\ C:\Users\user\Music\Dash\ C:\Users\user\Music\David Bowie\ C:\Users\user\Music\desktop.ini C:\Users\user\Music\Eminem\ C:\Users\user\Music\Imagine Dragons\ C:\Users\user\Music\John Lenon\ C:\Users\user\Music\Kellee Maize\ C:\Users\user\Music\KISS\ C:\Users\user\Music\Kongos\ C:\Users\user\Music\Lil Peep\ C:\Users\user\Music\Lil Pump\ C:\Users\user\Music\Linkin Park\ C:\Users\user\Music\Metallica\ C:\Users\user\Music\MS MR\ C:\Users\user\Music\Nautilus Pompilus\ C:\Users\user\Music\Nirvana\ C:\Users\user\Music\Noize MC\ C:\Users\user\Music\Old Gods of Asgard\ C:\Users\user\Music\One Rupublic\ C:\Users\user\Music\Other\ C:\Users\user\Music\Oxxxymiron\ C:\Users\user\Music\Pink Floyd\ C:\Users\user\Music\Poets of the Fall\ C:\Users\user\Music\Post Malone\ C:\Users\user\Music\Radiohead\ C:\Users\user\Music\Roy Jones Jr\ C:\Users\user\Music\Soundtracks\ C:\Users\user\Music\Sting\ C:\Users\user\Music\The Animals\ C:\Users\user\Music\The Lonely Biscuits\ C:\Users\user\Music\The Shins\ C:\Users\user\Music\The Wanted\ C:\Users\user\Music\Three Days Grace\ C:\Users\user\Music\Twenty One Pilots\ C:\Users\user\Music\VLNY\ C:\Users\user\Music\Yong Jeezy\ C:\Users\user\Music\Zayde Wolf\ As you can see, directories and files of the first level are displayed only. In this regard, a couple of questions:
- Is the algorithm written correctly?
- If so, why does the program work incorrectly?
- Is there a better way to accomplish this task?
node->dataas I understood it is only the file name, without the path to it. And when you call recursively create_tree, that one at the entrance to Root-> data sees only the file name and of course can not find anything. In node-> data, you must put the full path from the root. - Mike