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. Enumerating all paths on a drive?

Enumerating all paths on a drive?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
5 Posts 3 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.
  • L Offline
    L Offline
    Lord Kixdemp
    wrote on last edited by
    #1

    Hello everyone! I'm trying to enumerate all the paths on my C:\ drive. I want this: C: C:\folder C:\folder\subfolder C:\folder\subfolder\subsubfolder C:\folder\subfolder\subsubfolder\etc ...but instead I get this: C: C:\folder C:\folder\. C:\folder\.\. C:\folder\.\.\. What's the correct way of doing this? I'm using FindFirstFile and FindNextFile, not using MFC. Thanks people!

    Windows Calculator told me I will die at 28. :(

    N M 2 Replies Last reply
    0
    • L Lord Kixdemp

      Hello everyone! I'm trying to enumerate all the paths on my C:\ drive. I want this: C: C:\folder C:\folder\subfolder C:\folder\subfolder\subsubfolder C:\folder\subfolder\subsubfolder\etc ...but instead I get this: C: C:\folder C:\folder\. C:\folder\.\. C:\folder\.\.\. What's the correct way of doing this? I'm using FindFirstFile and FindNextFile, not using MFC. Thanks people!

      Windows Calculator told me I will die at 28. :(

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      See this illustration from MSDN, the part you are missing in colored in red...

      #include
      #include

      using namespace std;

      void Recurse(LPCTSTR pstr)
      {
      CFileFind finder;

      // build a string with wildcards
      CString strWildcard(pstr);
      strWildcard += _T("\\*.*");

      // start working for files
      BOOL bWorking = finder.FindFile(strWildcard);

      while (bWorking)
      {
      bWorking = finder.FindNextFile();

        **`// skip . and .. files; otherwise, we'd       // recur infinitely!`**
      
        `if (finder.IsDots())          continue;`
      
        // if it's a directory, recursively search it
      
        if (finder.IsDirectory())
        {
           CString str = finder.GetFilePath();
           cout << (LPCTSTR) str << endl;
           Recurse(str);
        }
      

      }

      finder.Close();
      }

      void main()
      {
      if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
      cout << "panic!" << endl;
      else
      Recurse(_T("C:"));
      }

      A dot(.) indicates current directory and a dot dot(..) indicates parent directory. So you need to ignore these two entries.


      Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

      L 1 Reply Last reply
      0
      • N Nibu babu thomas

        See this illustration from MSDN, the part you are missing in colored in red...

        #include
        #include

        using namespace std;

        void Recurse(LPCTSTR pstr)
        {
        CFileFind finder;

        // build a string with wildcards
        CString strWildcard(pstr);
        strWildcard += _T("\\*.*");

        // start working for files
        BOOL bWorking = finder.FindFile(strWildcard);

        while (bWorking)
        {
        bWorking = finder.FindNextFile();

          **`// skip . and .. files; otherwise, we'd       // recur infinitely!`**
        
          `if (finder.IsDots())          continue;`
        
          // if it's a directory, recursively search it
        
          if (finder.IsDirectory())
          {
             CString str = finder.GetFilePath();
             cout << (LPCTSTR) str << endl;
             Recurse(str);
          }
        

        }

        finder.Close();
        }

        void main()
        {
        if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
        cout << "panic!" << endl;
        else
        Recurse(_T("C:"));
        }

        A dot(.) indicates current directory and a dot dot(..) indicates parent directory. So you need to ignore these two entries.


        Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

        L Offline
        L Offline
        Lord Kixdemp
        wrote on last edited by
        #3

        Is there any way to do this without MFC and C++ (just plain C & Win32)? Thanks!

        Windows Calculator told me I will die at 28. :(

        N 1 Reply Last reply
        0
        • L Lord Kixdemp

          Is there any way to do this without MFC and C++ (just plain C & Win32)? Thanks!

          Windows Calculator told me I will die at 28. :(

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          Lord Kixdemp wrote:

          Is there any way to do this without MFC and C++ (just plain C & Win32)?

          Yeah, the source code of CFileFind::IsDots is as follows, now maybe you know how to do it...

          BOOL CFileFind::IsDots() const
          {
          ASSERT(m_hContext != NULL);
          ASSERT_VALID(this);

          // return TRUE if the file name is "." or ".." and
          // the file is a directory
          
          BOOL bResult = FALSE;
          if (m\_pFoundInfo != NULL && IsDirectory())
          {
          	LPWIN32\_FIND\_DATA pFindData = (LPWIN32\_FIND\_DATA) m\_pFoundInfo;
          	if (pFindData->cFileName\[0\] == '.')
          	{
          		if (pFindData->cFileName\[1\] == '\\0' ||
          			(pFindData->cFileName\[1\] == '.' &&
          			 pFindData->cFileName\[2\] == '\\0'))
          		{
          			bResult = TRUE;
          		}
          	}
          }
          
          return bResult;
          

          }


          Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com

          1 Reply Last reply
          0
          • L Lord Kixdemp

            Hello everyone! I'm trying to enumerate all the paths on my C:\ drive. I want this: C: C:\folder C:\folder\subfolder C:\folder\subfolder\subsubfolder C:\folder\subfolder\subsubfolder\etc ...but instead I get this: C: C:\folder C:\folder\. C:\folder\.\. C:\folder\.\.\. What's the correct way of doing this? I'm using FindFirstFile and FindNextFile, not using MFC. Thanks people!

            Windows Calculator told me I will die at 28. :(

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            When you're enumerating, skip any dirs named "." and ".."

            --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

            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