Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. recursive directory searching

recursive directory searching

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ryan McDermott
    wrote on last edited by
    #1

    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.

    N J 2 Replies Last reply
    0
    • R Ryan McDermott

      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.

      N Offline
      N Offline
      nguyenvhn
      wrote on last edited by
      #2

      :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.

      1 Reply Last reply
      0
      • R Ryan McDermott

        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.

        J Offline
        J Offline
        Joe Woodbury
        wrote on last edited by
        #3

        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

        2 1 Reply Last reply
        0
        • J Joe Woodbury

          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

          2 Offline
          2 Offline
          224917
          wrote on last edited by
          #4

          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.com

          messenger :suhredayan@hotmail.com

          J 1 Reply Last reply
          0
          • 2 224917

            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.com

            messenger :suhredayan@hotmail.com

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups