Solved

FindFirstFile and FindNextFile usage

Posted on 2000-03-21
6
564 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now