c - print all files and subdirectories in a give path -
c - print all files and subdirectories in a give path -
so, im writing programme recursively print directories/sub-directories , files in given path. im able go in first sub-directory , print files in it. problem right need find way step 1 directory level , go on left off reading. until status occurs @ original directory level.
#include "everything.h" #include "strsafe.h" win32_find_data ffd; handle hfind = invalid_handle_value; large_integer filesize; dword dwerror; void showdir(tchar *szdir); int _tmain(int argc, lpctstr argv[]) { tchar szdir[max_path]; size_t lengthofarg; // verify number of parameters if (argc != 2) { reporterror(_t("error: wrong number of arguments"), 1, false); } // length of entered directory stringcchlength(argv[1], max_path, &lengthofarg); // verify directory path not long if (lengthofarg > max_path - 2) { reporterror(_t("error: directory long"), 2, false); } // attach asterisk (wildcard search char) end of directory path stringcchcopy(szdir, max_path, argv[1]); stringcchcat(szdir, max_path, _t("*")); showdir(szdir); } void showdir(tchar *szdir) { // begin search; find first file in directory hfind = findfirstfile(szdir, &ffd); if (hfind == invalid_handle_value) { reporterror(_t("error in searching"), 3, true); } //hfind = findfirstfile(szdir, &ffd); while (findnextfile(hfind, &ffd) != 0) { if ((ffd.dwfileattributes & file_attribute_directory) == 0) { filesize.lowpart = ffd.nfilesizelow; filesize.highpart = ffd.nfilesizehigh; _tprintf(_t("%s %ld\n"), ffd.cfilename, filesize.quadpart); } // did find directory? // ffd.dwfileattributes says directory (file_attribute_directory) if ((ffd.dwfileattributes & file_attribute_directory) && (_tcscmp(ffd.cfilename, _t(".")) != 0 && (_tcscmp(ffd.cfilename, _t("..")) != 0))) { tchar fullpath[max_path]; stringcchcopy(fullpath, strlen(szdir) - 0, szdir); stringcchcat(fullpath, max_path, ffd.cfilename); stringcchcat(fullpath, max_path, "\\"); _tprintf(_t("<dir> %s \n"), fullpath); stringcchcat(fullpath, max_path, _t("*")); showdir(fullpath); } // go on search; seek find more files } // figure out if encountered error other "no more files" dwerror = getlasterror(); if (dwerror != error_no_more_files) { reporterror(_t("error in searching"), 4, true); } findclose(hfind); }
your global variables
win32_find_data ffd; handle hfind = invalid_handle_value; large_integer filesize; dword dwerror;
should local variables of showdir()
. each recursion level has own search handle, , when nested showdir()
returns, calling showdir()
can go on enumerating directory.
note code ignores first file in each directory (the result of findfirstfile()
). rewrite (error checking omitted brevity):
hfind = findfirstfile(szdir, &ffd); { // ... handle ffd ... } while (findnextfile(hfind, &ffd))
c windows recursion
Comments
Post a Comment