Jump to content

DummyFile_Create


jaberwacky
 Share

Recommended Posts

If you ever find yourself in need of a dummy file for testing then this function may be what you need.

Feed the function a size in megabytes and give it a location to store the dummy file and you're done.

Large files may take a while depending on your system.

[update: April 28, 2011] -- Now you can create UNICODE files!

#include-once

Func DummyFile_Create( Const $iTotalB , Const $fileSaveLoc , Const $alphaNumeric = 1 )
    Local $string
    Local Const $mode = 2 + 8
    Local Const $binary = 16
    Local Const $utf16 = 32

    Switch $alphaNumeric
        Case 0 ; numeric data
            FileOpen( $fileSaveLoc , $mode )
            If @error = -1 Then Return SetError( 1 , 0 , 1 )

            For $i = 1 To $iTotalB
                $string &= Random( 0 , 9 , 1 )
            Next

            FileWrite( 1 , $string )

        Case 1 ; ASCII alphanumeric data
            FileOpen( $fileSaveLoc , $mode )
            If @error = -1 Then SetError( 1 , 0 , 1 )

            For $i = 1 To $iTotalB
                $string &= Chr( Random( 0 , 255 , 1 ) )
            Next

            FileWrite( 1 , $string )

        Case 2 ; dummy file with no data
            ; trancexx
            FileOpen( $fileSaveLoc , $mode + $binary )
            If @error = -1 Then SetError( 1 , 0 , 1 )

            FileSetPos( 1 , $iTotalB - 1 , 0 )

            FileWrite( 1 , Chr( 32 ) ) ; Chr( 32 ) = NUL

        Case 3 ; UNICODE data
            FileOpen( $fileSaveLoc , $mode + $utf16 )
            If @error = -1 Then SetError( 1 , 0 , 1 )

            For $i = 1 To $iTotalB
                $string &= ChrW( Random( 256 , 65535 , 1 ) )
            Next

            FileWrite( 1 , $string )
    EndSwitch

    FileClose( 1 )
EndFunc
Edited by LaCastiglione
Link to comment
Share on other sites

If you just need a dummy file, period (without random #'s), you could easily quicken the routine by using a DLLStruct:

Func _FileCreateDummyInMB($sFilename,$iTotalMB)
    Local $stData=DllStructCreate("byte[1048576]"),$binData=DllStructGetData($stData,1)
    Local $hFile=FileOpen($sFilename,2)
    For $i=1 To $iTotalMB
        FileWrite($hFile,$binData)
    Next
    FileClose($hFile)
EndFunc

_FileCreateDummyInMB(@DesktopDir&"\dummyfile.bin",10)
Link to comment
Share on other sites

Very clever, trancexx. I didn't know you could set a position on a newly created file that is past the current position. Good to know :blink:

Link to comment
Share on other sites

Hmmm, what I use one of the two methods posted above to create a file of predetermined size. Then create a huge string that would fit snug into the file and just write it all at once?

Edited by jaberwocky6669
Link to comment
Share on other sites

Hmmm, what I use one of the two methods posted above to create a file of predetermined size. Then create a huge string that would fit snug into the file and just write it all at once?

Depending on your definition of 'Huge' RAM usage might go through the roof.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...