Use FindFirstFile and FindNextFile to find all files in a directory (including subdirectory)

zhaozj2021-02-16  58

First, obtain all file names in a certain directory in a program that does not use the MFC, including subdirectories. Put the file name in a buffer with a fixed degree, this buffer is enough to accommodate all file names.

The input of the function is the root directory to be found, and outputs buffering of all file names.

Algorithm: Use recursive second, code:

Void FindfileIndir (Char * Rootdir, Char * Strret)

{

Char fname [mac_filenamelenopath];

ZeromeMory (FName, Mac_FileNamelenopath);

WIN32_FIND_DATA FD;

ZeromeMory (& fd, sizeof (win32_find_data));

Handle hSearch;

Char filepathname [256];

Char TmpPath [256];

ZeromeMory (FilePathname, 256);

ZeromeMory (TmpPath, 256);

STRCPY (FilePathname, Rootdir);

Bool bsearchfinished = false;

FilePathname [Strlen (filepathname) -1]! = '//')

{

STRCAT (filepathname, "//");

}

STRCAT (FilePathname, "*");

HSEARCH = Findfirstfile (Filepathname, & fd);

// is Directory

IF ((fd.dwfileAttributes & file_attribute_directory)

&& strcmp (fd.cfilename, ".") && strcmp (fd.cfilename, ".."))

{

STRCPY (TMPPATH, ROOTDIR);

STRCAT (TMPPATH, FD.CFILENAME);

FindfileIndir (TMPPATH, STRRET);

}

ELSE IF (Strcmp (fd.cfilename, ".") && strcmp (fd.cfilename, "..")))

{

Sprintf (FNAME, "% -50.50s", fd.cfilename;

STRCAT (STRLET STRET [Strlen (Strret)], FNAME;

}

While (! bsearchfinished)

{

IF (FindneyXTfile (HSearch, & fd))

{

IF ((fd.dwfileAttributes & file_attribute_directory)

&& strcmp (fd.cfilename, ".") && strcmp (fd.cfilename, ".."))

{

STRCPY (TMPPATH, ROOTDIR);

STRCAT (TMPPATH, FD.CFILENAME);

FindfileIndir (TMPPATH, STRRET);

}

ELSE IF (Strcmp (fd.cfilename, ".") && strcmp (fd.cfilename, "..")))

{

Sprintf (FNAME, "% -50.50s", fd.cfilename;

STRCAT (STRLET STRET [Strlen (Strret)], FNAME;

}

}

Else

{

IF (getLastError () == error_no_more_files // Normal finished

{

BSearchfinished = True;

}

Else

Bsearchfinished = true; // Terminate Search

}

}

FindClose (HSearch);

}

转载请注明原文地址:https://www.9cbs.com/read-22284.html

New Post(0)