Menu

List all Files in a Directory - HELP

2005-04-05
2012-09-26
  • Nobody/Anonymous

    I wish to add code to list all files in a directory into my C++ program. I am using Dev-C++. Can anyone help me?

    Thanks, Brian

     
    • qWake

      qWake - 2005-04-05

      Under Windows, use the FindFirstFile and FindNextFile functions. They load a WIN32_FIND_DATA structure that contains details of each file and directory, so you select the files of interest to you based on the content of that structure, for instance directories, hidden files, system files, and so on.

      qWake

       
    • Nobody/Anonymous

      Thank you qWake. I am new to this forum, and you are always very helpful. I found the structure, but I am not sure if this is all of it.
      typedef struct _WIN32_FIND_DATA {
      DWORD dwFileAttributes;
      FILETIME ftCreationTime;
      FILETIME ftLastAccessTime;
      FILETIME ftLastWriteTime;
      DWORD nFileSizeHigh;
      DWORD nFileSizeLow;
      DWORD dwReserved0;
      DWORD dwReserved1;
      TCHAR cFileName[ MAX_PATH ];
      TCHAR cAlternateFileName[ 14 ];

      Is that it? Also, how do I know if it is a sub-folder or hidden file etc? I want to do something different by type. The only thing I see is the attributes field, so I am assuming this is the field to tell me these things. But I can't find the values and what they mean after googling for a while. Do you know?

       
    • qWake

      qWake - 2005-04-05

      Look at the MSDN reference for these two functions and you will see the details for the structure too. Specifically, look here:

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findfirstfile.asp

      and here:

      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/findnextfile.asp

      This second page has sample code to do what you want: to list the files in a directory. Flags for the various types are as you suspected: in the dwFileAttributes double word.

      qWake

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.