Solved

FindFirstFile and FindNextFile usage

Posted on 2000-03-21
6
581 Views
Last Modified: 2012-05-04
I'm using MS Vis C++ 6.0 on an NT machine.  I need some help in using FindFirstFile and FindNextFile.  If someone could give me 2 simple codes, it would be great.  1) I need a code that would output the list of files (and directories if possible) to screen.  2) I need a code that would output the list of files (and directories if possible) to an array.

Thanks in advance!
0
Comment
Question by:junkyboy
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2641782
To print the names.

   BOOL  Done;                     // Done searching for files?
   HANDLE  FndHnd = NULL;   // Handle to find data.
   WIN32_FIND_DATA FindDat;  // Info on file found.

   FndHnd = FindFirstFile("C:\\*.txt", &FindDat);
   Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

   while (!Done)
   {
      cout <<  FindDat.cFileName << endl; // Print name of file found.
      Done = !FindNextFile(FndHnd, &FindDat);
   }
   if (FndHnd)            // If there was anything found, then
      FindClose(FndHnd); // Close the find handle
0
 
LVL 22

Expert Comment

by:nietod
ID: 2641796
The array is a little more difficult, how do you know haw big to make it?  You can count the files and then allocate the array dynamcially, but a beter idea would be to use an array class, like vector<>.  At that point it is probably a good idea to use a string class to store the name too, so I'll use a vector of strings, like

  vector<string> FileList;

  BOOL  Done;                     // Done searching for files?
   HANDLE  FndHnd = NULL;   // Handle to find data.
   WIN32_FIND_DATA FindDat;  // Info on file found.

   FndHnd = FindFirstFile("C:\\*.txt", &FindDat);
   Done = (FndHnd == NULL);       // Indicate we are done if nothing was found.

   while (!Done)
   {
      string FileName = FindDat.cFileName;

      FileList.push_back(FileName);
      Done = !FindNextFile(FndHnd, &FindDat);
   }
   if (FndHnd)            // If there was anything found, then
      FindClose(FndHnd); // Close the find handle

Let me know if you have any questions.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2643174
nietod ,this code is wrong:

Done = (FndHnd == NULL);      

should be

Done = (FndHnd == INVALID_HANDLE_VALUE);

Regards
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 22

Expert Comment

by:nietod
ID: 2644382
Right!.  I made that misake before.  (That is from my EE notes).  I corrected my mistake when it was pointed out.  But the the correction was lost (apparently) when I lost my computer to a virus.  Thanks.  Correcting again.....
0
 

Author Comment

by:junkyboy
ID: 2649392
When I tried using the above code, Visual returns a "Access Violation" and I get strange characters instead of filenames.  This occurs after compiling/linking.
0
 

Author Comment

by:junkyboy
ID: 2649407
Sorry, I think I was doing something wrong in my code.  I got it all working.  Thank you for all your support, everyone! :)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question