Solved

FindFirstFile and FindNextFile usage

Posted on 2000-03-21
6
607 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

634 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