Solved

FindFirstFile and FindNextFile usage

Posted on 2000-03-21
6
598 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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