From: Rayne on
On Dec 1, 11:03 pm, "[Jongware]" <so...(a)no.spam.net> wrote:
> Rayne wrote:
> > On Nov 30, 2:43 pm, Friedel Jantzen <nospam_...(a)freenet.de> wrote:
> >> Am Sun, 29 Nov 2009 22:25:23 -0800 (PST) schrieb Rayne:
>
> >>> Hi all,
> >>> I'm trying to convert some code designed to work on Linux to now work
> >>> on Windows. Part of the code uses scandir( ) to store the file names
> >>> in an array. Is there a Windows equivalent of this function?
> >> Hi,
> >> start with FindFirstFile:http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx
> >> then get more matching files in a loop with FindNextFile:http://msdn.microsoft.com/en-us/library/aa364428(VS.85).aspx
> >> Dont forget to release the find handle with FindClose:http://msdn.microsoft.com/en-us/library/aa364413(VS.85).aspx
>
> >> Regards,
> >> Friedel
>
> > Thanks. But I'll have to manually store the file names in a linked
> > list myself, right?
>
> Correct. You also have to clean up the list yourself. Fortunately, this
> should all be rather basic ops.
>
> This one-minute program lists all subfolders and files from a given
> path. If you comment *in* the line
> // processSubdir (temppath, wfd.cFileName, level+1);
> it'll recurse through the entire sub-path.
> Instead of printing the found file name, you can easily store it into an
> array or linked list of choice.
>
> #include <windows.h>
> #include <stdio.h>
>
> void processSubdir (char *fullPath, char *folderName, int level)
> {
> char findpath[_MAX_PATH], temppath[_MAX_PATH];
> HANDLE fh;
> WIN32_FIND_DATA wfd;
> int i;
>
> strcpy (findpath, fullPath);
> if (folderName)
> {
> strcat (findpath, "\\");
> strcat (findpath, folderName);
> }
> strcat (findpath, "\\*.*");
>
> fh = FindFirstFile (findpath, &wfd);
> if (fh != INVALID_HANDLE_VALUE)
> {
> do
> {
> if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
> {
> for (i=0; i<level; i++) printf (" ");
> printf ("+-- %s\n", wfd.cFileName);
> }
> } while (FindNextFile (fh, &wfd));
> FindClose (fh);
> }
>
> fh = FindFirstFile (findpath, &wfd);
> if (fh != INVALID_HANDLE_VALUE)
> {
> do
> {
> if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
> {
> if (wfd.cFileName[0] == '.' && (wfd.cFileName[1] == 0 ||
> wfd.cFileName[1] == '.'))
> continue;
>
> for (i=0; i<level; i++) printf (" ");
> printf ("[Dir] %s\n", wfd.cFileName);
> strcpy (temppath, fullPath);
> if (folderName)
> {
> strcat (temppath, "\\");
> strcat (temppath, folderName);
> }
> // processSubdir (temppath, wfd.cFileName, level+1);
> fflush (stdout);
> }
> } while (FindNextFile (fh, &wfd));
> FindClose (fh);
> }
>
> }
>
> void main (int argc, char **argv)
> {
> if (argc > 1)
> processSubdir (argv[1], NULL, 0);
> else
> processSubdir (".", NULL, 0);
>
> }
>
> [Jongware]

Thanks!