Author: Yovanny Rodriguez
How to delete a file permanently?
Answer:
If you want to get rid of a file normally you just delete it. But someone else can
undelete it if the file hasn't been wiped correctly. For security purposes, to
insure that certain files are permanently gone, the WipeFile procedure writes over
the data in the file with random characters and then erases it.
1 procedure WipeFile(FileName: string);
2 var3 buffer: array[0..4095] of Byte;
4 max, n: LongInt;
5 i: Integer;
6 fs: TFileStream;
7 8 procedure RandomizeBuffer;
9 var10 i: Integer;
11 begin12 for i := Low(buffer) to High(buffer) do13 buffer[i] := Random(256);
14 end;
15 begin16 fs := TFilestream.Create(FileName, fmOpenReadWrite or fmShareExclusive);
17 try18 for i := 1 to 3 do19 begin20 RandomizeBuffer;
21 max := fs.Size;
22 fs.Position := 0;
23 while max > 0 do24 begin25 if max > SizeOf(buffer) then26 n := SizeOf(buffer)
27 else28 n := max;
29 fs.write(Buffer, n);
30 max := max - n;
31 end;
32 FlushFileBuffers(fs.Handle);
33 end;
34 finally35 fs.Free;
36 end;
37 Deletefile(FileName);
38 end;