Kasowanie całego folderu

0

Witam

Szukałem na forum, ale to co znalazłem niezadowoliło mnie, jak można skasować cały folder, wraz z podfolderami, i zawartymi w nich plikami ? Z góry dzięki

0

findfirst + findnext + rekurencja.

0

a może troszke konkretnirj, bo ja wiem może jakiś kodzicz, czy coś

0

Zmodyfikuj to odpowiednio

procedure DeleteAll(DirPath:string;FSubdirs:bool);
const
 Template='*.*';


var
   FInfo:         TSearchRec;
   DirSL:         TStringList;
   FileResult:    Integer;
   PName:         String;
   DName:         String;
   i:             Integer;
   ext:           String;
begin
     DirSL := TStringList.Create;
     if (DirPath[Length(DirPath)] = '\') then
       DName := DirPath
     else
       DName := DirPath + '\';
     FileResult := FindFirst( DName+Template, faAnyFile, FInfo );
     while (FileResult = 0) do
       begin
       PName := DirPath;
       if (DirPath[Length(DirPath)] = '\') then
         PName := Copy(PName,1,Length(DirPath)-1);
       PName := PName + '\' + FInfo.Name;
       if ((FInfo.Attr and faDirectory) = faDirectory) then
         begin
         if (FInfo.Name[1] <> '.') then
           DirSL.Add(PName)
         end
       else
         begin
          deleteFile(pname);
         end;

       FileResult := FindNext(FInfo);
       end;

     FindClose(FInfo);
    if FSubdirs then begin
     for i := 0 to DirSL.Count-1 do
       begin
       DeleteAll(DirSL[i],FSubdirs);
    end;
     DirSL.Free;
end;
0

Funkcja winapi pozwala na kasowanie, kopiowanie itp całych katalogów.

dodaj do moduł ShellAPI;

Var 
   s:tshfileopstruct;
begin
  s.wnd:=handle;
  s.wfunc:=fo_delete;  //operacja kasowanie katalogu lub pliku
  s.pfrom:='c:\katalog';   //co kasujemy?
  s.fflags:=fof_noconfirmation or fof_renameoncollision;
  s.fanyoperationsaborted:=true; //możliwość anulowania
  shfileoperation(s);  //wykonaj
end;
0

Dodatkowo pokazuje na ListBoxie jakie "pliki usunął" ;)

function KasujKatalog(const dir, path: String): Boolean;
var
SR     : TSearchRec;
Found  : Integer;
Source : String;
begin
Source := Dir + Path;
Found  := FindFirst(Source + '\*.*', faAnyFile, SR );
  try
   while (Found = 0) do
     begin
       if (SR.Name<>'.') and (SR.Name <> '..') then
         begin
           if (SR.Attr and faDirectory) <> 0 then KasujKatalog(Dir, Path + '\' + SR.Name)
         else
           begin
            FileSetAttr(Source + '\' + SR.Name, FileGetAttr(Source + '\' + SR.Name) and not (faReadOnly or faHidden));
            if not DeleteFile(Source + '\' + SR.Name) then Result := False;
            Form1.ListBox1.Items.Add(Source + '\' + SR.Name);
           end;
         end;
       Found := FindNext(SR);
     end;
  finally
    FindClose(SR);
  end;
    RemoveDir(Source);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
KasujKatalog('C:\','Folder_Do_Skasowania');
end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1