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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read all files in a specified directory

Read all files in a specified directory

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 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.
  • H Offline
    H Offline
    hkl
    wrote on last edited by
    #1

    Hi, I am using VC++ 6.0 to write a Win32 console app. Does anyone know how I can read all the file names in a directory if user specifies the directory for input file(s)? Thanks a lot!

    J V A 3 Replies Last reply
    0
    • H hkl

      Hi, I am using VC++ 6.0 to write a Win32 console app. Does anyone know how I can read all the file names in a directory if user specifies the directory for input file(s)? Thanks a lot!

      J Offline
      J Offline
      Joel Holdsworth
      wrote on last edited by
      #2

      try doing...

      _finddata_t fileinfo;
      long hFile;

      _chdir( --dirname-- );

      if( (hFile = _findfirst( "*", &fileinfo )) == -1L )
      --- no files in director ---
      else
      {
      do
      {
      --- called per file ---
      fileinfo.name contains the filname!

      } while( _findnext( hFile, &fileinfo ) == 0 );

      _findclose( hFile );
      }

      **

      *¨¨`) ¸¸.·´ ¸.·*¨¨`) (¸¸.·* ¸ .·* ¸¸.·* (¸¸.~~> Joel Holdsworth.

      **

      H 1 Reply Last reply
      0
      • H hkl

        Hi, I am using VC++ 6.0 to write a Win32 console app. Does anyone know how I can read all the file names in a directory if user specifies the directory for input file(s)? Thanks a lot!

        V Offline
        V Offline
        vcplusplus
        wrote on last edited by
        #3

        CFileFind finder;
        BOOL bWorking = finder.FindFile("*.*");
        while (bWorking)
        {
        bWorking = finder.FindNextFile();
        cout << (LPCTSTR) finder.GetFileName() << endl;
        }

        H 1 Reply Last reply
        0
        • J Joel Holdsworth

          try doing...

          _finddata_t fileinfo;
          long hFile;

          _chdir( --dirname-- );

          if( (hFile = _findfirst( "*", &fileinfo )) == -1L )
          --- no files in director ---
          else
          {
          do
          {
          --- called per file ---
          fileinfo.name contains the filname!

          } while( _findnext( hFile, &fileinfo ) == 0 );

          _findclose( hFile );
          }

          **

          *¨¨`) ¸¸.·´ ¸.·*¨¨`) (¸¸.·* ¸ .·* ¸¸.·* (¸¸.~~> Joel Holdsworth.

          **

          H Offline
          H Offline
          hkl
          wrote on last edited by
          #4

          Wow~! Thanks so much, guys! Joel Holdsworth wrote: _chdir( --dirname-- ); May I know what _chdir is? or what it means? Is it part of the fileinfo structure? Because I got an error msg for this. thanks again.:-D

          D 1 Reply Last reply
          0
          • V vcplusplus

            CFileFind finder;
            BOOL bWorking = finder.FindFile("*.*");
            while (bWorking)
            {
            bWorking = finder.FindNextFile();
            cout << (LPCTSTR) finder.GetFileName() << endl;
            }

            H Offline
            H Offline
            hkl
            wrote on last edited by
            #5

            Thanks. But CFileFind doesn't work in my case. I am only writing a console app, not MFC. (Or may be CFileFind can apply, but I don't know how.) :)

            G 1 Reply Last reply
            0
            • H hkl

              Thanks. But CFileFind doesn't work in my case. I am only writing a console app, not MFC. (Or may be CFileFind can apply, but I don't know how.) :)

              G Offline
              G Offline
              georgiek50
              wrote on last edited by
              #6

              Try this:HANDLE hFile; WIN32_FIND_DATA wfd; hFile = FindFirstFile("*.*", &wfd); while ( FindNextFile(hFile, &wfd) != 0 ) { // your code here // include this is if you don't want the '.' and '..' to show up if ( (strcmp(wfd.cFileName, "..") != 0 ) && (strcmp(wfd.cFileName, ".") != 0 ) // your code here eg. cout << wfd.cFileName << endl; // include something like this if you don't want the directories: if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) // your code here eg. continue; } FindClose(hFile)

              1 Reply Last reply
              0
              • H hkl

                Hi, I am using VC++ 6.0 to write a Win32 console app. Does anyone know how I can read all the file names in a directory if user specifies the directory for input file(s)? Thanks a lot!

                A Offline
                A Offline
                alex barylski
                wrote on last edited by
                #7

                Might wanna look at: http://www.codeproject.com/file/sadirread.asp[^] I'm drinking triples, seeing double and acting single :cool:

                1 Reply Last reply
                0
                • H hkl

                  Wow~! Thanks so much, guys! Joel Holdsworth wrote: _chdir( --dirname-- ); May I know what _chdir is? or what it means? Is it part of the fileinfo structure? Because I got an error msg for this. thanks again.:-D

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

                  hkl wrote: May I know what _chdir is? or what it means? It changes to the specified directory (i.e., CWD).


                  Five birds are sitting on a fence. Three of them decide to fly off. How many are left?

                  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