Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I delete all files in a folder?

Status
Not open for further replies.

SLider3

Programmer
Jun 17, 2003
56
0
0
PT
I want to delete all the files in a folder. How can i do that?
 
It take a combination of FindFirst, FindNext, and filtering out ".", "..", and directories. After you have looked at those, post back here if you get stuck. Somewhere I have a recursive routine that transverses directories and subdirectories.

I also remember someone posting some routines in this forum for that, too. Try a search and see what you get.



James P. Cottingham
[sup]
There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
[/sup]
 
Hi SLider,

unfortunately I am not familiar with the FindFirst and FindNext routines, I am doing a way around:

Code:
//----------------------------------------------------
void __fastcall TMainForm::DeleteAllUserFilesBitBtnClick(
      TObject *Sender)
{
AnsiString ThisDataPath;

if (DataPath[DataPath.Length()] == '\\')
   ThisDataPath=DataPath+MyObjectName;
else
   ThisDataPath=DataPath+"\\"+MyObjectName;

if (DirectoryExists(ThisDataPath))
{
  TFileListBox *TempFLB = new TFileListBox(this);              
  TempFLB->Parent = MyForm;
  TempFLB->Visible = false;
// load the filenames: 
  TempFLB->Directory = ThisDataPath+"\\MyFolder\\";
  TempFLB->Items->SaveToFile(
          ThisDataPath+"\\Intermediate.txt");

  TMemo *TempMemo = new TMemo(this);
  TempMemo->Parent = MyForm;
  TempMemo->Visible = false;
  TempMemo->Lines->LoadFromFile(
          ThisDataPath+"\\Intermediate.txt");
  int interm_max = TempMemo->Lines->Count;
  AnsiString UserDirFileName;
  AnsiString Ext;
  AnsiString IntermediateFileName;
  AnsiString UserDirFileNameCompletePath;
  for (int imx = 0; imx < interm_max; imx++)
  {
    UserDirFileName = TempMemo->Lines->Strings[imx];
    UserDirFileNameCompletePath =  
         ThisDataPath+"\\MyFolder\\"+UserDirFileName;
    Ext = ExtractFileExt(UserDirFileName);

// this was changed to let the user keep the wanted (e.g. 
//   archived) files in the directory, also checking
//  DOS formatted names MY1 and MY2)
    if ( ( Ext == ".my1") ||
         ( Ext == ".My1") ||
         ( Ext == ".MY1") ||
         ( Ext == ".my2") ||
         ( Ext == ".My2") ||
         ( Ext == ".MY2") ||
         ( Ext == ".my3")   )
    {
        DeleteFile(UserDirFileNameCompletePath);
    }
 } // end  for (int imx = 0; imx < interm_max; imx++)

// DO NOT FORGET:
      delete TempMemo;
      delete TempFLB;
}//end  if (DirectoryExists(ThisDataPath)
else
{
   ShowMessage("No files (*.my1, *.my2 or *.my3) found!");
}

} // end of DeleteAllUserFiles(...)
//---------------------------------------------------------------------------

Has anybody a smoother idea?
Just a remark:
Code:
//To back up before delete (but delete the last back up),
// when necessary:

IntermediateFileName = ChangeFileExt(
           UserDirFileNameCompletePath, ".bak");
if (FileExists(IntermediateFileName))
{
    DeleteFile(IntermediateFileName);
}
RenameFile(UserDirFileNameCompletePath, 
           IntermediateFileName);
I have this in the loop over the found names (imx as counter), instead of the delete.

Best regards,
Michael


... Welladay! Welladay!
For the winds of May!
Love is unhappy when love is away!
-excerpt,see my profile- James Joyce,1907
 
Here is an example function I pulled out of a program I am working on

Code:
void __fastcall TOptionsDlg::EmptyDir(AnsiString Directory)
{
 TSearchRec sr;

 if(FindFirst(Directory+"\\*.*"
 , faAnyFile | faDirectory, sr) == 0)
 {
    	do
    	{
                AnsiString CurFile = sr.Name;

      		if(CurFile != "." && CurFile != ".."  
                                  && CurFile != "...")
      		{
                        if(DirectoryExists(Directory+"\\"+CurFile))
                        {
                        	EmptyDir(Directory+"\\"+CurFile);
                                RemoveDir(Directory+"\\"+CurFile);
                        }
                  	if(FileExists(Directory+"\\"+CurFile))
                        	DeleteFile(Directory+"\\"+CurFile);
      		}
    	}
        while(FindNext(sr) == 0);

    	FindClose(sr);
  }
}

Sorry about the messy look, the forum mashed it up.

Hope that helped, -Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top