Solved

getting a list of all files in a given directory

Posted on 2002-04-16
8
253 Views
Last Modified: 2013-11-20
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
Comment
Question by:Psyco
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6946539
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
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6946569
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
 
LVL 6

Expert Comment

by:valipotor
ID: 6947576
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
 

Author Comment

by:Psyco
ID: 6949021
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:jkr
ID: 6949060
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
 

Author Comment

by:Psyco
ID: 6949106
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
 
LVL 86

Expert Comment

by:jkr
ID: 6949112
What line is the assertion on?
0
 

Author Comment

by:Psyco
ID: 6949136
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

912 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

20 Experts available now in Live!

Get 1:1 Help Now