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. Need help to get list of files in a directory...

Need help to get list of files in a directory...

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++
20 Posts 5 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.
  • T Offline
    T Offline
    Thilek
    wrote on last edited by
    #1

    Hi guys, I been working on a antivirus project as my final year project.. i have some problem with getting the list of all the files in a directory.. Lets say i choose C:/ and i want the list of all the files in the directory. Kindly help me with this ya... its a console program and i using DavC++ compiler... Regards, Thilek

    H J 2 Replies Last reply
    0
    • T Thilek

      Hi guys, I been working on a antivirus project as my final year project.. i have some problem with getting the list of all the files in a directory.. Lets say i choose C:/ and i want the list of all the files in the directory. Kindly help me with this ya... its a console program and i using DavC++ compiler... Regards, Thilek

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      Thilek wrote:

      Need help to get list of files in a directory...

      Best answer is FindFirstFile/FindNextFile and other way that its not good using of DlgDirList Function.

      Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

      1 Reply Last reply
      0
      • T Thilek

        Hi guys, I been working on a antivirus project as my final year project.. i have some problem with getting the list of all the files in a directory.. Lets say i choose C:/ and i want the list of all the files in the directory. Kindly help me with this ya... its a console program and i using DavC++ compiler... Regards, Thilek

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

        Since you are not using VC++ compiler, i'm sticking to pure windows apis. You could use FindFirstFile() and FindNextFile(). Check out this sample[^]. Regards, Jijo.

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

        T 1 Reply Last reply
        0
        • J Jijo Raj

          Since you are not using VC++ compiler, i'm sticking to pure windows apis. You could use FindFirstFile() and FindNextFile(). Check out this sample[^]. Regards, Jijo.

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

          T Offline
          T Offline
          Thilek
          wrote on last edited by
          #4

          JiJO raj, thanks ya but its still showing 27 C:\Documents and Settings\Thilek\Desktop\2nd_trial\god\main.cpp cannot convert `const WCHAR*' to `const CHAR*' for argument `1' to `void* FindFirstFileA(const CHAR*, _WIN32_FIND_DATAA*)' i running it on DevC++ since when i run it in VC++ express 2005 many header files missing in its library.... Any suggestions ???

          D 1 Reply Last reply
          0
          • T Thilek

            JiJO raj, thanks ya but its still showing 27 C:\Documents and Settings\Thilek\Desktop\2nd_trial\god\main.cpp cannot convert `const WCHAR*' to `const CHAR*' for argument `1' to `void* FindFirstFileA(const CHAR*, _WIN32_FIND_DATAA*)' i running it on DevC++ since when i run it in VC++ express 2005 many header files missing in its library.... Any suggestions ???

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

            Thilek wrote:

            thanks ya but its still showing 27

            27 files? 27 compiler errors? Please explain.

            Thilek wrote:

            C:\Documents and Settings\Thilek\Desktop\2nd_trial\god\main.cpp cannot convert `const WCHAR*' to `const CHAR*' for argument `1' to `void* FindFirstFileA(const CHAR*, _WIN32_FIND_DATAA*)'

            This is a Unicode issue. You'll need to use wcout.

            "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

            T 1 Reply Last reply
            0
            • D David Crow

              Thilek wrote:

              thanks ya but its still showing 27

              27 files? 27 compiler errors? Please explain.

              Thilek wrote:

              C:\Documents and Settings\Thilek\Desktop\2nd_trial\god\main.cpp cannot convert `const WCHAR*' to `const CHAR*' for argument `1' to `void* FindFirstFileA(const CHAR*, _WIN32_FIND_DATAA*)'

              This is a Unicode issue. You'll need to use wcout.

              "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

              T Offline
              T Offline
              Thilek
              wrote on last edited by
              #6

              NOW I CAN RUN IT USING VC++ PROFF 2005 NO PROBLEM :) .. Below is the code i use : 1.#include <windows.h> 2.#include <stdio.h> 3.#include <iostream> 4.#include <string> 5.#include <sstream> 6. 7. 8. 9.std::string convertWCharArrayToString(const WCHAR * const wcharArray) { 10. std::stringstream ss; 11. 12. int i = 0; 13. char c = (char) wcharArray[i]; 14. while(c != '\0') { 15. ss <<c; 16. i++; 17. c = (char) wcharArray[i]; 18. } 19. 20. std::string convert = ss.str(); 21. return convert; 22.} 23. 24.int main() 25.{ 26. WIN32_FIND_DATA findFileData; 27. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); 28. 29. if(hFind == INVALID_HANDLE_VALUE) { 30. std::cout <<"No files found." <<std::endl; 31. } else { 32. std::cout <<"Files found." <<std::endl; 33. } 34. 35. int fileNumber = 0; 36. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 37. while(FindNextFile(hFind, &findFileData)) { 38. fileNumber++; 39. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 40. } 41. 42. FindClose(hFind); 43. 44. char a; 45. std::cin>> a; 46. return 0; 47.} But when i change HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); to HANDLE hFind = FindFirstFile((LPCWSTR)"C:\*", &findFileData); its showing no file found and some funny symbols... but i wanted it to show entire list of files in a directory if i choose C:\ or D:\ and so on.. Can u guys help me plz... :)

              D V 2 Replies Last reply
              0
              • T Thilek

                NOW I CAN RUN IT USING VC++ PROFF 2005 NO PROBLEM :) .. Below is the code i use : 1.#include <windows.h> 2.#include <stdio.h> 3.#include <iostream> 4.#include <string> 5.#include <sstream> 6. 7. 8. 9.std::string convertWCharArrayToString(const WCHAR * const wcharArray) { 10. std::stringstream ss; 11. 12. int i = 0; 13. char c = (char) wcharArray[i]; 14. while(c != '\0') { 15. ss <<c; 16. i++; 17. c = (char) wcharArray[i]; 18. } 19. 20. std::string convert = ss.str(); 21. return convert; 22.} 23. 24.int main() 25.{ 26. WIN32_FIND_DATA findFileData; 27. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); 28. 29. if(hFind == INVALID_HANDLE_VALUE) { 30. std::cout <<"No files found." <<std::endl; 31. } else { 32. std::cout <<"Files found." <<std::endl; 33. } 34. 35. int fileNumber = 0; 36. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 37. while(FindNextFile(hFind, &findFileData)) { 38. fileNumber++; 39. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 40. } 41. 42. FindClose(hFind); 43. 44. char a; 45. std::cin>> a; 46. return 0; 47.} But when i change HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); to HANDLE hFind = FindFirstFile((LPCWSTR)"C:\*", &findFileData); its showing no file found and some funny symbols... but i wanted it to show entire list of files in a directory if i choose C:\ or D:\ and so on.. Can u guys help me plz... :)

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

                Thilek wrote:

                But when i change HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); to HANDLE hFind = FindFirstFile((LPCWSTR)"C:\*", &findFileData);

                Use two backslashes within string literals. Why are you using convertWCharArrayToString()?

                "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

                T 1 Reply Last reply
                0
                • D David Crow

                  Thilek wrote:

                  But when i change HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); to HANDLE hFind = FindFirstFile((LPCWSTR)"C:\*", &findFileData);

                  Use two backslashes within string literals. Why are you using convertWCharArrayToString()?

                  "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

                  T Offline
                  T Offline
                  Thilek
                  wrote on last edited by
                  #8

                  Still the same i tried using HANDLE hFind = FindFirstFile((LPCWSTR)"C:\\*", &findFileData); i even tried with "C://*" "C://" "C:\\" still showing no files found and some funny symbols... if i ever change it from "*" to anything this was happen.. hmmm... i am blur now... Help me plz... Thanks...

                  D 1 Reply Last reply
                  0
                  • T Thilek

                    Still the same i tried using HANDLE hFind = FindFirstFile((LPCWSTR)"C:\\*", &findFileData); i even tried with "C://*" "C://" "C:\\" still showing no files found and some funny symbols... if i ever change it from "*" to anything this was happen.. hmmm... i am blur now... Help me plz... Thanks...

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

                    Thilek wrote:

                    i even tried with "C://*" "C://" "C:\\"

                    What if you used *.*?

                    Thilek wrote:

                    still showing...some funny symbols...

                    How are you verifying this?

                    "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

                    T 1 Reply Last reply
                    0
                    • D David Crow

                      Thilek wrote:

                      i even tried with "C://*" "C://" "C:\\"

                      What if you used *.*?

                      Thilek wrote:

                      still showing...some funny symbols...

                      How are you verifying this?

                      "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

                      T Offline
                      T Offline
                      Thilek
                      wrote on last edited by
                      #10

                      well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....

                      D 1 Reply Last reply
                      0
                      • T Thilek

                        NOW I CAN RUN IT USING VC++ PROFF 2005 NO PROBLEM :) .. Below is the code i use : 1.#include <windows.h> 2.#include <stdio.h> 3.#include <iostream> 4.#include <string> 5.#include <sstream> 6. 7. 8. 9.std::string convertWCharArrayToString(const WCHAR * const wcharArray) { 10. std::stringstream ss; 11. 12. int i = 0; 13. char c = (char) wcharArray[i]; 14. while(c != '\0') { 15. ss <<c; 16. i++; 17. c = (char) wcharArray[i]; 18. } 19. 20. std::string convert = ss.str(); 21. return convert; 22.} 23. 24.int main() 25.{ 26. WIN32_FIND_DATA findFileData; 27. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); 28. 29. if(hFind == INVALID_HANDLE_VALUE) { 30. std::cout <<"No files found." <<std::endl; 31. } else { 32. std::cout <<"Files found." <<std::endl; 33. } 34. 35. int fileNumber = 0; 36. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 37. while(FindNextFile(hFind, &findFileData)) { 38. fileNumber++; 39. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 40. } 41. 42. FindClose(hFind); 43. 44. char a; 45. std::cin>> a; 46. return 0; 47.} But when i change HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); to HANDLE hFind = FindFirstFile((LPCWSTR)"C:\*", &findFileData); its showing no file found and some funny symbols... but i wanted it to show entire list of files in a directory if i choose C:\ or D:\ and so on.. Can u guys help me plz... :)

                        V Offline
                        V Offline
                        vinvino2001
                        wrote on last edited by
                        #11

                        Directly take the output from findFileData.cFileName. The problem exists in your convertWCharArrayToString().

                        std::cout <<fileNumber <<":" <<findFileData.cFileName <<std::endl;

                        T 1 Reply Last reply
                        0
                        • T Thilek

                          well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....

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

                          Thilek wrote:

                          well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....

                          This makes little, if any, sense. Perhaps you are not understanding relative vs. absolute paths. For example, if you specified "*.*", then ".\\*.*" is implied and the files in the current working directory (CWD) will be listed. If you specified "..\\*.*", then the files in the CWD's parent folder will be listed. If you specified "c:\\*.*", then the files in the root folder will be listed. Consider:

                          void main( void )
                          {
                          WIN32_FIND_DATA findFileData;

                          HANDLE hFind = FindFirstFile(\_T("\*.\*"), &findFileData);
                          if (hFind != INVALID\_HANDLE\_VALUE)
                          {
                              BOOL bFound = true;
                              int fileNumber = 0;
                           
                              while (bFound)
                              {
                                  fileNumber++;
                                  wcout << fileNumber << \_T(":") << findFileData.cFileName << endl;
                          
                                  bFound = FindNextFile(hFind, &findFileData);
                              }
                          
                              FindClose(hFind);
                          } 
                          

                          }

                          "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

                          T 1 Reply Last reply
                          0
                          • V vinvino2001

                            Directly take the output from findFileData.cFileName. The problem exists in your convertWCharArrayToString().

                            std::cout <<fileNumber <<":" <<findFileData.cFileName <<std::endl;

                            T Offline
                            T Offline
                            Thilek
                            wrote on last edited by
                            #13

                            well still the same.. if i put the exe file in C:/ and run it it shows the files and folders in C:/ but it does not going in further into the folders and get the file list.... The program i been working have to get the entire files in the subfolders.. since i am creating a anti-worm system... thanks :)

                            H V 2 Replies Last reply
                            0
                            • D David Crow

                              Thilek wrote:

                              well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....

                              This makes little, if any, sense. Perhaps you are not understanding relative vs. absolute paths. For example, if you specified "*.*", then ".\\*.*" is implied and the files in the current working directory (CWD) will be listed. If you specified "..\\*.*", then the files in the CWD's parent folder will be listed. If you specified "c:\\*.*", then the files in the root folder will be listed. Consider:

                              void main( void )
                              {
                              WIN32_FIND_DATA findFileData;

                              HANDLE hFind = FindFirstFile(\_T("\*.\*"), &findFileData);
                              if (hFind != INVALID\_HANDLE\_VALUE)
                              {
                                  BOOL bFound = true;
                                  int fileNumber = 0;
                               
                                  while (bFound)
                                  {
                                      fileNumber++;
                                      wcout << fileNumber << \_T(":") << findFileData.cFileName << endl;
                              
                                      bFound = FindNextFile(hFind, &findFileData);
                                  }
                              
                                  FindClose(hFind);
                              } 
                              

                              }

                              "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

                              T Offline
                              T Offline
                              Thilek
                              wrote on last edited by
                              #14

                              but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???

                              D 1 Reply Last reply
                              0
                              • T Thilek

                                but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???

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

                                Thilek wrote:

                                but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???

                                Yes. The easiest solution is via a recursive function call. Examples are plentiful on the Web and here at CP.

                                "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

                                T 1 Reply Last reply
                                0
                                • D David Crow

                                  Thilek wrote:

                                  but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???

                                  Yes. The easiest solution is via a recursive function call. Examples are plentiful on the Web and here at CP.

                                  "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

                                  T Offline
                                  T Offline
                                  Thilek
                                  wrote on last edited by
                                  #16

                                  thanks ya.. do u have any links related to that topics :)

                                  D 1 Reply Last reply
                                  0
                                  • T Thilek

                                    thanks ya.. do u have any links related to that topics :)

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

                                    Link 1 Link 2 Link 3

                                    "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

                                    T 1 Reply Last reply
                                    0
                                    • D David Crow

                                      Link 1 Link 2 Link 3

                                      "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

                                      T Offline
                                      T Offline
                                      Thilek
                                      wrote on last edited by
                                      #18

                                      Thanks a lot my friend i try it out and update with u the results.. God bless you... :)

                                      1 Reply Last reply
                                      0
                                      • T Thilek

                                        well still the same.. if i put the exe file in C:/ and run it it shows the files and folders in C:/ but it does not going in further into the folders and get the file list.... The program i been working have to get the entire files in the subfolders.. since i am creating a anti-worm system... thanks :)

                                        H Offline
                                        H Offline
                                        Hamid Taebi
                                        wrote on last edited by
                                        #19

                                        Where do you have problem exactly?FindFirstFile is easy to use?

                                        Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                                        1 Reply Last reply
                                        0
                                        • T Thilek

                                          well still the same.. if i put the exe file in C:/ and run it it shows the files and folders in C:/ but it does not going in further into the folders and get the file list.... The program i been working have to get the entire files in the subfolders.. since i am creating a anti-worm system... thanks :)

                                          V Offline
                                          V Offline
                                          vinvino2001
                                          wrote on last edited by
                                          #20

                                          You have to recursively call and iterate each Folders from the output if findFileData.dwFileAttributes is FILE_ATTRIBUTE_DIRECTORY. Try that.

                                          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