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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search files in directories 1

Status
Not open for further replies.

nirs

IS-IT--Management
Apr 4, 2003
37
IL
hello

how can i scan directories
and how can i notice the different between a file and a
sub directory

thank you

 
Use FindFirstFile/FindNextFile API, for example:
Code:
#include <windows.h>
#include <iostream>
using namespace std;

void ScanDir(const char* dirname)
{
  HANDLE h;
  WIN32_FIND_DATA info;
  string pat;

  if (!dirname)
     dirname = ".";
  pat = dirname;
  pat += "\\*.*";

  if ((h=FindFirstFile(pat.c_str(),&info)) 
  == INVALID_HANDLE_VALUE)
     return;

  do
  {
    if (info.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
    {
       if (strcmp(info.cFileName,".") 
       && strcmp(info.cFileName,".."))
          cout << "dir\t" << info.cFileName << endl;
       else
         ; // . (this) and .. (parent) refs, 
           // not true subdirnames...
    }
    else // this is a file
      cout << "file\t" << info.cFileName << endl;
  } while (FindNextFile(h,&info));

  if (GetLastError() != ERROR_NO_MORE_FILES)
     cout << "*** Dir scan failed." << endl; 
  FindClose(h);
}
It's not a so hard work to add (recursive) subdirectory scanning in this snippet.
 
ArkM, I seem to recall you've posted very similar code in a couple of other forums.
How about a FAQ entry?


--
 
Salem, it seems my English is too bad for this forum FAQ quality level...
It's rather strange fact (for me;) that question (scan dir) is so popular and so intriguing in our forum...
Let's waiting the next one...
 
ArkM:

If you feel the urge, you could write an FAQ and post it in the forums and I or someone else would be happy to make corrections before you submit it.

Your English is perfectly understandable, and understandably not perfect (whose is?).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top