recursive directory searching
-
how do you recursively search for a folder with a certain name? If anyone has source code please post it I am pretty sure to use Cfilefind but then again this is why i am asking. -Ryan M.
-
how do you recursively search for a folder with a certain name? If anyone has source code please post it I am pretty sure to use Cfilefind but then again this is why i am asking. -Ryan M.
:rolleyes:
BOOL SearchFolder(CString folder) { CFileFind finder; //build a string with wildcards CString strWildcard(folder); strWildcard += _T("\\*.*");//Whatever you needed //start searching for files BOOL bWorking = finder.FindFile(strWildcard); CString str; while (bWorking){ bWorking = finder.FindNextFile(); //skip . and .. files; if (finder.IsDots()) continue; else if (finder.IsDirectory()){ //You have a directory str = finder.GetFilePath(); //Do anything before //Recursive it again SearchFolder(str); //Do anything after } else{ //You have a file str=finder.GetFilePath(); //Process it yourself DoingFile(str); } } finder.Close(); return TRUE; }
Good luck. -
how do you recursively search for a folder with a certain name? If anyone has source code please post it I am pretty sure to use Cfilefind but then again this is why i am asking. -Ryan M.
Normally, I recommend using MFC classes whenever possible except for CFileFind, which I find to be abhorent. I'd also normally give you some hints and have you learn for yourself, but I don't feel like it, so here is a non-MFC program for you:
#define WIN32_LEAN_AND_MEAN #include < windows.h> #include < shlwapi.h> #include < stdio.h> #include < tchar.h> #pragma comment(lib, "shlwapi.lib") inline bool IsDots(LPCTSTR pFileName) { int i = 0; while (pFileName[i]) { if (pFileName[i++] != '.') return false; } return true; } bool Walk(LPCTSTR pDirToFind, LPTSTR pFinalPath) { bool foundDir = false; WIN32_FIND_DATA findData; HANDLE hFind = ::FindFirstFile(_T("*"), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (::CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, findData.cFileName, -1, pDirToFind, -1) == CSTR_EQUAL) { ::GetCurrentDirectory(MAX_PATH, pFinalPath); PathAddBackslash(pFinalPath); _tcscat(pFinalPath, findData.cFileName); foundDir = true; } else if (!IsDots(findData.cFileName)) { ::SetCurrentDirectory(findData.cFileName); foundDir = Walk(pDirToFind, pFinalPath); ::SetCurrentDirectory(_T("..")); } } } while (!foundDir && ::FindNextFile(hFind, &findData)); ::FindClose(hFind); } return foundDir; } int _tmain(int argc, TCHAR* argv[]) { if (argc != 2) { _putts(_T("FindDir < >")); return 1; } TCHAR finalPath[MAX_PATH]; if (!Walk(argv[1], finalPath)) { _tprintf(_T("Directory \"%s\" could not be found\n"), argv[1]); return 1; } _tprintf(_T("Directory path is: %s\n"), finalPath); return 0; }
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke -
Normally, I recommend using MFC classes whenever possible except for CFileFind, which I find to be abhorent. I'd also normally give you some hints and have you learn for yourself, but I don't feel like it, so here is a non-MFC program for you:
#define WIN32_LEAN_AND_MEAN #include < windows.h> #include < shlwapi.h> #include < stdio.h> #include < tchar.h> #pragma comment(lib, "shlwapi.lib") inline bool IsDots(LPCTSTR pFileName) { int i = 0; while (pFileName[i]) { if (pFileName[i++] != '.') return false; } return true; } bool Walk(LPCTSTR pDirToFind, LPTSTR pFinalPath) { bool foundDir = false; WIN32_FIND_DATA findData; HANDLE hFind = ::FindFirstFile(_T("*"), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (::CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE, findData.cFileName, -1, pDirToFind, -1) == CSTR_EQUAL) { ::GetCurrentDirectory(MAX_PATH, pFinalPath); PathAddBackslash(pFinalPath); _tcscat(pFinalPath, findData.cFileName); foundDir = true; } else if (!IsDots(findData.cFileName)) { ::SetCurrentDirectory(findData.cFileName); foundDir = Walk(pDirToFind, pFinalPath); ::SetCurrentDirectory(_T("..")); } } } while (!foundDir && ::FindNextFile(hFind, &findData)); ::FindClose(hFind); } return foundDir; } int _tmain(int argc, TCHAR* argv[]) { if (argc != 2) { _putts(_T("FindDir < >")); return 1; } TCHAR finalPath[MAX_PATH]; if (!Walk(argv[1], finalPath)) { _tprintf(_T("Directory \"%s\" could not be found\n"), argv[1]); return 1; } _tprintf(_T("Directory path is: %s\n"), finalPath); return 0; }
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'RourkeI dont think its a nice idea to use SetCurrentDirectory().Which can reflect at a different place in your project.
It's not a bug, it's an undocumented feature.
suhredayan@omniquad.commessenger :suhredayan@hotmail.com
-
I dont think its a nice idea to use SetCurrentDirectory().Which can reflect at a different place in your project.
It's not a bug, it's an undocumented feature.
suhredayan@omniquad.commessenger :suhredayan@hotmail.com
A) You should NEVER assume what the directory is in your project. B) Just call
GetCurrentDirectory()
before the call then restore it afterward. You could encapsulate it in a class (which I have.) C) Alternatively, you could use the final directory buffer to build up the path as you go. It requires a little more parsing, but if you preserve the length to make it easier to strip the path back down, it's not difficult. (Yes, I've done it this way, especially in the DOS days.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke