• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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
0
Psyco
Asked:
Psyco
  • 4
  • 3
1 Solution
 
jkrCommented:
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...
0
 
jkrCommented:
Or, just in case that you are not familia with these lists:

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);
 }
}
0
 
valipotorCommented:
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.
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.

 
PsycoAuthor Commented:
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?
0
 
jkrCommented:
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
}
}
0
 
PsycoAuthor Commented:
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
0
 
jkrCommented:
What line is the assertion on?
0
 
PsycoAuthor Commented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now