ファイル比較(2)
実装
フォルダ
こんなかんじで、指定フォルダ以下のサブフォルダを含む全てのファイル名を取得できる
#pragma warning( disable : 4786 ) // C4786の警告はいらない #include#include #include using namespace std; int SearchDirectory(const char *_path, set *fps); int SearchFile(const char *_path, set *fps); int main(int argc, char *argv[]) { set FilePathSet; char TargetDirectory[MAX_PATH]; strcpy(TargetDirectory, "D:\\c\\依頼\\diff\\"); SearchDirectory(TargetDirectory, &FilePathSet); return 0; } int SearchFile(const char *_path, set *fps) { char szffull[MAX_PATH + 1]; WIN32_FIND_DATA wfd; HANDLE hFind; // フォルダに存在する全てのファイルを登録する sprintf(szffull, "%s*.*", _path); hFind = FindFirstFile(szffull, &wfd); if (!(hFind == INVALID_HANDLE_VALUE)) { do { // ".",".."は登録しない if (wfd.cFileName[0] == '.') continue; // Directoryは登録しない if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) continue; char tmp[MAX_PATH]; strcpy(tmp, _path); strcat(tmp, wfd.cFileName); printf("%s\n", tmp); fps->insert(tmp); } while (FindNextFile(hFind, &wfd)); } FindClose(hFind); return 0; } // // 下層フォルダの検索 // int SearchDirectory(const char *_path, set *fps) { char szffull[MAX_PATH + 1]; WIN32_FIND_DATA wfd; HANDLE hFind; SearchFile(_path, fps); sprintf(szffull, "%s*.", _path); hFind = FindFirstFile(szffull, &wfd); if (hFind == INVALID_HANDLE_VALUE) { // サブフォルダがない return 1; } else { do { // ディレクトリじゃなかったらスキップ if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) continue; // 自身・親フォルダを参照すると永久ループになるので判定 if (strcmp(wfd.cFileName, ".") != 0 && strcmp(wfd.cFileName, "..") != 0){ char path[MAX_PATH + 1]; sprintf(path, "%s%s\\", _path, wfd.cFileName); SearchDirectory(path, fps); } } while (FindNextFile(hFind, &wfd)); } FindClose(hFind); return 0; }
FindFirstFileあたりでググればもっとちゃんとしたサンプルが見つかる気が・・・
続く・・・か?