Shred Files - Delete Files Securely

posted 17 Mar 2010, 18:58 by Delphi Basics
This procedure allows you to securely delete your files without a trace.

procedure ShredFile(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
  
AssignFile(f, FileName);
  Reset(f, 1);
  n := FileSize(f);
  for j := 0 to Counttowrite do
  begin
    for 
i := 1 to div Buffer do
    begin
      
BlockWrite(f, FillBuffer[j], Buffer);
    end;
  end;
  CloseFile(f);
  RenameFile(FileName, ExtractFilepath(FileName) + '$000000.tmp');
  DeleteFile(ExtractFilepath(FileName) + '$000000.tmp');
end;

Usage:
procedure ShredAndDeleteFile(const FileName: string);
var
  
newname: string;
begin
  
newname := ExtractFilepath(FileName) + '$000000.tmp';
  if not RenameFile(FileName, newname) then
    raise
    
Exception.CreateFmt('Fehlercode 2: Can %s not Delete!', [FileName]);
  ShredFile(newname);
  DeleteFile(newname);
end;
Comments