# Get a list of file names in present dir?

This topic is 4499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well, that's pretty much my question. I am compiling C++ in Visual Studio 7. I'm working on saving the game state right now, but I'd like to know how to determine what save files already exist in a particular directory. Does anyone know of any system calls I can make to list out all of the filename strings in a given directory? Thanks much, jujumbura

##### Share on other sites
Check out FindFileFirst and FindFileNext, both win32 functions.

##### Share on other sites
Well,

I didn't find anything called "FindFileFirst" or "FindFileNext". What I did find is a class called "CFileFind", which seems to do what I'd like, according to the documentation.

Unfortunately, I'm getting linker errors when I try to use it. The documentation says that it needs me to include "afx.h", but the example they provide shows an include of "afxwin.h". If I include either one of these, I get a compiler error saying that the new and new[] operators are being defined twice.

Any ideas?

##### Share on other sites
Quote:
 Original post by jujumburaWell,I didn't find anything called "FindFileFirst" or "FindFileNext". What I did find is a class called "CFileFind", which seems to do what I'd like, according to the documentation.Unfortunately, I'm getting linker errors when I try to use it. The documentation says that it needs me to include "afx.h", but the example they provide shows an include of "afxwin.h". If I include either one of these, I get a compiler error saying that the new and new[] operators are being defined twice. Any ideas?

CFileFind is an MFC class. What you want is FindFirstFile and FindNextFile.

Sorry for the misnaming, but I don't see why anyone would rate me down for that. This is a very strange place.

##### Share on other sites
Thanks all,

After seeing the proper spelling, I did indeed get FindFirstFile and FindNextFile to give me the results I needed.

And Mercury, I wouldn't dream of it; I'm happy for any help I get, misspelled or not.

jujumbura

##### Share on other sites
Is there a better way to find all the existing files in a directory in C#, or would using the same C# equivelent functions be the best way?

-Chris

##### Share on other sites
Quote:
 Original post by bengaltgrsIs there a better way to find all the existing files in a directory in C#, or would using the same C# equivelent functions be the best way?-Chris

The best way to list files in a directory in C# is with DirectoryInfo

Eg:
  #using System.IO;  DirectoryInfo di = new DirectoryInfo( "." );  FileInfo[] files = di.GetFiles( "*.*" );  //Optional  foreach( FileInfo fi in files )   {     // Do something with files     Console.WriteLine( fi.Name );  }

##### Share on other sites
That was much easier than I thought, thanks.

-Chris

• 23
• 10
• 19
• 15
• 14