Random Files Generator
26 Aug 2013Whilst doing some house keeping on some old VBScript files the other day
I came across one for creating test files. I’ve blogged previously about
creating test files using my MTF utility
and whilst it or fsutil
can be very useful, this script filled a
particular niche. It was created as part of a test suite of scripts it
creates multiple files of a specific size filled with random printable
content.
The script below shows an example of how to use the GenerateFiles function, which is the heart of the script. The function takes a set of five parameters.
- The number of files to be produced.
- The directory in which the files are to be written (including trailing slash).
- The file extension for the files to be written (no “.” required).
- The number of characters to be written to the file (1 character -> 1 byte).
- The fast processing flag (TRUE/FALSE).
Hopefully most of the parameters are self explanatory, but the last one is worth a bit more explanation.
When fast processing is enabled, the same basic content will be used for each file that is produced. It will be identical in each file save for the first few characters which will give the name of the file (without the file extension) followed by a space. When fast processing is disabled, the content in each file will be randomly generated independently for each file.
The file names used for the files are numeric starting at zero. The script knows how many files to produce and so prefixes each file name with the requisite number of zeros so that they order numerically in the file system when sorting by name.
e.g. if 100 files with a file extension of “txt” were being created, the file names would be 00.txt, 01,txt, …, 98.txt and 99.txt.
As a final note, the RANDOM_CHARACTERS
string (see FileContent
function) is used to generate the content for the files so if there are
any specific character sets you want to use (e.g. just numbers, no upper
case characters, no symbols) you can tailor it to suit your own needs.
Option Explicit
Const OUTPUT_FOLDER = "C:\Users\sylumer\Scripts\vbs\ManyTestFiles\Output\"
Const NO_OF_FILES = 5
'One character will stored as one byte of information
Const CHARACTERS_PER_FILE = 50000
Const FILE_EXTENSION = "txt"
'Const FAST = True
Const FAST = False
GenerateFiles NO_OF_FILES, OUTPUT_FOLDER, FILE_EXTENSION, CHARACTERS_PER_FILE, FAST
Function GenerateFiles(p_intNoOfFiles, p_strOuputDirectory, p_strFileExtension, p_intCharactersInEachFile, p_strFastProcessing)
Dim objFSO, intCounter, fhOutputFile, strFileName, strFileContent, strFileStaticContent
Set objFSO = CreateObject("Scripting.FileSystemObject")
'If speed is require rather than total randomness, we'll save some text to add on after the file ID
If p_strFastProcessing then
strFileStaticContent = FileContent(p_intCharactersInEachFile - Len(p_intNoOfFiles - 1) - 1)
End if
'Create the files using a numeric ID (starting at zero)
For intCounter = 0 to (p_intNoOfFiles - 1)
'Generate the next file name
strFileName = p_strOuputDirectory & ExpandNumber(intCounter, p_intNoOfFiles - 1) & "." & p_strFileExtension
'Write the appropriate quantity of content to file
Set fhOutputFile = objFSO.CreateTextFile(strFileName, true)
If p_strFastProcessing then
'Using the same random content in each file (with a prefix relating to the file ID
strFileContent = ExpandNumber(intCounter, p_intNoOfFiles - 1) & " " & strFileStaticContent
Else
'Generating unique random content for each file
strFileContent = FileContent(p_intCharactersInEachFile)
End If
fhOutputFile.Write(strFileContent)
fhOutputFile.Close
Next
'Notify user of completion
MsgBox p_intNoOfFiles & " files generated in" & vbCrLf & """" & OUTPUT_FOLDER & """", vbInformation, "Generation Complete"
End Function
'Pad a number to a specified number of characters by prefixing with zeros.
Function ExpandNumber(p_strNum, p_strBase)
ExpandNumber = p_strNum
While Len(ExpandNumber) < Len(p_strBase)
ExpandNumber = "0" & ExpandNumber
Wend
End Function
'Generate a string of random printable characters to a specified length.
Function FileContent(p_intLength)
Const RANDOM_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ1234567890!!?$%^&*()_+-={}[];'#:@~,./<>?\|`?
Dim intCounter
Randomize
For intCounter = 1 To p_intLength
FileContent = FileContent & Mid(RANDOM_CHARACTERS, Int(Rnd() * Len(RANDOM_CHARACTERS) + 1), 1)
Next
End Function