Link to home
Start Free TrialLog in
Avatar of Psyco
Psyco

asked on

getting a list of all files in a given directory

I am writing a mfc supporting console app and i want to get a list of all filenames in a given directory (w:\var\), i would prefer they go into an array of CStrings, but into a 2d character array would work just as well, what is the best way of going about this?

There is only 1 subdirectory in this directory, so if that is included in the list it is not a problem as the filtering i will do on the filenames afterwards will take it out of the list anyway.

Thanks in advance for your help
Avatar of jkr
jkr
Flag of Germany image

use "CFileFind":

CFileFind ff;
CStringList sl;

if ( ff.FindFile ( "w:\\var\\*.*)) {

 sl.PushBack ( ff.GetFileName());

 while ( ff.FindNextFile ()) {

  sl.PushBack ( ff.GetFileName());
 }
}

and you will have a CStringList with all the filenames...
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of valipotor
valipotor

You can also use a CListBox object and call it's Dir function.The Dir function has two params , the last one is the path of the dir and can contain wildcards.The you can retreive it as you will.
You'll find an exemple in MSDN.
Hope it helps you.
Avatar of Psyco

ASKER

when i use your code

CFileFind ff;
CStringList sl;
CString strFile;

if ( ff.FindFile ( "w:\\var\\*.*")) {

strFile = ff.GetFileName();
sl.PushBack ( strFile);

while ( ff.FindNextFile ()) {

 strFile = ff.GetFileName();
 sl.PushBack ( strFile);
}
}

i get the following errors

error C2039: 'PushBack' : is not a member of 'CStringList'
        c:\program files\microsoft visual studio\vc98\mfc\include\afxcoll.h(780) : see declaration of 'CStringList'

error C2039: 'PushBack' : is not a member of 'CStringList'
        c:\program files\microsoft visual studio\vc98\mfc\include\afxcoll.h(780) : see declaration of 'CStringList'

i changed it around a bit to

(AllFiles is a CString array & temp is an int)

CFileFind ff;
temp = 0;

if ( ff.FindFile( "w:\\var\\*.*"))
{
     AllFiles[temp] = ff.GetFileName();
     temp++;
          while ( ff.FindNextFile ())
     {
          AllFiles[temp] = ff.GetFileName();
          temp++;
     }
}

and it gives me an assertion error when trying to run
AllFiles[temp] = ff.GetFileName();
and it is coming from filefind.cpp

what is going on?
Sorry, it should have read

CFileFind ff;
CStringList sl;
CString strFile;

if ( ff.FindFile ( "w:\\var\\*.*")) {

strFile = ff.GetFileName();
sl.PushBack ( strFile);

while ( ff.FindNextFile ()) {

strFile = ff.GetFileName();
sl.AddTail( strFile); // wrong member function name
}
}
Avatar of Psyco

ASKER

with that new code, i just get back to the problem i was having after i changed the first bit round

strFile = ff.GetFileName();

that line is generating an assertion error in filefind.cpp

even calling ff.GetFileName() without assigning the return value to anything gives me the error
What line is the assertion on?
Avatar of Psyco

ASKER

after taking a little more time working through the error
i have got it working, i added a

ff.FindNextFile();

above the line causing the problem and it fixed it, apparently FindFile doesn't get ff pointing to the first file

thanks for all your help, it is really appreciated