Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate File of a determinate Size in Windows?

How is the Best and fastest way to do it?

like image 748
Jedi Master Spooky Avatar asked Apr 30 '09 17:04

Jedi Master Spooky


1 Answers

PowerShell:

$f = new-object System.IO.FileStream c:\temp\test.dat, Create, ReadWrite
$f.SetLength(42MB)
$f.Close()

This will create a file c:\temp\test.dat that consists of 42MB of nullbytes. You can of course just give byte count instead as well. (42MB = 42 * 1048576 for PowerShell).

Note:
Keep in mind that unless you specify the full path (in my example, C:\temp\test.dat), using relative paths (i.e .\test.dat) will only create files on the directory where PS was started in, i.e. ["C:\users\currentUser"] if you start PS from the Run... command, or ["C:\Windows\system32"] if you start it with Shift+RightClick (OR) Alt->F>S>R on the windows explorer.
To workaround this, you must change the underlying directory (you can check it with this as a command on PS: [IO.directory]::GetCurrentDirectory()) to the current path you're on in the PS console, simply including the line:

[IO.directory]::SetCurrentDirectory($(get-location).Path)

So, to wrap it up, you make it work with the relative path you're currently in:

[IO.directory]::setCurrentDirectory($(get-location).Path) #Changes directory to current PS directory
[Int64]$size = 150MB #This allows you to set the size beforehand
$f = new-object System.IO.FileStream .\test.dat, Create, ReadWrite
$f.SetLength($size)
$f.Close()
like image 104
mihi Avatar answered Sep 17 '22 15:09

mihi