Jump to content
Sign in to follow this  
buttercheese

create a file containing only zeros or random data

Recommended Posts

Hi together,

I need a tool, that can create files of a desired size. The files should contain only zeros or random signs. It´s recommended that you can handle the filesize exactly. Taht means, that creating a file with 12345 Bytes should contain 12345 Bytes. Influencing name and suffix of the file would be fine but doesn´t really matters.

Thanks a lot for all of your ideas.

Cheers

buttercheese

Share this post


Link to post
Share on other sites

U could try something like (NOT TESTED):

While 1
If FileGetSize("C:\File.txt") = "12345" Then
;
; Do Something
;
ExitLoop
Else
LogFileWrite("C:\File.txt", "0")
EndIf
Wend

Exit

Func LogFileWrite($filename, $text)
  While 1
     If 1 = FileWrite($filename, $text) Then  ExitLoop 
     Sleep(300)
  Wend
  Return
EndFunc

Share this post


Link to post
Share on other sites

Just tested the script and it works, but creating a file of 10000 kb took me approx. 162047 millisec. according to the Auto-It Timer. :lol::D

So very big files will be a problem if you don't have much time...... :idiot:

Here's my testing script:

FileDelete("C:\File.txt")

$begin = TimerInit()

While 1
If FileGetSize("C:\File.txt") = "10000" Then
;
; Do Something
;
ExitLoop
Else
LogFileWrite("C:\File.txt", "0")
EndIf
Wend
$dif = TimerDiff($begin)
MsgBox(0,"Time Difference",$dif)

Exit

Func LogFileWrite($filename, $text)
  While 1
     If 1 = FileWrite($filename, $text) Then  ExitLoop 
     Sleep(300)
  Wend
  Return
EndFunc

Share this post


Link to post
Share on other sites

Try using a handle instead of opening/writing/closing the file every time, which is what happens when you don't specify a handle. And remove the Sleep(), or at least reduce it to like 10 ms (Probably should just remove it, though).

Edited by Valik

Share this post


Link to post
Share on other sites

To speed up things, first create a/some larger string(s) in memory and append that to the file, it helps to minimize disk access.

Edited by Tys

Share this post


Link to post
Share on other sites

Maybe I mis-interpret the original question. But I think buttercheese ment the following:

Func FileFillRandom($Filename,$Size, $Zero)
;
; Fills a file up to given size. Zero or random bytes by flag. 
;
; Arguments:
; $Filename Name of file to create
; $Size     Size the file should be
; $Zero        Flag: 1=fill only with zeros
;                   0=fill with random characters
;
; Returns:
; -1           If file could not be created
; otherwise Number of bytes written (if successfull this should be equal to $Size)
;

 Dim $Counter;Just a counter
 Dim $fHandle;Filehandle to filename

 $fHandle=FileOpen($Filename,2); Open the file in write mode
 if $fHandle = -1 then
    Msgbox (0,"FileFillRandom","Error writing to file: " & $Filename)
    Return -1
 endif

 For $Counter = 1 to $Size
   if $Zero = 1 then
    if not FileWrite($fHandle,chr(0)) then exitloop
   else
    if not FileWrite($fHandle,chr(Random(0,255))) then exitloop
   endif
 Next

 FileClose($fHandle); Close the file

 Return $Counter -1; Must be -1 because the for-loop has increased it.

EndFunc

You can call this function like this:

; Test the function FileFillRandom()
Dim $Return; Just a return value


$Return=FileFillRandom("C:\TESTFILE.DAT",100,0)
if $Return <> 100 then 
    MsgBox (0,"FileFillRandom Test","Error: written: " & $Return & " but expected: 100")
else
    MsgBox (0,"FileFillRandom Test","Test 1 ok, written: " & $Return & " bytes.")
endif

$Return=FileFillRandom("C:\TESTFILE2.DAT",200,1)
if $Return <> 200 then 
    MsgBox (0,"FileFillRandom Test","Error: written: " & $Return & " but expected: 200")
else
    MsgBox (0,"FileFillRandom Test","Test 2 ok, written: " & $Return & " bytes.")
endif

Regards,

-Sven

EDIT: I opened file in append (1) mode instead of overwrite (=2). Changed this now in the code.

Edited by SvenP

Share this post


Link to post
Share on other sites

@SvenP :idiot:

.. I changed the "Return" line in your func as follows:

;   Return $Counter -1; Must be -1 because the for-loop has increased it.
    Return FileGetSize($FileName)

.. and got some interesting results:

  • asking for a file of 1000 random bytes yields files of varying size: sometimes 992, sometimes 996, sometimes 995 etc.
  • asking for a file of null characters yields an empty file.
You might want to add 1 to the Random number generated, so as to avoid the file-termination character. Also probably explains why a non-random file is "empty"

:D

Share this post


Link to post
Share on other sites

Also couldn't you just break the size, for example 1024, into larger chunks? Like 1000, 20, 4?

I think I saw that somewhere else, but it wasn't faster. Just throwing that up in the air :idiot:


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

Hey tremendous,

thanks alot for your feedback. :-)

May I tell you some more about my idea?

As I have seen, generating all the random stuff takes a lot of time. I want to use the code to erase data from HD after formatting. Getting data after formatting should be made unpossible. So, if the file is about 40GB, you can spend holidays until the job is done. ;-) The content of the file could be whatever. I just want to create a big file, write to disk, erase, create a second big file, write to disk, erase... (about 8 times) and now the HD is really cleaned and ready to become second hand instead of becoming garbage for security reasons. The selecting of the filesize should be given for leaving the needed data (if there is).

Your ideas are still welcome, thanks

buttercheese

Share this post


Link to post
Share on other sites

Question:

How about if I append files?

Lets say, that I´ve got or generate 1 file with 10MB, 1 with 1MB... and so on...

When the file should be f.e. 402 MB I append 40 times the 10MB file and 2 times the 1MB.

Is that an idea to start, a feasable way?

Cheers

Buttercheese

Edited by buttercheese

Share this post


Link to post
Share on other sites

If this is your intention, then I'd look for some free software that already does this. It'll be written in a much more efficient method and will do a much more thorough job.

Share this post


Link to post
Share on other sites

If this is your intention, then I'd look for some free software that already does this.  It'll be written in a much more efficient method and will do a much more thorough job.

<{POST_SNAPBACK}>

Stick in the mud.........

Anyway, I think that writing this is a good idea for basic *HOME* security for the paranoid user...and it is a lesson on how to make files of X size with random data in it.

I will concede that Valik does have a valid point.

Norton has a good file wiper, as well as blank space wiping that only took about 3 hours to do my 40GB drive with government settings (3 passes: write all 1's, write all 0's, write random binary pattern.)

Spybot S&D also has a "file shredder" that performs a secure removal of a file, but you have to use the Spybot GUI to access it.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...