Important alert: (current site time 7/16/2013 1:10:58 AM EDT)
 

VB icon

*** Destroy a file without getting error! ***

Email
Submitted on: 4/24/1999
By: Matt Evans 
Level: Not Given
User Rating: By 9 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 68459
 
     This DOES use the kill function, but when you use this it actually opens the file you want to destroy, cleans it out, then deletes it so you don't get any error because of sensitive data!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: *** Destroy a file without getting error! ***
' Description:This DOES use the kill function, but when you use this it actually opens the file you want to destroy, cleans it out, then deletes it so you don't get any error because of sensitive data!
' By: Matt Evans
'
' Inputs:File To Delete
'
' Returns:No more file
'
' Assumes:Make sure not to delete win.com, win.ini, config.sys, himem.sys, autoexec.bat or any of those files, lol ;D
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1650&lngWId=1'for details.'**************************************

Sub DestroyFile(sFileName As String)
Dim Block1 As String, Block2 As String, Blocks As Long
Dim hFileHandle As Integer, iLoop As Long, offset As Long
'Create two buffers with a specified 'wipe-out' characters
Const BLOCKSIZE = 4096
Block1 = String(BLOCKSIZE, "X")
Block2 = String(BLOCKSIZE, " ")
'Overwrite the file contents with the wipe-out characters
hFileHandle = FreeFile
Open sFileName For Binary As hFileHandle
Blocks = (LOF(hFileHandle) \ BLOCKSIZE) + 1
For iLoop = 1 To Blocks
offset = Seek(hFileHandle)
Put hFileHandle, , Block1
Put hFileHandle, offset, Block2
Next iLoop
Close hFileHandle
'Now you can delete the file, which contains no sensitive data
Kill sFileName
End Sub


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/8/1999 5:54:00 PMJim Matt

This doesn't work reliably. It does not account for the fact that starting with Windows 95 the operating system caches file writes. If the cache is not manually flushed after the wipe, nothing will be written to disk when Windows 95 sees that the file is just going to be erased anyway.
(If this comment was disrespectful, please report it.)

 
5/10/1999 1:19:00 PMWes Savage

hmmm I use a file list box with all attributes set true, see if the file is there (doing a Dir$ causes crash), I then SetAttr, 0 (read onlys cause crash) and then kill ..... even the cache sees it as gone! Simple and elegant no?
(If this comment was disrespectful, please report it.)

 
10/29/1999 9:18:00 AMOddOne

I write data-destruction software. This code is on the right track, but lacking. A few things you need to know:

1 - If you open a file using the API and don't use overlapped I/O (this code, etc.) you must write data to disk in even increments of cluster size or the write to disk may fail. (WriteFile API call is beter but WILL fail under this condition.)

2 - Using those two characters is not the best combination. Better off using Chr(85) and Chr(170), which in binary are 01010101 and 10101010 respectively. Also known as the binary checkerboard pair.

3 - The tactic used will fail due to Windows' file I/O caching. You must -use- the caching for this to work. How you do it is by performing your writes for ONE pass, calling the FlushBuffer API to force Windows to flush the cache to disk, closing the handle, and repeating for the other pass.
(If this comment was disrespectful, please report it.)

 
2/23/2000 8:34:14 AMLee

This code is great - just what i needed. Like any code it works well in some situations and not others - i cant use kill c:\... in mine - so this comes in just dandy :)

Cheers

(If this comment was disrespectful, please report it.)

 
2/24/2000 9:23:28 AMLepe

I had problems killing archives, with this, my problem was solved. Thank you!
(If this comment was disrespectful, please report it.)

 
6/11/2000 4:29:39 AMetrask

I have a text box (txtFileName) that has the name of the file in it and when I press "Delete" it adds the path and the "\" at the end. What would I do to this code to make it compatible?
(If this comment was disrespectful, please report it.)

 
3/25/2001 1:57:49 PMAndrew

This code is great...I just got rid of the last line, and now i am using it to empty out text files.
(If this comment was disrespectful, please report it.)

 
5/25/2001 12:35:38 PMT C

