Avatar billede stussy_1000 Nybegynder
30. oktober 2000 - 10:56 Der er 12 kommentarer

Hvad er komandoen for at få delphi til at slette en mappe tak

prblemet er at efter jeg har oprettet min nye mappe skal jeg kunne slette den gamle fra programmet  tak tak
Avatar billede borrisholt Novice
30. oktober 2000 - 10:57 #1
function RemoveDir(const Dir: string): Boolean;

Jens B
Avatar billede borrisholt Novice
30. oktober 2000 - 10:57 #2
Ligger i sysutils.

Jens B
Avatar billede stussy_1000 Nybegynder
30. oktober 2000 - 10:59 #3
hvad skal jeg skrive
Avatar billede borrisholt Novice
30. oktober 2000 - 11:00 #4
RemoveDir(\'C.\\test\');

Jens B
Avatar billede borrisholt Novice
30. oktober 2000 - 11:00 #5
hov ..

RemoveDir(\'C:\\test\') ...
Avatar billede stussy_1000 Nybegynder
30. oktober 2000 - 11:07 #6
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;

end.
Avatar billede borrisholt Novice
30. oktober 2000 - 11:10 #7
TForm1.Button1Click(Sender: TObject);
begin
  RemoveDir(\'E:\\games\\shoot\'); 
end;

Avatar billede delphidaner Nybegynder
30. oktober 2000 - 11:36 #8
Det skal lige pointeres at mappen skal være tom for at det virker.
Avatar billede borrisholt Novice
30. oktober 2000 - 12:45 #9
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;


Avatar billede borrisholt Novice
30. oktober 2000 - 13:04 #10
hov der var vist gået ged i den før :

Siste procedure skal laves sådan her :

function DelTreeA (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);

      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;

Jens B
Avatar billede borrisholt Novice
09. januar 2001 - 14:56 #11
stussy_1000 >DU dukker pludselig op efter 2 1/2 måned afviser mine svar uden grund ... Hvad har du etenlig gang i ?

Jens B
Avatar billede gizmo-gizmo Nybegynder
07. juli 2002 - 18:51 #12
fjols.
Avatar billede Ny bruger Nybegynder

Din løsning...

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester