Everybody knows that simply deleting a file from a hard disk doesn't truly make the file impossible to recover. Often, this is a good thing, but sometimes you want a file to be thoroughly deleted. Keep reading to find out how to accomplish this.
Secure File Deletion in Delphi - The rest of the code (Page 4 of 4 )
Click on the "Delete Files" button and add the following code:
procedure TForm1.SpeedButton1Click(Sender: TObject); var files:integer; begin //if no files are selected then show a warning if listbox1.Items.Count < 1 then begin MessageDlg('There are no files selected to delete. Please try again.', mtWarning,[mbOk], 0); exit; end else begin if MessageDlg('You will not be able to recover the files once shredded.'+#13#10+' Do you want to continue?', mtInformation, [mbYes, mbNo], 0)=mrYes then for files:=0 to listbox1.Items.Count-1 do begin //retrieve dragged files one by one, overwrite and delete getridofFile(listbox1.Items[files]); end; //update the listbox filelistbox1.Update; //reset progressbar,labels and form prog.Position:=0; label1.Caption:='0'; form1.listbox1.Clear; label1.Caption:='0'; //Display a message MessageDlg('File(s) deleted', mtInformation, [mbOk], 0); end; end;
This is the most important bit of code. It actually executes the two procedures outlined earlier. Let's go through it. First, it checks whether the files have been selected, and displays the appropriate messages. If files have been selected, then it calls the "getridofFile" procedure, and displays a message depending on whether the operation was successful.
Next, click on the listbox, then go to the object inspector’s events tab and double click on the "dragdrop" field and enter the following code:
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer); var i,c: integer; begin
with FileListBox1 do if SelCount > 0 then for i:=0 to Items.Count-1 do if Selected[i] then ListBox1.Items.Add(items[i]); c:=ListBox1.Items.count; label1.Caption:=inttostr(c);
end;
Select the listbox and do the same, except double click on dragover field and enter the code below:
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin //where are the files coming from? if source is TFilelistbox then accept:=true; end;
The above code will handle the drag and drop actions, which are central to the app. Basically, the user will be able to select more than one file at a time and drag and drop them into the listbox.
The last bit of code will deal with setting the directory list box to a default drive and setting the progressbar to 100.
procedure TForm1.FormCreate(Sender: TObject); begin //set progressbar max prog.Max:=100; //set directory default directorylistbox1.Directory:='c:\'; end;
This will enable you to select all files at once…
procedure TForm1.SpeedButton2Click(Sender: TObject); begin filelistbox1.SelectAll; {MessageDlg('Right Click and drag selected files', mtInformation, [mbOk], 0);} MessageDlg ('Click and hold down left mouse button and drag selected files', mtCustom, [mbOK], 0); end;
That’s it. Start deleting!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.