Sign in to follow this  
Followers 0
SpookMeister

Filling unused disk space with zeros

4 posts in this topic

#1 ·  Posted (edited)

I've been playing around with a really nice drive imaging tool g4u recently, and I'm wanting to make a tool to help the process along.

Specifically with reducing the size of the image (see this link). To summarize that link, to get the best image size, it is worth while to over-write any unused disk space with zeros before performing the disk imaging. He makes some suggestions on tool that will do this task for you, but all of them either require installing software onto the system (perl script, a pascal solution that I don't fully understand, or eraser), or require user intervention at a specific point in time (cipher).

I'm thinking that for my needs, creating a file full of zeros that is approximately the size of the remaining space on the disk should do the trick, it should also be fairly easy to do in autoit.... I think.

So the question I have before I start testing this is, do you think I will run into issues once the file gets to the filling up the drive?

Here's the code I threw up to show my idea:

#include <GUIConstants.au3>
Global $g_szVersion = "DriveZero v1.0"
If WinExists($g_szVersion) Then Exit ; It's already running
AutoItWinSetTitle($g_szVersion)

Global $var = "C:\"
Global $line = ""
Global $tmpfile = ""
For $i = 1 To 1024
    $line = $line & "0"
Next
For $i = 1 to 10
$line = $line & $line
Next

GUICreate($g_szVersion, 300, 190) ; will create a dialog box that when displayed is centered

$label1 = GUICtrlCreateLabel("C:\", 60, 30, 220, 30, $SS_SUNKEN)
$label2 = GUICtrlCreateLabel("", 20, 150, 260, 20, $SS_SUNKEN)
$prog1 = GUICtrlCreateProgress(20, 120, 260, 10)
$prog2 = GUICtrlCreateProgress(20, 135, 260, 10)
$btn1 = GUICtrlCreateButton("...", 20, 30, 30, 30)
$btn2 = GUICtrlCreateButton("Zero out the free space on the drive", 20, 80, 260, 30)
GUICtrlSetState(-1, $GUI_FOCUS) ; the focus is on this button

GUISetState() ; will display an empty dialog box

; Run the GUI until the dialog is closed
Do
    $msg = GUIGetMsg()
    Select
        Case $msg = $btn1
            $var = FileSelectFolder("Choose a drive.", "")
            If $var = "" Then $var = "C:\"
            GUICtrlSetData($label1, $var)
        Case $msg = $btn2
            _zerodrive($var)
    EndSelect
Until $msg = $GUI_EVENT_CLOSE
If $tmpfile <> "" Then FileDelete($tmpfile)

Func _zerodrive($drv)
    GUICtrlSetState($btn1, $GUI_DISABLE)
    GUICtrlSetState($btn2, $GUI_DISABLE)
    If StringLen($drv) > 3 Then $drv = StringTrimRight($drv, StringLen($drv) - 3) ;ensure just using the root drive of folder selected
    $szDrv = Int(DriveSpaceFree($drv))
    $tmpfile = $drv & @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & ".txt"
    FileOpen($tmpfile, 1)
    $pct = 0
    While 1
        $tmpsize = Int((FileGetSize($tmpfile) / 1024) / 1024) ; in mb
        GUICtrlSetData($label2, " " & $tmpsize & " of " & $szDrv)
        $movebar = Int(($tmpsize / $szDrv) * 100)
        GUICtrlSetData($prog2, $movebar)
        If Int(DriveSpaceFree($drv)) < 2 Then ExitLoop
        FileWriteLine($tmpfile, $line)
        $pct = $pct + 1
        If $pct > 100 Then $pct = 0
        GUICtrlSetData($prog1, $pct)
    WEnd
    FileClose($tmpfile)
EndFunc   ;==>_zerodrive

Suggestions and opinions welcome

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites



Well if C is your sys part and especially if it also contains your swapfile, you might want to set your swapfile to a big fixed size and turn off the warning on low diskspace.


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites

Windows will barf if your system drive and swap file drive have critically low disk space. Don't try it unless you don't need your data...

Doesn't your image software have an option to ignore unused disk space?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0