Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file management

Status
Not open for further replies.

drewdaman

Programmer
Aug 5, 2003
302
CA
hi there..

first time on this forum! was wondering if it is possible to get the names of all the files in a directory from your code. i don't have a problem dealing with a file (ie opneing, reading etc) but i dont' know how to say go to a particular directory and give me a list of all the files in that directory. can anyone help me out? thanks!

 
It seems it's the most popular question about Windows API in this forum...
Use FindFirstFile() et al:
Code:
#include <windows.h>
...
const char dirpat = "C:\\mydir\\*.*"; // or what else...
WIN32_FIND_DATA filedata;
HANDLE hdir = FindFirstFile(dirpat,&filedata);
if (hdir != INVALID_HANDLE_VALUE)
{
  do // scan dir with this pattern (*.* - all files)
  if (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY 
      == 0)
  {  // Use file info
     // filedata.cFileName etc... See MSDN this struct info
  } while (FindNextFile(hdir,&filedata));
  FindClose(hdir);
}
Be careful: you will get subdirs too (with ., .. entries).
There are no such portable equivs in C/C++ libraries...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top