Article View: borland.public.cppbuilder.nativeapi
Article #11041Re: FindFirstFile, FindNext Problem
From: Remy Lebeau (TeamB)
Date: Mon, 01 Mar 2004 04:26
Date: Mon, 01 Mar 2004 04:26
87 lines
3845 bytes
3845 bytes
"Martin C" <martinc@aol.com> wrote in message
news:4042b3bc@newsgroups.borland.com...
> sFullPath = szPath;
What is szPath declared as an where does it get its value? Neither is
present in the code you have shown.
> dwAttributes = GetFileAttributes(sFullPath.c_str());
You are not checking to see if GetFileAttributes() returns an error. In
fact, you don't need to call GetFileAttributes() at all, the file's
attributes are already contained in the WIN32_FIND_DATA structure.
> DeleteAllFilesFolders(sFullPath.c_str(),
MaskToFind);
You are not returning any return value from DeleteAllFilesFolders() to make
sure that it is even safe to call RemoveDirectory().
> RemoveDirectory(sFullPath.c_str());
You are not checking to see if RemoveDirectory() returns an error.
> if(MaskToFind == "*.*")
You don't need to test for that individually, MatchesMask() will handle
"*.*" as well.
> DeleteFile(sFullPath);
You are not checking to see if DeleteFile() returns an error.
Try this code instead:
bool __fastcall TForm1::DeleteAllFilesFolders(const AnsiString &Folder,
const AnsiString &MaskToFind)
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
String sFullPath, sFindFilter, sCurrentFolder;
bool Result = true;
sCurrentFolder = IncludeTrailingBackslash(Folder);
sFindFilter = sCurrentFolder + "*.*";
hFind = FindFirstFile(sFindFilter.c_str(), &wfd);
if( hFind != INVALID_HANDLE_VALUE )
{
do
{
if( (_tcscmp(wfd.cFileName, _T(".")) == 0) ||
(_tcscmp(wfd.cFileName, _T("..")) == 0) )
continue;
sFullPath = sCurrentFolder + String(wfd.cFileName);
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY )
{
Result = SetFileAttributes(sFullPath.c_str(),
wfd.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
if( !Result )
break;
}
if( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
Result = DeleteAllFilesFolders(sFullPath, MaskToFind);
if( Result )
Result = RemoveDirectory(sFullPath.c_str());
}
else
{
if( MatchesMask(wfd.cFileName, MaskToFind) )
Result = DeleteFile(sFullPath);
}
}
while( (Result) && (FindNextFile(hFind, &wfd)) );
FindClose(hFind);
}
return Result;
}
Gambit
Technical Details
Message-ID:
<4042b964$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!peernews3.colt.net!newsfeed00.sul.t-online.de!t-online.de!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4042b3bc@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,cd3253abe6cdd94c X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-02-29 20:25:14 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4042b3bc@newsgroups.borland.com> Subject: Re: FindFirstFile, FindNext Problem Date: Sun, 29 Feb 2004 20:26:07 -0800 Lines: 89 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.22.138.253 Message-ID: <4042b964$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1078114660 67.22.138.253 (29 Feb 2004 20:17:40 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!colt.net!peernews3.colt.net!newsfeed00.sul.t-online.de!t-online.de!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail