cancel
Showing results for 
Search instead for 
Did you mean: 

Windows VBScript to create random test data...

sdo
Moderator
Moderator
Partner    VIP    Certified

Sometimes we only need one, or a few, small files for functional testing, and sometimes it's useful to always have truely random data as part of a test.

And some of us cannot import unknown executable code, but it should be quite clear that this short script is safe as a text file.

The script just creates a ".bin" file of the same name as the script.  If anyone wants the script enhanced to accept proper named switches/parameters like -size and -count and -name, then let me know and I'll re-work it do so.

How to use it:

1) Save it as "make-random.vbs"

2) Then to run you can do:

> cscript make-random.vbs
...which will default to creating a 1MB file name "make-random.bin"

> cscript make-random.vbs a.bin 10
...which will create a 10MB file named "a.bin".

Here's the code:

Option Explicit
Dim go_fso
Dim gs_script_spec, gs_script_path, gs_script_name
Dim gs_arg_file, gs_arg_mb, gl_arg_mb
  Call s_init()
  Call s_method2()
WScript.Quit


Sub s_init()
  Set go_fso = CreateObject( "Scripting.FileSystemObject" )
  gs_script_spec = WScript.ScriptFullName
  gs_script_path = go_fso.GetParentFolderName( gs_script_spec )
  gs_script_name = go_fso.GetBaseName(         gs_script_spec )
  gs_arg_file = gs_script_name & ".bin"
  gl_arg_mb   = 1
  Select Case WScript.Arguments.Count
  Case 0
  Case 1
    gs_arg_file = WScript.Arguments.Item( 0 )
  Case 2
    gs_arg_file = WScript.Arguments.Item( 0 )
    gs_arg_mb   = WScript.Arguments.Item( 1 )
    If Not IsNumeric( gs_arg_mb ) Then Call s_abort( "P2 must be numeric size for MB" )
    gl_arg_mb = CLng( gs_arg_mb )
    If gl_arg_mb < 1 Then Call s_abort( "P2 must be 1 or more" )
  Case Else
    Call s_abort( "too many arguments, only 2 allowed" )
  End Select
  Randomize
End Sub


Sub s_abort( ps_text )
  WScript.Echo ps_text
  WScript.Quit( 1 )
End Sub


Sub s_method2()
  Dim lo_stream, lx_data(511), ll_i, ll_j, ls_buffer, ll_MB
  Set lo_stream = CreateObject( "ADODB.Stream" )
  lo_stream.Type = 1
  lo_stream.Open
  For ll_MB = 1 To gl_arg_mb
    WScript.StdOut.Write vbCr & ll_MB & " "
    For ll_j = 1 To 1024
      Randomize
      For ll_i = LBound( lx_data ) To UBound( lx_data )
        lx_data( ll_i ) = ChrW( ( Int( Rnd * 256 ) * 256 ) + Int( Rnd * 256 ) )
      next
      ls_buffer = Join( lx_data, "" )

      With CreateObject( "ADODB.Stream" )
        .type = 2
        .Open
        .WriteText ls_buffer
        .Position = 2
        .CopyTo lo_stream
      End With
    Next
  Next
  lo_stream.SaveToFile gs_arg_file, 2
  lo_stream.Close
End Sub
2 REPLIES 2

Nicolai
Moderator
Moderator
Partner    VIP   

Cooooool Smiley Happy

 

sdo
Moderator
Moderator
Partner    VIP    Certified

There is a limitation, in so far as the script uses RAM to buffer up the file's random data content before flushing it all to disk, so... it's probably best not to try to create a file that's too big.  I would have thought around 100MB to maybe around 1GB would be the max, depending upon how much free RAM is available on the system/server that it is being run upon.