Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Powerbits #4 – Generating a Text File of a Specific Size

I was recently helping with a problem in a forum post.  On this occassion the person who posted the thread received an elegant text parsing solution that just didn’t scale.  In order to help him further I needed to be able to generate a similar file to the one he was using.  I needed a 50 MB text file with specific text repeated over and over.  So let’s look at today’s powerbit that does just that:

$file = "test.txt"
$size = 50MB
$writechunks = 10MB
$string = @"
abc 123
def 456
"" |Out-File $file

while ((Get-ChildItem $file).length -lt $size) {
    $string*($writechunks/16/$string.length) |Out-File $file -Append

The file may not be exactly the size requested, but it’s close enough for my purposes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: