Advertisement

VC++: How to list files in a directory?

Started by July 25, 2000 12:45 PM
5 comments, last by Hans 24 years, 4 months ago
How to list all the files in a directory that is on your hard drive (=not on the net)? All I want is to view contents of a local directory to a char-string. Or better would be if I could define the search, like "*.map". So I don't need any dialog-style thing. I just need a function that I can use to do this: char filename[100],retval=1; - some init stuff here, like which directory to view and what files to look for - while(retval) { retval=GetNextFileFromDirectory(filename); } This should be easy but I can't find any info on this. I tried looking from MSDN but found nothing. And I even asked here but I couldn't get the FindFirstFile, CFileFind etc. working. Are they even the right methods for the thing I need? Edited by - Hans on 7/25/00 12:54:26 PM
I dont know about VC, but back in the good old days of DOS Borland, findfirst and findnext would be exactly what you''re looking for. They should still be in VC, check it out.
They also accept wildcards, like *.map



ByteMe95::~ByteMe95()
ByteMe95::~ByteMe95()My S(h)ite
Advertisement
i don''t have a documentation handy, so expect some errors in that code (i am assuming you want to use win32 api)

WIN32_FIND_DATA fd;
HFIND hFind = FindFirstFile("*.exe" , &fd);
if( hFind != INVALID_HANDLE_VALUE)
{
do
{
//do something with fd.cFileName
}while(FindNextFile(hFile, &fd));
FindClose(hFile);
}


hope that helps
ridcully
There is a very simple way to any normal DOS stuff in your programs, its a funtion called:
system()
this basically allows anything you can do in dos to do in here.
for what you need this would work:
    string dirname = "c:\\searchdir";  //directory to searchstring stype = "*.map";    //file type to findstring syscom = "dir " + stype        + " /B " + dirname + " |more > temp.tmp";  //the commandsystem(syscom.c_str());  //execute the commandfstream fp;  fp.open("temp.txt", ios::in);  //open the file//this temp.txt file contatin the full path to each file in the//directory, one per line(so u would use getline for each one)//to see how my command works, try this://go to dos prompt and type: //dir c:\ /B//also u could add after  |more > blah.tmp//to send it to a file instead of screen    

btw there may be errors in the code but u get the idea.
good luck!

-blide
blide@mail.com
-blideblide@mail.com
Got it working now, thanks! I used the findfirst-method.
But I had to add some libraries and now I always get this when linking:

LINK : warning LNK4089: all references to "ADVAPI32.dll" discarded by /OPT:REF

And same to "SHELL32.dll" and "comdlg32.dll" too. How to get rid of those warnings? (I only added two libraries to the program and it didn''t compile without them both)

When using the findfirst, if I do a wildcard search of *.txt it also finds *.txt2 and *.txtawkrl even though I don''t need those. Yea, I know this is a remain of the old 8.3 file system but these new functions shouldn''t have this kind of problems.

-Hans
Warnings like that only happen if (no. 1) you are in release build mode, and (no. 2) because you''ve linked to libraries and headers but haven''t used any of their functions...

For now, just compile in debug build until you need to distribute your app...

========
Smidge
--Mr Smidge
Advertisement

Check this link out on the MSDN. It appears to have what your looking for.


http://msdn.microsoft.com/library/default.asp?URL=/library/psdk/winbase/filesio_4lf7.htm

This topic is closed to new replies.

Advertisement