Last active
August 29, 2015 13:56
-
-
Save Nora-Ballard/9019255 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-SparseFile | |
{ | |
param( | |
[string]$filepath, | |
[long]$sizeInBytes, | |
[switch]$passthrough | |
) | |
if (test-path $filepath) | |
{ | |
throw "File '$filepath' already exists." | |
} | |
else | |
{ | |
try | |
{ | |
$fs = new-object System.IO.FileStream $filepath, Create, ReadWrite | |
$fs.SetLength($sizeInBytes) | |
} | |
catch {} | |
finally | |
{ | |
$fs.Close() | |
} | |
if ($passthrough) | |
{ | |
write-output $( get-item $filepath ) | |
} | |
} | |
} | |
function New-PlaceholderFile | |
{ | |
param( | |
[int32]$Count, | |
[string]$Path = $pwd | |
) | |
$FileNamePrefix = '\Placeholder_' | |
$FileExtension = '.file' | |
$sizeInBytes = 5GB | |
$CountOffset = 0 | |
1..$count | ForEach-Object { | |
while ($filepath -eq $null) | |
{ | |
$Padding = 3 | |
do | |
{ | |
$FileNumber = $( $_ + $CountOffset ).ToString() | |
if ($count.tostring().length -gt $Padding) | |
{ | |
$FileNumberPadded = $FileNumber.padleft( $($count.tostring().length), '0') | |
} | |
else | |
{ | |
$FileNumberPadded = $FileNumber.padleft($Padding,'0') | |
} | |
$filename = $FileNamePrefix + $FileNumberPadded + $FileExtension | |
$filepath = Join-path $path $filename | |
if (test-path $filepath) | |
{ | |
$filepath = $null | |
$CountOffset++ | |
} | |
} while (!$filepath) | |
} | |
$Message = "Create new sparce file '$FilePath'" | |
Write-Debug $Message | |
New-SparseFile -filepath $filepath -sizeInBytes $sizeInBytes -passthrough | |
$filepath = $null | |
} | |
} | |
#New-SparseFile -FilePath '.\test4.plc' -sizeInBytes 5GB 5 -passthrough | |
#New-PlaceholderFile -count 15 -path 'D:' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment