How to Generate File of a determinate Size in Windows?
Solution 1
I found this Page, I try it and work great.
To create a single File use
fsutil file createnew filename filesize
To create a lot of Files use
For /L %i in (1,1,25000) do fsutil file createnew A%i.tmp 12288
will create 25,000 files of 12288 bytes (12KB) named A1.tmp, A2.tmp, A3,tmp…
Solution 2
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()
Solution 3
c:\>fsutil file createnew /?
Usage : fsutil file createnew <filename> <length>
Eg : fsutil file createnew C:\testfile.txt 1000
Related videos on Youtube
Jedi Master Spooky
I am from Argentina. I graduated from ITBA as a Software Engineer. I am Married with 1 baby girl and 1 baby boy. This week I was awarded with the "5 Estrellas Platino" in .NET Development. I am a Microsoft Active Professional (MAP) 2009 and 2010
Updated on March 31, 2022Comments
-
Jedi Master Spooky about 2 years
How is the Best and fastest way to do it?
-
ICTMitchell about 15 yearsWhat language/environment in Windows? cmd-files? Powershell? C#?
-
-
ZXX over 13 yearsBeware that this only generates so called sparse file - you get a record in a folder with a file size but not actual data clusters assigned with the file. So for purposes like testing, zeroing or blocking off large blocks of data clusters it's useless.
-
Anders over 3 yearsI might add that this is extremely fast to do.
-
mklement0 about 3 years@ZXX: I don't think that
fsutile file createnew <file> <size>
creates sparse files - the docs make no mention of that, and runningfsutil sparse queryflag <file>
on a file created this way indeed suggests that the file is not sparse. -
sawyer seguin almost 3 years@msanford In fact, you can use relative paths, but they will be relative to the directory PowerShell was started in (i.e.
[IO.Directory]::GetCurrentDirectory()
), and not relative to the directory you switched to using thecd
orSet-Location
command. -
sawyer seguin almost 3 yearsWhich is generally true for any kind of COM or .NET interop you perform inside PowerShell.
-
msanford almost 3 years@mihi Thank you for that additional details; I assumed it was something of this nature! So, for those getting New-Object : Exception calling ".ctor" with "3" argument(s): "Could not find a part of the path c:\some\path\test.dat` see above :)
-
thatOneGuy over 2 years@Anders unbelievably fast