Monthly Archives: May 2013

Wipe a memory card

Today I had rather an odd task in mind. I needed to wipe a Compact Flash memory card and be reasonably confident the data was completely gone. Perhaps not usually a task for VBA but it’s what I do quickest so why not?

My idea was simple:

  1. Format the drive
  2. Write junk data to the drive until it’s full
  3. Delete the junk data
  4. Format the drive again – for good measure!

Step 2 is the interesting bit. I decided to create a text file (using the VBA Open command) and keep filling it with random numbers until the card was full. Then close it and delete the file.

Now if you try to write data to a drive with no space on it, VBA gets upset! Clearly an error of some sort was bound to happen eventually so I used On Error Resume Next and Application.DisplayAlerts = False to bypass it. Here’s what I came up with:

Sub WipeIt()
    Dim fileNo As Integer
    Const fileName As String = "K:\rubbish.txt" 'junk data file - change as needed
    
    Application.DisplayAlerts = False
    On Error Resume Next
    
    Randomize
    fileNo = FreeFile
    Open fileName For Output As #fileNo
    Do
        Print #fileNo, Rnd()
        If Err.Number  0 Then Exit Do
    Loop
    Close #fileNo
    Kill fileName
    
    On Error GoTo 0
    Application.DisplayAlerts = True
End Sub

Job done!