Secure File Deletion in Delphi - The code
(Page 3 of 4 )
I’ve tried to comment as much as possible in the code. We will start writing the two procedures that actually power this app.
Add the following lines just above the private section:
procedure overwriteFile(FileName: string);
procedure getridofFile(const FileName: string);
The following two procedures will implement the algorithm as discussed before. Add the code in the implementation section of your form:
procedure TForm1.overwriteFile(FileName: string);
const
Buffer = 1024;
Counttowrite = 34;
FillBuffer: array[0..5] of Integer = ($00, $FF, $00, $F0, $0F, $00);
var
arr: array[1..Buffer] of Byte;
f: file;
i, j, n: Integer;
begin
//open the file
AssignFile(f, FileName);
Reset(f, 1);
n := FileSize(f);
//overwrite the file 34 times
for j := 0 to Counttowrite do
begin
for i := 1 to n div Buffer do
begin
BlockWrite(f, FillBuffer[j], Buffer);
form1.label1.caption:=inttostr(form1.prog.Position)+'%';
end;
form1.prog.position:=j+1;
end;
CloseFile(f);
RenameFile(FileName, ExtractFilepath
(FileName) + '$000000.tmp');
DeleteFile(ExtractFilepath(FileName) +
'$000000.tmp');
end;
procedure TForm1.getridofFile(const FileName:
string);
var
newname: string;
begin
// first rename the file
newname := ExtractFilepath(FileName) +
'$000000.tmp';
if not RenameFile(FileName, newname) then
raise
Exception.CreateFmt('Cannot find %s !',
[FileName]);
overwriteFile(newname);
DeleteFile(newname);
end;
The overwritefile procedure overwrites the contents of the file and then saves the file as ‘$000000.tmp' while updating the progressbar at the same time. The second procedure called getridofFile, grabs the file called ‘$000000.tmp', overwrites it again and then deletes it.
The two procedures are the meat of the application, and are also very simple to use and understand. Let’s move on to the rest of the procedures needed to use the app.
Next: The rest of the code >>
More Delphi-Kylix Articles
More By Jacques Noah