Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CFileFind

128 views
Skip to first unread message

dai

unread,
Oct 17, 2001, 7:42:27 AM10/17/01
to
How to find file in subfolder using CFileFind?

--
Thanks,

dai


Tim Slattery

unread,
Oct 17, 2001, 8:46:06 AM10/17/01
to
"dai" <dai...@263.net> wrote:

>How to find file in subfolder using CFileFind?

You can tell that the name you're examining belongs to a folder by
calling CFileFind::IsDirectory(). Once you've determined that it is,
you'll need to create a new CFileFind object to iterate through the
files and folders that it contains. This is a pretty good case for a
recursive function: it calls itself each time it finds a subdirectory
to walk through.

--
Tim Slattery
MS MVP(DTS)
Slatt...@bls.gov

headexplodes

unread,
Oct 24, 2001, 7:25:31 AM10/24/01
to
Hi,

all i want to be able to do is to get a list of all the files in a
particular directory. I tried using CFileFind. It works fine for the current
folder but i cant figure out how to change it. Is this the best way to list
files in one folder? if yes, how do i change the folder it searches. if no,
what should i use?

Thanx for the help, robert.

--
I always use MSVC++6
MSN Messenger and E-Mail: head_e...@hotmail.com

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.

Tim Slattery

unread,
Oct 24, 2001, 8:49:37 AM10/24/01
to
"headexplodes" <head_e...@hotmail.com> wrote:

>Hi,
>
>all i want to be able to do is to get a list of all the files in a
>particular directory. I tried using CFileFind. It works fine for the current
>folder but i cant figure out how to change it. Is this the best way to list
>files in one folder? if yes, how do i change the folder it searches. if no,
>what should i use?

Create a new CFileFind object to search another folder. A typical way
to do this is a recursive function that walks a tree by calling itself
when it finds a subdirectory that needs to be enumerated. Each call
instantiates another CFileFind object. Each time the function returns,
a CFileFind object goes out of scope and is destructed.

MrETS

unread,
Oct 24, 2001, 11:25:14 AM10/24/01
to
This code is set to tabs=3
#define CP (char*)(LPCTSTR) // Get char* from CString object

call like this:
FindFiles( "D:\Whatever\whatever","*.exe",500,TRUE)
You must provide a function called
BOOL ProcessFoundFile( CP ss );
that will handle any files that match your mask.
//==========================================================================
===================
// Very Good file finder, will recurse into subdirectories
// It can use any standard DOS/WIN filemask in the 'mask'
// You could use this function in many applications!
//==========================================================================
===================
void ETSZip::FindFiles( char *startpath, char *mask, int limit, BOOL
subfolders ) {
if( m_Count>=limit ) return; // Reached my limit
BOOL bWorking,bFinding;
CFileFind finder,ff;
CString st,ss;
CString sWild; sWild.Format("%s\\*.*",startpath); // Everything, even Sub
Directories
CString sMask; sMask.Format("%s\\%s",startpath,mask); // Only files
matching 'mask'
// Start looking for Files (and/or Sub Directories) ------------------
bWorking = finder.FindFile(sWild);
while( bWorking && finder.FindNextFile() ) {
if( finder.IsDots() ) // skip . and .. parent directories
continue; // otherwise, we'd recurse infinitely!
if( finder.MatchesMask( FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM) )
continue; // skip Hidden and System files
st = finder.GetFilePath(); // His Wildcard Match
if( finder.IsDirectory() ) { // Found a Sub Directory ----
if( !subfolders ) continue; // No Subfolders Wanted
FindFiles(CP st,mask,limit,subfolders); // RECURSE
continue;
} // Found a File, But does it match the file mask? ------------------
bFinding = ff.FindFile(sMask); // Look only for 'sMask' type of files
while( bFinding ) { // Possibly...
bFinding = ff.FindNextFile(); // Scan Folder, trying to match 'finder'
if( ff.MatchesMask( FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM) )
continue; // skip Hidden and System files
ss = ff.GetFilePath(); // Matches our 'File Mask'
if( ss.CompareNoCase(st)==0 ) { // Match 'st' from above?
m_Count++; // He matched our mask
if( !ProcessFoundFile( CP ss ) ) { // Process this file
limit=m_Count; // Error, break loop
return; // We'll be gone in a sec
}
if( m_Count>=limit ) return; // Has found a file that
break;
}
}
}
}
// Called from above when a file is found
BOOL ETSZip::ProcessFoundFile( char *path ) {
// Handle this file
// Return FALSE to stop the loop
}

Cheers!
Val Patterson, ProTEXT Customer Support


0 new messages