Reputation: 336
So, I have a folder and I'd like to create say 10 files of 150MB to test something. How can I achieve that?
Upvotes: 2
Views: 1220
Reputation: 336
This is an extension of the answer given at:
How to Generate File of a determinate Size in Windows?
All that would be missing is a for
loop and a max amount of files.
[IO.directory]::setCurrentDirectory($(get-location).Path) #Changes directory to current PS directory
[Int64]$size = 150MB #This allows you to set the size beforehand
[Int16]$AmountOfFiles = 10
for ($i = 0; $i -lt $AmountOfFiles; $i++) {
$f = new-object System.IO.FileStream ".\test_$i.dat", Create, ReadWrite
$f.SetLength($Size)
$f.Close()
}
And lets go one by one the lines, just for fun and clarity:
[IO.directory]::setCurrentDirectory($(get-location).Path) #Changes directory to current PS directory
As explained on the previous answer, PS session has an "underlying" path used by .Net that it's not the same as the "current directory" (get-location
or pwd
), therefore we have to "manually" change it. That's achieved with the $(Get-Location).Path
portion of the code, it will set the "underlying" path to the same path as the PSSession.
[Int64]$size = 150MB #This allows you to set the size beforehand
[Int16]$AmountOfFiles = 10
Self-explanatory but basically we can set the size and the amount of files we need.
The size can be anything, 1GB, 12MB, 15KB. Powershell will take care of the conversion.
for ($i = 0; $i -lt $AmountOfFiles; $i++)
An standard for loop:
i
on 0 (that can also be used inside the { }
of the loop).-lt
) the $AmountOfFiles
variable, so in this case $i
will reach 9 and then stop looping.$i
by oneFinally:
$f = new-object System.IO.FileStream ".\test_$i.dat", Create, ReadWrite
$f.SetLength($Size)
$f.Close()
Upvotes: 4