Created
November 5, 2024 13:44
-
-
Save Rapidhands/7855f8ddfee8c942fcdc95cc68bf7832 to your computer and use it in GitHub Desktop.
Create-SizedEmptyDummyFile
This file contains hidden or 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
<# | |
.SYNOPSIS | |
Creates one or more empty dummy files of a specified size in a given directory. | |
.DESCRIPTION | |
The Create-SizedEmptyDummyFile function allows you to create one or more empty dummy files of a specified size in a given directory. It supports the creation of both regular files and sparse files, depending on the file size and the user's permissions. | |
.PARAMETER NumberOfFiles | |
The number of files to create. | |
.PARAMETER FileSizeKB | |
The size of each file in kilobytes. | |
.PARAMETER DestinationDirectory | |
The directory where the files will be created. The directory must exist and the user must have write permissions. | |
.PARAMETER UseSparse | |
If set, the function will attempt to create sparse files instead of regular files. Sparse files only allocate physical disk space for the used portions of the file, saving disk space. | |
.PARAMETER BufferSize | |
The size of the buffer used for writing the file contents. This parameter only applies to regular files, not sparse files. | |
.INPUTS | |
This cmdlet has no inputs. | |
.OUTPUTS | |
The cmdlet creates an array of one or more objects with the following properties: | |
FilePath: The full path of the created file. | |
FileSize: The size of the created file in bytes. | |
.EXAMPLE | |
Create 5 files of 512 KB each in the "C:\Temp" directory: | |
Create-SizedEmptyDummyFile -NumberOfFiles 5 -FileSizeKB 512 -DestinationDirectory "C:\Temp" -Verbose | |
.EXAMPLE | |
Create 3 sparse files of 2048 KB each in the "C:\Temp" directory (requires admin privileges): | |
Create-SizedEmptyDummyFile -NumberOfFiles 3 -FileSizeKB 2048 -DestinationDirectory "C:\Temp" -UseSparse -Verbose | |
.NOTES | |
File Name : Create-SizedEmptyDummyFile.ps1 | |
Version : V.1.0 | |
Date : 2023-05-12 | |
Author : O.FERRIERE | |
Change : V.1.0 - 2023-05-12 - Initial version | |
#> | |
function Create-SizedEmptyDummyFile | |
{ | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidateRange(1, [Int64]::MaxValue)] | |
[int64]$NumberOfFiles, | |
[Parameter(Mandatory = $true)] | |
[ValidateRange(1, [Int64]::MaxValue)] | |
[int64]$FileSizeKB, | |
[Parameter(Mandatory = $true)] | |
[ValidateScript({ | |
if (-Not (Test-Path $_)) | |
{ | |
throw 'Path does not exist' | |
} | |
if (-Not (Test-Path -Path $_ -PathType Container)) | |
{ | |
throw 'The Path argument must be a folder. File paths are not allowed.' | |
} | |
# Check write permissions | |
try | |
{ | |
$testFile = Join-Path $_ 'test.tmp' | |
[System.IO.File]::Create($testFile).Close() | |
Remove-Item $testFile | |
return $true | |
} | |
catch | |
{ | |
throw "Cannot write to directory: $_" | |
} | |
})] | |
[System.IO.DirectoryInfo]$DestinationDirectory, | |
[Parameter()] | |
[switch]$UseSparse, | |
[Parameter()] | |
[ValidateRange(1KB, 1GB)] | |
[int64]$BufferSize = 1MB | |
) | |
begin | |
{ | |
# Check if you have admin rights for fsutil | |
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
# Prepare file name format | |
$fileNameFormat = 'DummyFile_{0}KB_{1:D4}.txt' | |
# Calculate size in bytes | |
[int64]$SizeInBytes = $FileSizeKB * 1KB | |
} | |
process | |
{ | |
try | |
{ | |
for ($i = 1; $i -le $NumberOfFiles; $i++) | |
{ | |
$fileName = $fileNameFormat -f $FileSizeKB, $i | |
$filePath = Join-Path -Path $DestinationDirectory -ChildPath $fileName | |
if ($UseSparse -and $isAdmin) | |
{ | |
Write-Verbose "Creating sparse file: $filePath" | |
try | |
{ | |
$file = [System.IO.File]::Create($filePath) | |
$file.Close() | |
# Mark as sparse | |
$null = & fsutil sparse setflag "$filePath" | |
$file = [System.IO.File]::OpenWrite($filePath) | |
$file.SetLength($sizeInBytes) | |
$file.Close() | |
} | |
catch | |
{ | |
Write-Error "Failed to create sparse file: $_" | |
continue | |
} | |
} | |
else | |
{ | |
Write-Verbose "Creating regular file: $filePath" | |
try | |
{ | |
$buffer = New-Object byte[] 1MB | |
$stream = [System.IO.File]::Create($filePath) | |
$remainingBytes = $sizeInBytes | |
while ($remainingBytes -gt 0) | |
{ | |
$writeSize = [Math]::Min(1MB, $remainingBytes) | |
$stream.Write($buffer, 0, $writeSize) | |
$remainingBytes -= $writeSize | |
} | |
$stream.Close() | |
} | |
catch | |
{ | |
Write-Error "Failed to create file: $_" | |
continue | |
} | |
} | |
Write-Output "File Created: $filePath" | |
} | |
} | |
finally | |
{ | |
if ($null -ne $stream) | |
{ | |
$stream.Dispose() | |
} | |
if ($null -ne $file) | |
{ | |
$file.Dispose() | |
} | |
} | |
} | |
end | |
{ | |
Write-Output "All files were created in the directory: $DestinationDirectory" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment