🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #11040

FindFirstFile, FindNext Problem

#11040
From: Martin C
Date: Sun, 29 Feb 2004 20:52
82 lines
3591 bytes
Hi all,

I am trying to use the following code to erase all (Files and Folders) under
a specified folder (ex: C:\\MyFolder), although each time I try to run my
application the function start giving weird results, for example sometime it
erases all files under (ex: C:\\MyFolder) and leave all subfolders and files
inside the subfolders, and sometime it erases all files under (ex:
C:\\MyFolder) and files under subfolders and leave subfolders without
erasing them !!, so can someone help me please and tell me what I am doing
wrong here....

Thanks

Environment: BCB 5 Ent
Windows: XP

Function:
======

ex. for calling:       DeleteAllFilesFolders("C:\\MyFolder", "*.*");

void __fastcall TForm1::DeleteAllFilesFolders(AnsiString Folder, const
AnsiString &MaskToFind)
{
        WIN32_FIND_DATA wfd;
        HANDLE hFind;
        String sFullPath;
        String sFindFilter;
        DWORD dwAttributes = 0;

        sFindFilter = Folder;
        sFindFilter += "\\*.*";

        if ((hFind = FindFirstFile(sFindFilter.c_str(), &wfd)) ==
INVALID_HANDLE_VALUE)
        {  return;
        }

        do
        {
                if (_tcscmp(wfd.cFileName, _T(".")) == 0 ||
_tcscmp(wfd.cFileName, _T("..")) == 0)
                {
                   continue;
                }

                 sFullPath = szPath;
                 sFullPath += "\\";
                 sFullPath += wfd.cFileName;

                 dwAttributes = GetFileAttributes(sFullPath.c_str());
                 if (dwAttributes & FILE_ATTRIBUTE_READONLY)
                {
                      dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
                      SetFileAttributes(sFullPath.c_str(), dwAttributes);
                }

                if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
               {
                       DeleteAllFilesFolders(sFullPath.c_str(), MaskToFind);
                       RemoveDirectory(sFullPath.c_str());
                }
                else
               {
                       if(MaskToFind == "*.*")
                       {
                                DeleteFile(sFullPath);
                       }
                       else
                       {
                             if(MatchesMask(wfd.cFileName, MaskToFind))
                            {
                                DeleteFile(sFullPath);
                            }
                       }
               }
        }
        while(FindNextFile(hFind, &wfd));
        FindClose(hFind);
}
//--------------------------------------------------------------------------
--
Technical Details
Message-ID: <4042b3bc@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,cd3253abe6cdd94c,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-02-29 20:09:34 PST
From: "Martin C" <martinc@aol.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: FindFirstFile, FindNext Problem
Date: Sun, 29 Feb 2004 22:52:09 +0200
Lines: 84
Organization: V Safe
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: 81.10.4.238
Message-ID: <4042b3bc@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1078113212 81.10.4.238 (29 Feb 2004 19:53:32 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail