bool findFileInDir(wchar_t* rootDir) { wchar_t fname[BUFFSIZE]; ZeroMemory(fname, BUFFSIZE); WIN32_FIND_DATA fd; ZeroMemory(&fd, sizeof(WIN32_FIND_DATA)); HANDLE hSearch; wchar_t filePathName[BUFFSIZE]; wchar_t tmpPath[BUFFSIZE]; ZeroMemory(filePathName, BUFFSIZE); ZeroMemory(tmpPath, BUFFSIZE); wcscpy(filePathName, rootDir); BOOL bSearchFinished = FALSE; if( filePathName[wcslen(filePathName) -1] != L'\\' ) { wcscat(filePathName, L"\\"); } wcscat(filePathName, L"*"); hSearch = FindFirstFile(filePathName, &fd); //Is directory if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") ) { wcscpy(tmpPath, rootDir); wcscat(tmpPath, fd.cFileName); wcscat(tmpPath, L"\\"); findFileInDir(tmpPath); } else if( wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") ) { wsprintf(fname, L"%ls%ls", rootDir, fd.cFileName); DEBUG_MSG((L"[findFileInDir] filename: %ls", fname)); } while( !bSearchFinished ) { if( FindNextFile(hSearch, &fd) ) { if( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") ) { wcscpy(tmpPath, rootDir); wcscat(tmpPath, fd.cFileName); wcscat(tmpPath, L"\\"); findFileInDir(tmpPath); } else if( wcscmp(fd.cFileName, L".") && wcscmp(fd.cFileName, L"..") ) { wsprintf(fname, L"%ls%ls", rootDir, fd.cFileName); DEBUG_MSG((L"[findFileInDir] filename: %ls", fname)); } } else { if( GetLastError() == ERROR_NO_MORE_FILES ) //Normal Finished { bSearchFinished = TRUE; } else bSearchFinished = TRUE; //Terminate Search } } FindClose(hSearch); return true; }
2012年1月9日 星期一
[C++] Enumerate file in directory.
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言