Solved

getting a list of all files in a given directory

Posted on 2002-04-16
8
265 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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
 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
modThree challenge 4 91
post4 challenge 28 100
fix34  challenge 9 121
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

809 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