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. How to loop through a directory

How to loop through a directory

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorial
9 Posts 6 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.
  • S Offline
    S Offline
    santhoshv84
    wrote on last edited by
    #1

    Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...

    Thanks and Regards. SANTHOSH V

    _ S J D 4 Replies Last reply
    0
    • S santhoshv84

      Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...

      Thanks and Regards. SANTHOSH V

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      FindFirstFile/FindNextFile

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • S santhoshv84

        Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...

        Thanks and Regards. SANTHOSH V

        S Offline
        S Offline
        Sarath C
        wrote on last edited by
        #3

        See the sample from MSDN. Hope you can use MFC

        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();
             TRACE(\_T("%s\\n"), (LPCTSTR)str);
             Recurse(str);
          }
        

        }

        finder.Close();
        }

        void PrintDirs()
        {
        Recurse(_T("C:\\JPG"));
        }

        -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

        My blog - Sharing My Thoughts, An Article - Understanding Statepattern

        S 1 Reply Last reply
        0
        • S santhoshv84

          Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...

          Thanks and Regards. SANTHOSH V

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #4

          Use the CFileFind class. 1) Use CFileFind::FindFile() and CFileFind::FindNextFile() to iterate through the directories and files. 2) Use CFileFind::IsDirectory() function to determine whether its file/folder and just add it to your array. 3) You can get the file name by using CFileFind::GetFileName() and file path by calling CFileFind::GetFilePath(). if you need the windows api itself, have a look at FindFirstFile() and FindNextFile(). Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          S 1 Reply Last reply
          0
          • S Sarath C

            See the sample from MSDN. Hope you can use MFC

            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();
                 TRACE(\_T("%s\\n"), (LPCTSTR)str);
                 Recurse(str);
              }
            

            }

            finder.Close();
            }

            void PrintDirs()
            {
            Recurse(_T("C:\\JPG"));
            }

            -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

            My blog - Sharing My Thoughts, An Article - Understanding Statepattern

            S Offline
            S Offline
            Sarath C
            wrote on last edited by
            #5

            alternate methods are to use CRT _findfirst/_findnext[^] or Win32 FindFirstFile/FindNextFile[^] Functions

            -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

            My blog - Sharing My Thoughts, An Article - Understanding Statepattern

            1 Reply Last reply
            0
            • J Jijo Raj

              Use the CFileFind class. 1) Use CFileFind::FindFile() and CFileFind::FindNextFile() to iterate through the directories and files. 2) Use CFileFind::IsDirectory() function to determine whether its file/folder and just add it to your array. 3) You can get the file name by using CFileFind::GetFileName() and file path by calling CFileFind::GetFilePath(). if you need the windows api itself, have a look at FindFirstFile() and FindNextFile(). Regards, Jijo.

              _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

              S Offline
              S Offline
              SandipG
              wrote on last edited by
              #6

              Little late.. Some body didn't like it :(

              Regards, Sandip.

              modified on Wednesday, August 6, 2008 6:59 AM

              J 2 Replies Last reply
              0
              • S SandipG

                Little late.. Some body didn't like it :(

                Regards, Sandip.

                modified on Wednesday, August 6, 2008 6:59 AM

                J Offline
                J Offline
                Jijo Raj
                wrote on last edited by
                #7

                My keyboard is a bit slow. :laugh: Regards, Jijo.

                _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                1 Reply Last reply
                0
                • S SandipG

                  Little late.. Some body didn't like it :(

                  Regards, Sandip.

                  modified on Wednesday, August 6, 2008 6:59 AM

                  J Offline
                  J Offline
                  Jijo Raj
                  wrote on last edited by
                  #8

                  Don't worry pal! I've voted a 5. ;) Regards, Jijo.

                  _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                  1 Reply Last reply
                  0
                  • S santhoshv84

                    Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...

                    Thanks and Regards. SANTHOSH V

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    santhoshv84 wrote:

                    I need to get all the files from a directory in an array. But i cant use CFileDialog..

                    Even if you could, why would you want/need to? :confused:

                    "Love people and use things, not love things and use people." - Unknown

                    "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                    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