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.
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.