Sign in to follow this  
Followers 0
Endgame

Create Filler File

1 post in this topic

While working on a project, I came across a requirement to generate a file of size X. I decided to create a little AutoIt fuction to perform this process. Thought I would share it with everyone else in case they could use something like this.

#include <String.au3>

Func CreateFileSpecificSize($FileName,$Size)
    Dim $FileSize = 0
    
    If $Size>DriveSpaceFree(StringLeft($FileName,3))*1048576 Then
        Return 1
    EndIf
    
    $pGDFS = DllStructCreate("dword;dword;dword;dword")
    $ret = DllCall("kernel32.dll","int","GetDiskFreeSpace","str", StringLeft($FileName,3),"ptr",DllStructGetPtr($pGDFS,1),"ptr",DllStructGetPtr($pGDFS,2),"ptr",DllStructGetPtr($pGDFS,3),"ptr",DllStructGetPtr($pGDFS,4))
    $ClusterSize = DllStructGetData($pGDFS, 1) * DllStructGetData($pGDFS, 2)
    $pGDFS=0

    $NewSize = Int($Size/$ClusterSize)*$ClusterSize
    
    ProgressOn("Building " & $NewSize & " Byte File","0 Bytes","0% Complete",0,0)

    Dim $OneK = _StringRepeat("a",1024)
    Dim $OneM = _StringRepeat($OneK,1024)

    While $FileSize<$NewSize
        $FileDifference = $NewSize-$FileSize
        
        Select
            Case $FileDifference>1048576
                FileWrite($FileName,$OneM)
            Case $FileDifference<=1048576 And $FileDifference>1024
                FileWrite($FileName,$OneK)
            Case Else
                FileWrite($FileName,_StringRepeat("a",$FileDifference))
        EndSelect
        
        $FileSize = FileGetSize($FileName)
        $Percentage = Int($FileSize/$NewSize*100)
        ProgressSet($Percentage,$Percentage & "% Complete",$FileSize & " Bytes")
    WEnd

    ProgressOff()
    
    Return 0
EndFunc

Note the need for the #include <String.au3> outside the function.

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