ファイルを再帰関数を使って列挙する【Windows API】
再帰関数を使ってファイルを列挙する方法を紹介します。
// ファイルの列挙処理を行なう(再帰版)
// ShowFileList("C:\\"); などと指定して使用する
// [引数]
// pszBasePath : 検索を開始するパス
//
// [返値]
// なし
void ShowFileList(char *pszBasePath)
{
char szSearchPath[MAX_PATH+1];
char szSubPath[MAX_PATH+1];
char szFileName[MAX_PATH+1];
WIN32_FIND_DATA fd;
lstrcpy(szSearchPath, pszBasePath);
if (szSearchPath[lstrlen(szSearchPath)-1] != '*') {
lstrcat(szSearchPath, "*");
}
HANDLE hFind = FindFirstFile(szSearchPath, &fd);
do {
if (lstrcmp(fd.cFileName, "..") != 0 &&
lstrcmpi(fd.cFileName, ".") != 0) {
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// ディレクトリの場合
lstrcpy(szSubPath, pszBasePath);
lstrcat(szSubPath, fd.cFileName);
lstrcat(szSubPath, "\\");
printf("[DIR] %s\n", szSubPath);
ShowFileList(szSubPath);
} else {
// ファイルの場合
lstrcpy(szFileName, pszBasePath);
lstrcat(szFileName, fd.cFileName);
printf("[FILE] %s\n", szFileName);
}
}
} while(FindNextFile(hFind, &fd));
FindClose(hFind);
return;
}
