Lenguaje de programación Borland Delphi
Este truco elimina una carpeta y todo lo que contenga (ficheros y subcarpetas) y la envía a la Papelera de Reciclaje de Windows. Admite la posibilidad de mostrar un mensaje de confirmación y el progreso de la eliminación. Pare ello crearemos un nuevo proyecto en Delphi, añadiremos un formulario y dentro de éste insertaremos un TEdit llamado "txtCarpeta", un TButton y dos TCheckBox llamados "opConfirmacion" y "opProgreso", en el Uses de la unidad añadiremos "ShellAPI", pues la función que vamos a utilizar para este truco pertenece al API de Windows:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
opConfirmacion: TCheckBox;
opProgreso: TCheckBox;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function Deltree (const carpeta : string; const confirmacion,
mostrarProgreso : boolean): boolean;
var
SHFileOpStruct : TSHFileOpStruct;
DirBuf : array [0..255] of char;
begin
try
Fillchar(SHFileOpStruct,Sizeof(SHFileOpStruct),0);
FillChar(DirBuf, Sizeof(DirBuf), 0 );
StrPCopy(DirBuf, carpeta);
with SHFileOpStruct do
begin
Wnd := 0;
pFrom := @DirBuf;
wFunc := FO_DELETE;
fFlags := FOF_ALLOWUNDO;
if not confirmacion then
fFlags := fFlags or FOF_NOCONFIRMATION;
if not mostrarProgreso then
fFlags := fFlags or FOF_SILENT;
end;
result := SHFileOperation(SHFileOpStruct) = 0;
except
result := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Deltree(txtCarpeta.Text, opConfirmacion.Checked, opProgreso.Checked);
end;
end.
Publicado el: 2004-12-26