Somacon.com: Articles on websites & etc.

§ Home > Index > Freeware

Erase Disk Free Space Script

Use this script to erase all the free space on your disk drive by filling it with blank, zeroed files. The WSF script runs on Windows and is freeware licensed under GPL. Just copy it to the drive you want to fill up, and double-click on it.

This script is handy when you want to sell or give away an old flash or hard disk drive, and you want some certainty that files on the drive are not easily recoverable. First erase all the files on the drive, then run this script. After it finishes, all user-writeable areas of the disk will have been written over with zeroes. With a 7200 rpm drive, it takes approximately one hour to fill up one hundred gigabytes. Because it does not overwrite or delete any existing files, the script is safer to use for novices.

This script is useful for the casual Windows users who want to clear out their disk with minimal fuss. You should be transferring ownership of the drive to someone who is unlikely to attempt to recover files from the drive, and you should not have highly sensitive information on the disk.

If you need something more secure, try the freeware Eraser program. You can read the source code for more details. This script can also be used as a crude benchmark to measure the raw write speed of your disk. The Western Digital 7200 rpm drives on my system wrote the files at 32-36 MB/second.

If you don't want to install any software, you can also use the Cipher utility included in Windows. This is also very effective and simple. See my article on this topic: Using Cipher to Erase Disk Unused or Free Space.

Download EraseFreeSpace.wsf

Source Code

<?php 

header('Content-type: text/xml');
header('Content-disposition: attachment; filename="EraseFreeSpace.wsf"');

echo '<'.'?'; ?>xml version="1.0" encoding="UTF-8"?>
<job>
<runtime>
<description>
EraseFreeSpace.wsf
A script that fills a drive with blank files to overwrite all the free space.
Copyright (C) 2005  Shailesh N. Humbad https://www.somacon.com/ 

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
http://www.gnu.org/copyleft/gpl.html

SYSTEM REQUIREMENTS:
1. Microsoft Windows Script Version 5.6
http://msdn.microsoft.com/library/default.asp?url=/downloads/list/webdev.asp

INSTRUCTIONS

1. Copy the script to the drive you want to fill up.
2. Run the script by double-clicking on it.

NOTES

* This script should not be used on the system drive (usually C:).
* The blank files will be created in "X:\EraseFreeSpaceBlankFiles".
* After the script completes, you may optionally delete the blank files.
* If you stop and restart the script without deleting the blank files, 
   it will continue where it left off.
* The script will show space left, estimated time left, and write speed.
   You can use this script as a crude disk write speed benchmark!
* This script will fill the drive with files containing zeros.  Since most
   disk drives have areas of reserved storage space, no guarantee is made 
   that all information on the disk will be overwritten.

