Craig Forrester

The Pleasure of Finding Things Out

github linkedin email rss
Scripting Notebook: Create a File of a Specific Size
March 1, 2018
2 minutes read

This is the first post in a series I’m calling “Scripting Notebook,” in which I will explore ways of accomplishing various scripting tasks across several languages: Bash, CMD, PowerShell, and Python.

Creating a File of Arbitrary Size

Sometimes when you’re testing, for whatever reason, you need one or more files that are a particular size. You don’t care what they are, you just need some data to take up disk space, transfer over the network, etc. So here, in our four target languages,1 I’m going to show how to do exactly that.

CMD

In CMD, otherwise known as the Windows Command Prompt, we rely on an old tool called Fsutil:

REM Create 1 GB test file:
fsutil file createnew one-gigabyte-file-cmd.dat 1073741824

PowerShell

We’re cheating a little bit here, using .NET Framework’s System.IO.FileStream Class to do most of the work…

# Create 1 GB test file
$f = New-Object System.IO.FileStream .\one-gigabyte-file-powershell.dat, Create, ReadWrite
$f.SetLength(1GB)
$f.Close()

You may try a “pure” PowerShell implementation like this, it will take a very, very long time:

# DO NOT do this. Poor memory utilization, very long execution time
1..1024 | %{ Add-Content -Value (New-Object byte[] 1mb) -Encoding Byte -Path .\one-gigabyte-file-ps1.dat }

And while you may be tempted to “optimize” that last implementation by using larger objects, it consumes a ton of memory and nasty things will happen. You have been warned!

Python

Here’s a clean and fast implementation in Python. The with operator handles closing out the file for us, as a bonus:

with open("one-gigabyte-file-python.dat", "wb") as out:
    out.truncate(1024**3)

Bash

Then there’s the tried-and-true dd command in bash:

dd if=/dev/zero of=one-gigabyte-file-bash.dat bs=1073741824 count=1

This works well on Windows too, if you’ve got WSL, MinGW or Cygwin installed.

Additional Reading


  1. I’m using the term “language” very loosely when referring to CMD and Bash because, while these shells each have their own language with their own syntax, we will often be relying on other command line software to do the work — as is the case immediately here with CMD, where we’re using fsutil to create our file. [return]

Back to posts