Jump to content
Fórum Script Brasil
  • 0

(Resolvido) Problema na função de Deletar Pastas e Arquivos


MagoDanger

Question

Olá amigos, bom dia!

Estou precisando de uma função para deletar todos os arquivos de umas determinada pasta e depois excluir a pasta

Tentei fazer da seguinte forma, porém funciona a parte aonde deleta todos os arquivos de dentro da pasta... MAS, não deleta apasta.

O que estou fazendo de errado,?

** Essa parte FUNCIONA:

var
i: integer;
sr: TSearchRec;

begin
I := FindFirst('\\server\listagem\images\' + DBText1.caption + '\*.*', faAnyFile, SR);
while I = 0 do
begin
  DeleteFile('\\server\listagem\images\' + DBText1.caption + '\' + SR.Name);
  I := FindNext(SR);

Essa parte é a que NÃO funciona:
begin
Windows.RemoveDirectory('\\server\listagem\images\' + DBText1.caption );

end; end; end;

Erro apresentado: Incompatible types: 'String' and "PAnsiChar'

Obrigado, abraços!

Edited by MagoDanger
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

excluir o diretório com todos os arquivos

EX:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    function BrowseForFolder:string;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses ShlObj;

procedure DeleteDir(const DirName: string);
var
  Path: string;
  F: TSearchRec;

begin
  Path:= DirName + '\*.*';
  if FindFirst(Path, faAnyFile, F) = 0 then begin
    try
      repeat
        if (F.Attr and faDirectory <> 0) then begin
          if (F.Name <> '.') and (F.Name <> '..') then begin
            DeleteDir(DirName + '\' + F.Name);
          end;
        end
        else
          DeleteFile(DirName + '\' + F.Name);
      until FindNext(F) <> 0;
    finally
      FindClose(F);
    end;
  end;
  RemoveDir(DirName);
end;

procedure TForm1.Button1Click(Sender: TObject);
var DPath : string;
begin
   Dpath := BrowseForFolder;
   If Dpath <> '' then
   begin
      ShowMessage('O DIRETÓRIO ' + DPath + ' E TODOS OS ARQUIVOS '+#13+#13+
      'SERÃO DELETADOS.');
      DeleteDir(DPath);
   end;
end;

{uses ShlObj}
function TForm1.BrowseForFolder:string;
var
BrowseInfo : TBrowseInfo; {browse info structure for the API function call}
PIDL : PItemIDList; {a PIDL, the storage method for paths used by Shell}
SelectedPath : array[0..MAX_PATH] of Char; {the buffer where the result will be returned}

begin
   Result := '';
   { initialize TBrowseInfo structure to nulls (0) }
   FillChar(BrowseInfo,SizeOf(BrowseInfo),#0);
   BrowseInfo.hwndOwner := Handle; {Form1.Handle, the default}
   BrowseInfo.pszDisplayName := @SelectedPath[0]; {buffer address for API to store result}
   BrowseInfo.lpszTitle := 'Select a folder';
   BrowseInfo.ulFlags := BIF_RETURNONLYFSDIRS; {only file system folders}

   { show the folder browser and return the result to the PIDL itemlist }
   PIDL := SHBrowseForFolder(BrowseInfo);

   { get selected directory from the itemlist and include the full path}
   if Assigned(PIDL) then
      if SHGetPathFromIDList(PIDL, SelectedPath) then
Result := string(SelectedPath);
   end;

end.

end.

abraço

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...