hvis nu jeg gerne vil slette en mappe der er som følger E:\\games\\shoot hvad skal jeg skrive i procedure TForm1.Button1Click(Sender: TObject); begin hvad skal jeg skrive HER end;
Alternativt kan du også lave en funktion der sletter et biblotek med samtlige undermapper og file.
enten ved at lade Windows gøre det :
uses ShellAPI;
Function DelTree(DirName : string): Boolean; var SHFileOpStruct : TSHFileOpStruct; DirBuf : array [0..255] of char; begin try Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0); FillChar(DirBuf, Sizeof(DirBuf), 0 ); StrPCopy(DirBuf, DirName); with SHFileOpStruct do begin Wnd := 0; pFrom := @DirBuf; wFunc := FO_DELETE; fFlags := FOF_ALLOWUNDO; fFlags := fFlags or FOF_NOCONFIRMATION; fFlags := fFlags or FOF_SILENT; end; Result := (SHFileOperation(SHFileOpStruct) = 0); except Result := False; end; end;
eller ved selv at lede bibloteket igennem og slette undervejs :
function DelTree (Path : string) : boolean;
function ConcatPaths (Path1, Path2 : string) : string; begin if (Path1 = \'\') or (Path2 = \'\') then result := Path1 + Path2 else begin if (Length (Path2) > 0) and (Path2 [1] = \'\\\') then Path2 := Copy (Path2, 2, Length (Path2)-1);
Under alle omstendigheder kan de kaldes sådan her :
procedure TForm1.Button1Click(Sender: TObject); begin DelTree (\'E:\\games\\shoot\'); end;
Jens B if (Length (Path1) > 0) and (Path1 [Length (Path1)] = \'\\\') then result := Concat (Path1, Path2) else result := Concat (Path1, \'\\\', Path2) end end;
var Find : TSearchRec; Res : integer; begin result := false; if Path = \'\' then Exit;
if Path [Length (Path)] = \'\\\' then Path := Copy (Path, 1, Length (Path)-1);
Res := FindFirst (ConcatPaths (Path, \'*.*\'), faAnyFile, Find);
try while Res = 0 do begin if (Find.Name <> \'.\') and (Find.Name <> \'..\') then if (Find.Attr and faDirectory) = 0 then begin FileSetAttr (ConcatPaths (Path, Find.Name), 0); DeleteFile (PChar (ConcatPaths (Path, Find.Name))) end else DelTree (ConcatPaths (Path, Find.Name)); Res := FindNext (Find) end finally FindClose (Find) end; result := RemoveDir (Path) // end;
function ConcatPaths (Path1, Path2 : string) : string; begin if (Path1 = \'\') or (Path2 = \'\') then result := Path1 + Path2 else begin if (Length (Path2) > 0) and (Path2 [1] = \'\\\') then Path2 := Copy (Path2, 2, Length (Path2)-1);
if (Length (Path1) > 0) and (Path1 [Length (Path1)] = \'\\\') then result := Concat (Path1, Path2) else result := Concat (Path1, \'\\\', Path2) end end;
var Find : TSearchRec; Res : integer; begin result := false; if Path = \'\' then Exit;
if Path [Length (Path)] = \'\\\' then Path := Copy (Path, 1, Length (Path)-1);
Res := FindFirst (ConcatPaths (Path, \'*.*\'), faAnyFile, Find);
try while Res = 0 do begin if (Find.Name <> \'.\') and (Find.Name <> \'..\') then if (Find.Attr and faDirectory) = 0 then begin FileSetAttr (ConcatPaths (Path, Find.Name), 0); DeleteFile (PChar (ConcatPaths (Path, Find.Name))) end else DelTree (ConcatPaths (Path, Find.Name)); Res := FindNext (Find) end finally FindClose (Find) end; result := RemoveDir (Path) // end;
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.