</description>
</runtime>
<script language="VBScript">
<![CDATA[
Option Explicit

' Force execution from command shell (cscript) mode
ForceCScriptExecution

' Run the main program and quit
RunMainProgram

WScript.Quit

' ======================================================
' ================  Main Program Section  ==============
' ======================================================

Sub RunMainProgram()
    Dim objFSO, objDrive, objBlankFile
    Dim NumberCreated, TotalRunTime
    Dim S, E, BlankIndex, ErrorNumber
    Dim AverageSpeedMBPerSecond, TimeLeftSeconds
    Dim str10MBString, BlanksPath, BlankFilePath, DiskFreeSpace
    Const BytesInMegabyte = 1048576
    Const BytesInGigabyte = 1073741824
    Dim BlankFileLength : BlankFileLength = BytesInMegabyte * 10

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Get the number of gigabytes of the current drive
    Set objDrive = objFSO.GetDrive(objFSO.GetDriveName(WScript.ScriptFullName))

    ' Create the blank folder
    BlanksPath = objFSO.BuildPath(_
        objFSO.GetDriveName(WScript.ScriptFullName), _
        "\EraseFreeSpaceBlankFiles")
    If Not objFSO.FolderExists(BlanksPath) Then
        objFSO.CreateFolder BlanksPath
    End If
    WScript.Echo "Filling " & _
        FormatNumber(Round(objDrive.FreeSpace/BytesInGigabyte, 3),3) & _
        " GB of free space in drive " & _
        objFSO.GetDriveName(WScript.ScriptFullName)

    ' Loop until the the write fails
    NumberCreated = 0
    BlankIndex = 0
    TotalRunTime = 0
    str10MBString = String(BlankFileLength,"0")
    Do While True
        BlankIndex = BlankIndex + 1
        BlankFilePath = objFSO.BuildPath(BlanksPath, "\blank_" & _
            BlankIndex & ".zero")
        If Not objFSO.FileExists(BlankFilePath) Then
            DiskFreeSpace = objDrive.FreeSpace
            ' Create the blank file
            S = Timer
            ' Try to create the file
            On Error Resume Next
            Set objBlankFile = _
                objFSO.CreateTextFile(BlankFilePath, False, False)
            ErrorNumber = Err.Number
            On Error Goto 0
            If ErrorNumber = -2147024784 Then
                Exit Do
            ElseIf ErrorNumber <> 0 Then
                Err.Raise ErrorNumber, "EraseFreeSpace", ErrorNumber
            End If

            ' Reset the file to fill up the space
            If DiskFreeSpace < BlankFileLength Then
                str10MBString = String(DiskFreeSpace,"0")
            End If
            
            ' Try to write to the file
            On Error Resume Next
            objBlankFile.Write str10MBString
            ErrorNumber = Err.Number
            On Error Goto 0
            If ErrorNumber = -2147024784 Then
                Exit Do
            ElseIf ErrorNumber <> 0 Then
                Err.Raise ErrorNumber, "EraseFreeSpace"
            End If
            objBlankFile.Close
            Set objBlankFile = Nothing
            E = Timer
            If DiskFreeSpace > BlankFileLength Then
                NumberCreated = NumberCreated + 1
            End If
            TotalRunTime = TotalRunTime + (E - S)
            If TotalRunTime <> 0 Then
                AverageSpeedMBPerSecond = NumberCreated * 10 / TotalRunTime
                TimeLeftSeconds = ((DiskFreeSpace / BytesInMegabyte) / _
                    AverageSpeedMBPerSecond)
                WScript.Echo _
                    FormatNumber(Round(DiskFreeSpace/BytesInGigabyte, 3),3) & _
                    " GB remaining. Wrote " & _
                    FormatNumber(Round(NumberCreated * 10 / 1000,2),2) & _
                    " GB at " & _
                    FormatNumber(Round(AverageSpeedMBPerSecond, 2),2) & _
                    " MB/s. Minutes left " & _
                    LeftPad(Int(TimeLeftSeconds/60), "0", 3) & ":" & _
                    LeftPad(TimeLeftSeconds Mod 60, "0", 2)
            End If
        End If
    Loop
    WScript.Echo "Completed filling drive. Press CTRL-C to Exit"
    WScript.Sleep 60*1000*60

End Sub

' Force execution in cscript.exe rather than wscript.exe
Function ForceCScriptExecution()
    Dim objShell

    ' If running in cscript, do nothing
    If LCase(Right(WScript.FullName,11)) = "cscript.exe" Then
        Exit Function
    End If

    ' If running in wscript, execute the script using cscript
    ' and then quit this script
    If LCase(Right(WScript.FullName,11)) = "wscript.exe" Then
        Set objShell = CreateObject("WScript.Shell")
        objShell.Run "cscript.exe """ & WScript.ScriptFullName & """", 1, False
        Set objShell = Nothing
        WScript.Quit
    End If
    
    ' If script engine anything else, quit with an error
    WScript.Echo "Invalid scripting engine."
    WScript.Quit
End Function

' Pad the string with the character specified
' on the left so that the resulting length of
' the string is at least LengthToPad
' Return the padded value as a string 
' Null values are treated as empty strings
Function LeftPad(ByVal ValueToPad, ByVal Character, ByVal LengthToPad)
    Dim ConvertedValue, CountCharacters
    If Len(CStr(Character)) <> 1 Then
        Serr "LeftPad: Character not length one."
    End If
    If Not IsNumeric(LengthToPad) Then
        Serr "LeftPad: Length not numeric."
    End If
    If IsNull(ValueToPad) Then
        ValueToPad = ""
    Else
        ConvertedValue = CStr(ValueToPad)
    End If
    CountCharacters = LengthToPad - Len(ConvertedValue)
    If CountCharacters > 0 Then
        LeftPad = CStr(String(CountCharacters, Character) & ConvertedValue)
    Else
        LeftPad = CStr(ConvertedValue)
    End If
End Function


]]>
</script>
</job>

Have you heard of the new, free Automated Feeds offered by Google Merchant Center? Learn more in Aten Software's latest blog post comparing them to traditional data feed files.
Created 2005-06-19, Last Modified 2011-07-24, © Shailesh N. Humbad
Disclaimer: This content is provided as-is. The information may be incorrect.