A better “test file” maker

Recently I needed to do some testing in Exchange 2010 OWA using different sized files. Rather than e-mail some file I found on my system which happened to be “close enough” to the right file size, I decieded to write a Powershell type file which I could use to create these files.

I wanted to make sure I could create some dynamicly sized files so I created a type called TestFiles.

Here is the code I came up with:

Add-Type -TypeDefinition @"
	using System;
	public class TestFiles
	{
		private string OneKB = new string('X', 1024);
		private string OneMB = new string('X', (1024 * 1024));
		public void CreateFileWithSizeInMB(int size, string FilePath)
		{
			using (System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, true))
			{
				for (int i = 0; i < size; i++)
				{
					file.Write(OneMB);
				}
			}
		}

		public void CreateFileWithSizeInKB(int size, string FilePath)
		{
			using (System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, true))
			{
				for (int i = 0; i < size; i++)
				{
					file.Write(OneKB);
				}
			}
		}
	}
"@

The nice thing about this is if I want to create some other kind of test file, I can just add the code for it and all my test files can be found in one place. :d

Here is an example usage:

 $tf = new-object TestFiles
 $tf.CreateFileWithSizeInMB(12,"c:\users\glaisne\desktop\12MB.txt")

Leave a Reply