Solved

getting a list of all files in a given directory

Posted on 2002-04-16
8
239 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Comment Utility
What line is the assertion on?
0
 

Author Comment

by:Psyco
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

10 Experts available now in Live!

Get 1:1 Help Now