Quantcast
Channel: blog.atwork.at
Viewing all articles
Browse latest Browse all 1118

Create a file with specific size

0
0

Sometimes it is useful to create a file of a specific size to test network or internet speed or to upload it to a specific application. This is how you can create a file with a specific file size in Windows and with PowerShell.

Using the fsutil command line tool

For the start, we can use fsutil command with the file createnew option in a command prompt or in a PowerShell terminal for that purpose.

Note: You must enable Windows Subsystem for Linux before you can run fsutil. See more at the docs at fsutil (see alternatives with PowerShell below). If enabled, run the following command:

fsutil file createnew .\10mb-testfile.txt 10485760

The file size is passed in bytes:

  • 1 MB: 1024*1024=1048576
  • 10MB: 1024*1024*10=10485760
  • 100MB: 1024*1024*100=104857600
  • 1GB: 1024*1024*1024=1073741824
  • etc

The command creates a new file with the given bytes, as here.

image

The description of fsutil file informs about the functionality: Finds a file by user name (if Disk Quotas are enabled), queries allocated ranges for a file, sets a file's short name, sets a file's valid data length, sets zero data for a file, creates a new file of a specified size, finds a file ID if given the name, or finds a file link name for a specified file ID.

When we check, we see the created file with the specified size.

image

The content of the generated file is NUL (a null byte containing the value zero, or zero byte).

image

Just to mention: Of course, if we zip this file, we end up with a much smaller file because the content is perfect for zipping.

image

We need to keep this in mind if we want to use that file for data transfers. File transfer algorithms recognize and pack such files very well.

Using PowerShell method 1

Another method is using PowerShell. This is more than a one-liner, but it also does the job.

# method 1: use the file object
$file = New-Object System.IO.FileStream ".\1kb-testfile.txt", Create, ReadWrite
$file.SetLength(1kb)
$file.Close()

PowerShell understands the size values very nicely: KB, MB, GB, etc.
So, we can use 5kb for 5 Kilobytes, 10mb for 10 Megabytes, etc for the SetLength method. This is much more useable.

The file is generated containing zero bytes as before, and we get the same result.

Using PowerShell method 2

To be more flexible with the file content, we can use the following code.

# method 2: create a new test file with random content
$out = New-Object byte[] 1kb;
(New-Object Random).NextBytes($out);
[IO.File]::WriteAllBytes(".\1kb-testfile.txt", $out)

We first create a new byte array with a specific size and with null bytes, and fill it with random bytes. Then, the output is written to a file.

The file content no longer consists of zero bytes, but of random characters. The result can look like this.

image

This can be useful to bypass file transfer optimizations.

I hope this article will help to quickly generate files of a specific file size as needed for various testing purposes.


Viewing all articles
Browse latest Browse all 1118

Latest Images

Trending Articles