I need some help if anyone can provide some. The situation is I have multiple users that use my exe and it writes all the data to files on one server. The problem is that when I go to delete the old outdated data I get an error that Nt can't find the file even though I see it and know it exist. It happens only on some and not all files. Can provide more info if need be.
(If this comment was disrespectful, please report it.)

 
12/19/2001 8:32:46 AMRobert Jackson

This may not be the best way in my project, but not knowing any other way, this solved my problem. I create an Excell file and at a certain point need to delete it. This code worked for me. If anybody knows of a more elegant way, please let me know. I am still learning.
(If this comment was disrespectful, please report it.)

 
2/13/2002 3:39:55 PMAngie

This didn't work for me, it still didn't kill the file. I receive the Run time error 70, Permission denied.
(If this comment was disrespectful, please report it.)

 
2/14/2002 5:36:09 PMDenny

How do i use this Code. LoL. Little Help Here.
(If this comment was disrespectful, please report it.)

 
5/28/2002 4:08:43 PMEliteProgrammerX

well just go get the deleter.ocx and that will solve your problems!!!!!!!!!
(If this comment was disrespectful, please report it.)

 
8/11/2002 2:50:00 AMCbrine99

What can this do that the kill function can't?
(If this comment was disrespectful, please report it.)

 
12/14/2002 5:09:54 AM

Hmmm....How can I write over the file that is running? I just get a file access error message. I want a self-destructing file. I don't even care about deleting it.
(If this comment was disrespectful, please report it.)

 
7/14/2003 1:49:42 AMSimon J Smith

Never ever seen a situation I would need this in the last 10 years.

(If this comment was disrespectful, please report it.)

 
11/9/2003 10:08:03 PM

hai..Thanks!

(If this comment was disrespectful, please report it.)

 
2/13/2004 4:24:54 PM

This code doesn't work for me.
The best way to kill file without geting the 70 error number is... to make sure the application doesn't have handle to the file :)
use:
Close #FileNum

Thanks anyway :)
(If this comment was disrespectful, please report it.)

 
3/26/2004 4:47:16 AM

yes, Very good, this can come in very handy.. keep up the good coding!!
(If this comment was disrespectful, please report it.)

 
6/18/2004 3:22:56 AM

This does not work. How can I use it properly. Could you tell more?
(If this comment was disrespectful, please report it.)

 
9/17/2004 2:10:26 AMMichael Vainshtein

I had to wait some time because the file was 700MB.. but it worked in the end. gr8 code 5 from me..
What i don't understand is that you have so many feedbacks and relatively small amount of voters..
(If this comment was disrespectful, please report it.)

 
1/14/2005 3:37:25 AM

being a newbie... how do i make it work.. can anyone send me the working version of this program.... to mex_young@yahoo.com
(If this comment was disrespectful, please report it.)

 
1/20/2005 2:20:08 PM

doesn't work for the files which whoe "read Only" option are checked...how do u think the critical sytem files can be deleted...but did i make a mistake... who can help me out...
(If this comment was disrespectful, please report it.)

 
4/29/2005 7:56:19 AM

Nice. :)
(If this comment was disrespectful, please report it.)

 
7/26/2005 12:17:34 AMDigital Razor

one thing that you can do to get around the read only is use ,VbNormal ,vbhidden ,vbsystem ,vbreadonly switch it to vbNormal poof no read only.. and one other thing that might help others with file killing name "C:\junk.avi" as "C:\junk.exe" exe files can be turned into junk very rapidly then with cmd1 open "C:\junk.exe" for input as #1 --- print #1, "###Deleted###File####" then
close #1 dont ever forget to close the file . as a extra safety name "C:\junk.exe" as "C:\RandomFile.fuk" basicly mask the name as well as the old extention with new names and then erase it ..
(If this comment was disrespectful, please report it.)

 
8/22/2005 4:45:35 AMWAWO

Special Coding
(If this comment was disrespectful, please report it.)

 
10/11/2005 8:37:33 AM

thenk yuooo

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.