Vieil exemple Microsoft =>
LPTSTR CheckSlash (__in LPTSTR lpDir)
{
LPTSTR lpEnd;
lpEnd = lpDir + lstrlen(lpDir);
if (*(lpEnd - 1) != TEXT('\\')) {
*lpEnd = TEXT('\\');
lpEnd++;
*lpEnd = TEXT('\0');
}
return lpEnd;
}
BOOL RecurseDir(LPTSTR lpDir, LPTSTR lpExcludeList)
{
HANDLE hFile = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fd;
LPTSTR lpEnd, lpTemp;
BOOL bResult = TRUE;
BOOL bSkip;
// Setup the ending pointer
lpEnd = CheckSlash (lpDir);
// Append *.* to the source directory
lstrcpy(lpEnd, TEXT("*.*"));
// Search through the source directory
hFile = FindFirstFile(lpDir, &fd);
if (hFile == INVALID_HANDLE_VALUE) {
if ( (GetLastError() == ERROR_FILE_NOT_FOUND) ||
(GetLastError() == ERROR_PATH_NOT_FOUND) ) {
} else {
bResult = FALSE;
}
goto RecurseDir_Exit;
}
do {
// Append the file / directory name to the working buffer
lstrcpy (lpEnd, fd.cFileName);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// Check for "." and ".."
if (!lstrcmpi(fd.cFileName, TEXT("."))) {
continue;
}
if (!lstrcmpi(fd.cFileName, TEXT(".."))) {
continue;
}
// Check if this directory should be excluded
if (lpExcludeList) {
bSkip = FALSE;
lpTemp = lpExcludeList;
while (*lpTemp) {
if (lstrcmpi (lpTemp, lpDir) == 0) {
bSkip = TRUE;
break;
}
lpTemp += lstrlen (lpTemp) + 1;
}
if (bSkip)
continue;
}
// Found a directory.
//
// 1) Change into that subdirectory on the source drive.
// 2) Recurse down that tree.
// 3) Back up one level.
// Recurse the subdirectory
if (!RecurseDir(lpDir, lpExcludeList)) {
bResult = FALSE;
goto RecurseDir_Exit;
}
} else {
// Found a file, add the filesize
g_dwProfileSize += fd.nFileSizeLow;
}
// Find the next entry
} while (FindNextFile(hFile, &fd));
RecurseDir_Exit:
// Remove the file / directory name appended above
*lpEnd = TEXT('\0');
// Close the search handle
if (hFile != INVALID_HANDLE_VALUE) {
FindClose(hFile);
}
return bResult;
}